@nekzus/liop 2.1.0-alpha.1 → 2.1.0-alpha.10
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/README.md +30 -1
- package/dist/bin/agent.js +303 -4
- package/dist/bin/agent.js.map +1 -1
- package/dist/bridge.d.ts +2 -2
- package/dist/bridge.js +4 -1
- package/dist/chunk-32ADSAJS.js +104 -0
- package/dist/chunk-32ADSAJS.js.map +1 -0
- package/dist/chunk-72MNYFR6.js +64 -0
- package/dist/chunk-72MNYFR6.js.map +1 -0
- package/dist/chunk-E5QBDD5E.js +469 -0
- package/dist/chunk-E5QBDD5E.js.map +1 -0
- package/dist/chunk-EEYEVHO2.js +9329 -0
- package/dist/chunk-EEYEVHO2.js.map +1 -0
- package/dist/chunk-F5YNYL5L.js +14512 -0
- package/dist/chunk-F5YNYL5L.js.map +1 -0
- package/dist/chunk-HB5DXX3Q.js +1976 -0
- package/dist/chunk-HB5DXX3Q.js.map +1 -0
- package/dist/chunk-IJHTRIZC.js +56 -0
- package/dist/chunk-IJHTRIZC.js.map +1 -0
- package/dist/chunk-IMPCCZ2Y.js +463 -0
- package/dist/chunk-IMPCCZ2Y.js.map +1 -0
- package/dist/chunk-J3WPBMJ5.js +332 -0
- package/dist/chunk-J3WPBMJ5.js.map +1 -0
- package/dist/chunk-MCXMS5ZI.js +30 -0
- package/dist/chunk-MCXMS5ZI.js.map +1 -0
- package/dist/chunk-NJRSFFD7.js +815 -0
- package/dist/chunk-NJRSFFD7.js.map +1 -0
- package/dist/chunk-OUUTDSOW.js +24 -0
- package/dist/chunk-OUUTDSOW.js.map +1 -0
- package/dist/chunk-PHTWUTY7.js +300 -0
- package/dist/chunk-PHTWUTY7.js.map +1 -0
- package/dist/chunk-PZ5AY32C.js +9 -0
- package/dist/{chunk-4C666HHU.js.map → chunk-PZ5AY32C.js.map} +1 -1
- package/dist/chunk-QLCOEP5J.js +68 -0
- package/dist/chunk-QLCOEP5J.js.map +1 -0
- package/dist/chunk-RDWCGZ2A.js +87 -0
- package/dist/chunk-RDWCGZ2A.js.map +1 -0
- package/dist/chunk-RWRRBYG4.js +1 -0
- package/dist/client.d.ts +2 -2
- package/dist/client.js +9 -1
- package/dist/gateway.d.ts +2 -2
- package/dist/gateway.js +10 -1
- package/dist/{index-BcuTJtQX.d.ts → index-BlGc0iym.d.ts} +22 -1
- package/dist/{index-Brfvxmdt.d.ts → index-qM8ZH8sC.d.ts} +1 -1
- package/dist/index.d.ts +5 -5
- package/dist/index.js +60 -4
- package/dist/index.js.map +1 -1
- package/dist/kyber-FCVPX6CE.js +4 -0
- package/dist/{kyber-NONMBQNH.js.map → kyber-FCVPX6CE.js.map} +1 -1
- package/dist/mesh.js +5 -1
- package/dist/server.d.ts +2 -2
- package/dist/server.js +8 -1
- package/dist/{types-DzEXgi4s.d.ts → types-sKeUxuky.d.ts} +4 -46
- package/dist/types.d.ts +1 -1
- package/dist/types.js +4 -1
- package/dist/verifier-GCZDNZK7.js +6 -0
- package/dist/{verifier-XU2DB56Z.js.map → verifier-GCZDNZK7.js.map} +1 -1
- package/dist/workers/logic-execution.js +256 -1
- package/dist/workers/logic-execution.js.map +1 -1
- package/dist/workers/zk-verifier.js +174 -1
- package/dist/workers/zk-verifier.js.map +1 -1
- package/package.json +43 -44
- package/dist/chunk-2MGFSIXN.js +0 -2
- package/dist/chunk-2MGFSIXN.js.map +0 -1
- package/dist/chunk-4C666HHU.js +0 -2
- package/dist/chunk-7L5ODML2.js +0 -3
- package/dist/chunk-7L5ODML2.js.map +0 -1
- package/dist/chunk-ANFXJGMP.js +0 -2
- package/dist/chunk-ANFXJGMP.js.map +0 -1
- package/dist/chunk-DBXGYHKY.js +0 -2
- package/dist/chunk-DBXGYHKY.js.map +0 -1
- package/dist/chunk-DQ6UW6L7.js +0 -2
- package/dist/chunk-DQ6UW6L7.js.map +0 -1
- package/dist/chunk-GI2LSJYZ.js +0 -13
- package/dist/chunk-GI2LSJYZ.js.map +0 -1
- package/dist/chunk-I46YEWND.js +0 -33
- package/dist/chunk-I46YEWND.js.map +0 -1
- package/dist/chunk-KQ5BDO2M.js +0 -54
- package/dist/chunk-KQ5BDO2M.js.map +0 -1
- package/dist/chunk-PWCXZWSE.js +0 -2
- package/dist/chunk-PWCXZWSE.js.map +0 -1
- package/dist/chunk-RYYRR4N5.js +0 -31
- package/dist/chunk-RYYRR4N5.js.map +0 -1
- package/dist/chunk-S6RJHZV2.js +0 -2
- package/dist/chunk-S6RJHZV2.js.map +0 -1
- package/dist/chunk-SB5XJXKV.js +0 -2
- package/dist/chunk-SB5XJXKV.js.map +0 -1
- package/dist/chunk-T3L6OCM3.js +0 -3
- package/dist/chunk-T3L6OCM3.js.map +0 -1
- package/dist/chunk-TYVG6TXQ.js +0 -2
- package/dist/chunk-TYVG6TXQ.js.map +0 -1
- package/dist/chunk-V5MKJT6S.js +0 -2
- package/dist/chunk-V5MKJT6S.js.map +0 -1
- package/dist/kyber-NONMBQNH.js +0 -2
- package/dist/verifier-XU2DB56Z.js +0 -2
|
@@ -0,0 +1,1976 @@
|
|
|
1
|
+
import { LiopVerifier } from './chunk-32ADSAJS.js';
|
|
2
|
+
import { Kyber768Wrapper } from './chunk-QLCOEP5J.js';
|
|
3
|
+
import { authorizeRequest } from './chunk-IJHTRIZC.js';
|
|
4
|
+
import { liopV1, createChannelCredentials } from './chunk-RDWCGZ2A.js';
|
|
5
|
+
import { log } from './chunk-72MNYFR6.js';
|
|
6
|
+
import * as crypto2 from 'crypto';
|
|
7
|
+
import * as grpc from '@grpc/grpc-js';
|
|
8
|
+
|
|
9
|
+
// src/economy/estimator.ts
|
|
10
|
+
var RealTokenEstimator = class {
|
|
11
|
+
name = "o200k_base";
|
|
12
|
+
countFn;
|
|
13
|
+
constructor(countFn, setMergeCacheSizeFn) {
|
|
14
|
+
this.countFn = countFn;
|
|
15
|
+
if (setMergeCacheSizeFn) {
|
|
16
|
+
setMergeCacheSizeFn(1e4);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
countTokens(text) {
|
|
20
|
+
if (text.length === 0) return 0;
|
|
21
|
+
return this.countFn(text);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
var HeuristicTokenEstimator = class {
|
|
25
|
+
name = "heuristic (chars/4)";
|
|
26
|
+
countTokens(text) {
|
|
27
|
+
if (text.length === 0) return 0;
|
|
28
|
+
return Math.ceil(text.length / 4);
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
async function createTokenEstimator() {
|
|
32
|
+
try {
|
|
33
|
+
const mod = await import('gpt-tokenizer');
|
|
34
|
+
const estimator = new RealTokenEstimator(
|
|
35
|
+
mod.countTokens,
|
|
36
|
+
mod.setMergeCacheSize
|
|
37
|
+
);
|
|
38
|
+
log.debug("[LIOP-Economy] Token estimator initialized: o200k_base");
|
|
39
|
+
return estimator;
|
|
40
|
+
} catch {
|
|
41
|
+
log.info(
|
|
42
|
+
"[LIOP-Economy] gpt-tokenizer unavailable, falling back to heuristic estimator"
|
|
43
|
+
);
|
|
44
|
+
return new HeuristicTokenEstimator();
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
function createSyncTokenEstimator() {
|
|
48
|
+
return new HeuristicTokenEstimator();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// ../../node_modules/.pnpm/@opentelemetry+api@1.9.1/node_modules/@opentelemetry/api/build/esm/version.js
|
|
52
|
+
var VERSION = "1.9.1";
|
|
53
|
+
|
|
54
|
+
// ../../node_modules/.pnpm/@opentelemetry+api@1.9.1/node_modules/@opentelemetry/api/build/esm/internal/semver.js
|
|
55
|
+
var re = /^(\d+)\.(\d+)\.(\d+)(-(.+))?$/;
|
|
56
|
+
function _makeCompatibilityCheck(ownVersion) {
|
|
57
|
+
const acceptedVersions = /* @__PURE__ */ new Set([ownVersion]);
|
|
58
|
+
const rejectedVersions = /* @__PURE__ */ new Set();
|
|
59
|
+
const myVersionMatch = ownVersion.match(re);
|
|
60
|
+
if (!myVersionMatch) {
|
|
61
|
+
return () => false;
|
|
62
|
+
}
|
|
63
|
+
const ownVersionParsed = {
|
|
64
|
+
major: +myVersionMatch[1],
|
|
65
|
+
minor: +myVersionMatch[2],
|
|
66
|
+
patch: +myVersionMatch[3],
|
|
67
|
+
prerelease: myVersionMatch[4]
|
|
68
|
+
};
|
|
69
|
+
if (ownVersionParsed.prerelease != null) {
|
|
70
|
+
return function isExactmatch(globalVersion) {
|
|
71
|
+
return globalVersion === ownVersion;
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
function _reject(v) {
|
|
75
|
+
rejectedVersions.add(v);
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
function _accept(v) {
|
|
79
|
+
acceptedVersions.add(v);
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
82
|
+
return function isCompatible2(globalVersion) {
|
|
83
|
+
if (acceptedVersions.has(globalVersion)) {
|
|
84
|
+
return true;
|
|
85
|
+
}
|
|
86
|
+
if (rejectedVersions.has(globalVersion)) {
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
const globalVersionMatch = globalVersion.match(re);
|
|
90
|
+
if (!globalVersionMatch) {
|
|
91
|
+
return _reject(globalVersion);
|
|
92
|
+
}
|
|
93
|
+
const globalVersionParsed = {
|
|
94
|
+
major: +globalVersionMatch[1],
|
|
95
|
+
minor: +globalVersionMatch[2],
|
|
96
|
+
patch: +globalVersionMatch[3],
|
|
97
|
+
prerelease: globalVersionMatch[4]
|
|
98
|
+
};
|
|
99
|
+
if (globalVersionParsed.prerelease != null) {
|
|
100
|
+
return _reject(globalVersion);
|
|
101
|
+
}
|
|
102
|
+
if (ownVersionParsed.major !== globalVersionParsed.major) {
|
|
103
|
+
return _reject(globalVersion);
|
|
104
|
+
}
|
|
105
|
+
if (ownVersionParsed.major === 0) {
|
|
106
|
+
if (ownVersionParsed.minor === globalVersionParsed.minor && ownVersionParsed.patch <= globalVersionParsed.patch) {
|
|
107
|
+
return _accept(globalVersion);
|
|
108
|
+
}
|
|
109
|
+
return _reject(globalVersion);
|
|
110
|
+
}
|
|
111
|
+
if (ownVersionParsed.minor <= globalVersionParsed.minor) {
|
|
112
|
+
return _accept(globalVersion);
|
|
113
|
+
}
|
|
114
|
+
return _reject(globalVersion);
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
var isCompatible = _makeCompatibilityCheck(VERSION);
|
|
118
|
+
|
|
119
|
+
// ../../node_modules/.pnpm/@opentelemetry+api@1.9.1/node_modules/@opentelemetry/api/build/esm/internal/global-utils.js
|
|
120
|
+
var major = VERSION.split(".")[0];
|
|
121
|
+
var GLOBAL_OPENTELEMETRY_API_KEY = /* @__PURE__ */ Symbol.for(`opentelemetry.js.api.${major}`);
|
|
122
|
+
var _global = typeof globalThis === "object" ? globalThis : typeof self === "object" ? self : typeof window === "object" ? window : typeof global === "object" ? global : {};
|
|
123
|
+
function registerGlobal(type, instance, diag, allowOverride = false) {
|
|
124
|
+
var _a;
|
|
125
|
+
const api = _global[GLOBAL_OPENTELEMETRY_API_KEY] = (_a = _global[GLOBAL_OPENTELEMETRY_API_KEY]) !== null && _a !== void 0 ? _a : {
|
|
126
|
+
version: VERSION
|
|
127
|
+
};
|
|
128
|
+
if (!allowOverride && api[type]) {
|
|
129
|
+
const err = new Error(`@opentelemetry/api: Attempted duplicate registration of API: ${type}`);
|
|
130
|
+
diag.error(err.stack || err.message);
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
if (api.version !== VERSION) {
|
|
134
|
+
const err = new Error(`@opentelemetry/api: Registration of version v${api.version} for ${type} does not match previously registered API v${VERSION}`);
|
|
135
|
+
diag.error(err.stack || err.message);
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
api[type] = instance;
|
|
139
|
+
diag.debug(`@opentelemetry/api: Registered a global for ${type} v${VERSION}.`);
|
|
140
|
+
return true;
|
|
141
|
+
}
|
|
142
|
+
function getGlobal(type) {
|
|
143
|
+
var _a, _b;
|
|
144
|
+
const globalVersion = (_a = _global[GLOBAL_OPENTELEMETRY_API_KEY]) === null || _a === void 0 ? void 0 : _a.version;
|
|
145
|
+
if (!globalVersion || !isCompatible(globalVersion)) {
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
return (_b = _global[GLOBAL_OPENTELEMETRY_API_KEY]) === null || _b === void 0 ? void 0 : _b[type];
|
|
149
|
+
}
|
|
150
|
+
function unregisterGlobal(type, diag) {
|
|
151
|
+
diag.debug(`@opentelemetry/api: Unregistering a global for ${type} v${VERSION}.`);
|
|
152
|
+
const api = _global[GLOBAL_OPENTELEMETRY_API_KEY];
|
|
153
|
+
if (api) {
|
|
154
|
+
delete api[type];
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// ../../node_modules/.pnpm/@opentelemetry+api@1.9.1/node_modules/@opentelemetry/api/build/esm/diag/ComponentLogger.js
|
|
159
|
+
var DiagComponentLogger = class {
|
|
160
|
+
constructor(props) {
|
|
161
|
+
this._namespace = props.namespace || "DiagComponentLogger";
|
|
162
|
+
}
|
|
163
|
+
debug(...args) {
|
|
164
|
+
return logProxy("debug", this._namespace, args);
|
|
165
|
+
}
|
|
166
|
+
error(...args) {
|
|
167
|
+
return logProxy("error", this._namespace, args);
|
|
168
|
+
}
|
|
169
|
+
info(...args) {
|
|
170
|
+
return logProxy("info", this._namespace, args);
|
|
171
|
+
}
|
|
172
|
+
warn(...args) {
|
|
173
|
+
return logProxy("warn", this._namespace, args);
|
|
174
|
+
}
|
|
175
|
+
verbose(...args) {
|
|
176
|
+
return logProxy("verbose", this._namespace, args);
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
function logProxy(funcName, namespace, args) {
|
|
180
|
+
const logger = getGlobal("diag");
|
|
181
|
+
if (!logger) {
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
return logger[funcName](namespace, ...args);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// ../../node_modules/.pnpm/@opentelemetry+api@1.9.1/node_modules/@opentelemetry/api/build/esm/diag/types.js
|
|
188
|
+
var DiagLogLevel;
|
|
189
|
+
(function(DiagLogLevel2) {
|
|
190
|
+
DiagLogLevel2[DiagLogLevel2["NONE"] = 0] = "NONE";
|
|
191
|
+
DiagLogLevel2[DiagLogLevel2["ERROR"] = 30] = "ERROR";
|
|
192
|
+
DiagLogLevel2[DiagLogLevel2["WARN"] = 50] = "WARN";
|
|
193
|
+
DiagLogLevel2[DiagLogLevel2["INFO"] = 60] = "INFO";
|
|
194
|
+
DiagLogLevel2[DiagLogLevel2["DEBUG"] = 70] = "DEBUG";
|
|
195
|
+
DiagLogLevel2[DiagLogLevel2["VERBOSE"] = 80] = "VERBOSE";
|
|
196
|
+
DiagLogLevel2[DiagLogLevel2["ALL"] = 9999] = "ALL";
|
|
197
|
+
})(DiagLogLevel || (DiagLogLevel = {}));
|
|
198
|
+
|
|
199
|
+
// ../../node_modules/.pnpm/@opentelemetry+api@1.9.1/node_modules/@opentelemetry/api/build/esm/diag/internal/logLevelLogger.js
|
|
200
|
+
function createLogLevelDiagLogger(maxLevel, logger) {
|
|
201
|
+
if (maxLevel < DiagLogLevel.NONE) {
|
|
202
|
+
maxLevel = DiagLogLevel.NONE;
|
|
203
|
+
} else if (maxLevel > DiagLogLevel.ALL) {
|
|
204
|
+
maxLevel = DiagLogLevel.ALL;
|
|
205
|
+
}
|
|
206
|
+
logger = logger || {};
|
|
207
|
+
function _filterFunc(funcName, theLevel) {
|
|
208
|
+
const theFunc = logger[funcName];
|
|
209
|
+
if (typeof theFunc === "function" && maxLevel >= theLevel) {
|
|
210
|
+
return theFunc.bind(logger);
|
|
211
|
+
}
|
|
212
|
+
return function() {
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
return {
|
|
216
|
+
error: _filterFunc("error", DiagLogLevel.ERROR),
|
|
217
|
+
warn: _filterFunc("warn", DiagLogLevel.WARN),
|
|
218
|
+
info: _filterFunc("info", DiagLogLevel.INFO),
|
|
219
|
+
debug: _filterFunc("debug", DiagLogLevel.DEBUG),
|
|
220
|
+
verbose: _filterFunc("verbose", DiagLogLevel.VERBOSE)
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// ../../node_modules/.pnpm/@opentelemetry+api@1.9.1/node_modules/@opentelemetry/api/build/esm/api/diag.js
|
|
225
|
+
var API_NAME = "diag";
|
|
226
|
+
var DiagAPI = class _DiagAPI {
|
|
227
|
+
/** Get the singleton instance of the DiagAPI API */
|
|
228
|
+
static instance() {
|
|
229
|
+
if (!this._instance) {
|
|
230
|
+
this._instance = new _DiagAPI();
|
|
231
|
+
}
|
|
232
|
+
return this._instance;
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Private internal constructor
|
|
236
|
+
* @private
|
|
237
|
+
*/
|
|
238
|
+
constructor() {
|
|
239
|
+
function _logProxy(funcName) {
|
|
240
|
+
return function(...args) {
|
|
241
|
+
const logger = getGlobal("diag");
|
|
242
|
+
if (!logger)
|
|
243
|
+
return;
|
|
244
|
+
return logger[funcName](...args);
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
const self2 = this;
|
|
248
|
+
const setLogger = (logger, optionsOrLogLevel = { logLevel: DiagLogLevel.INFO }) => {
|
|
249
|
+
var _a, _b, _c;
|
|
250
|
+
if (logger === self2) {
|
|
251
|
+
const err = new Error("Cannot use diag as the logger for itself. Please use a DiagLogger implementation like ConsoleDiagLogger or a custom implementation");
|
|
252
|
+
self2.error((_a = err.stack) !== null && _a !== void 0 ? _a : err.message);
|
|
253
|
+
return false;
|
|
254
|
+
}
|
|
255
|
+
if (typeof optionsOrLogLevel === "number") {
|
|
256
|
+
optionsOrLogLevel = {
|
|
257
|
+
logLevel: optionsOrLogLevel
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
const oldLogger = getGlobal("diag");
|
|
261
|
+
const newLogger = createLogLevelDiagLogger((_b = optionsOrLogLevel.logLevel) !== null && _b !== void 0 ? _b : DiagLogLevel.INFO, logger);
|
|
262
|
+
if (oldLogger && !optionsOrLogLevel.suppressOverrideMessage) {
|
|
263
|
+
const stack = (_c = new Error().stack) !== null && _c !== void 0 ? _c : "<failed to generate stacktrace>";
|
|
264
|
+
oldLogger.warn(`Current logger will be overwritten from ${stack}`);
|
|
265
|
+
newLogger.warn(`Current logger will overwrite one already registered from ${stack}`);
|
|
266
|
+
}
|
|
267
|
+
return registerGlobal("diag", newLogger, self2, true);
|
|
268
|
+
};
|
|
269
|
+
self2.setLogger = setLogger;
|
|
270
|
+
self2.disable = () => {
|
|
271
|
+
unregisterGlobal(API_NAME, self2);
|
|
272
|
+
};
|
|
273
|
+
self2.createComponentLogger = (options) => {
|
|
274
|
+
return new DiagComponentLogger(options);
|
|
275
|
+
};
|
|
276
|
+
self2.verbose = _logProxy("verbose");
|
|
277
|
+
self2.debug = _logProxy("debug");
|
|
278
|
+
self2.info = _logProxy("info");
|
|
279
|
+
self2.warn = _logProxy("warn");
|
|
280
|
+
self2.error = _logProxy("error");
|
|
281
|
+
}
|
|
282
|
+
};
|
|
283
|
+
|
|
284
|
+
// ../../node_modules/.pnpm/@opentelemetry+api@1.9.1/node_modules/@opentelemetry/api/build/esm/metrics/NoopMeter.js
|
|
285
|
+
var NoopMeter = class {
|
|
286
|
+
constructor() {
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* @see {@link Meter.createGauge}
|
|
290
|
+
*/
|
|
291
|
+
createGauge(_name, _options) {
|
|
292
|
+
return NOOP_GAUGE_METRIC;
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* @see {@link Meter.createHistogram}
|
|
296
|
+
*/
|
|
297
|
+
createHistogram(_name, _options) {
|
|
298
|
+
return NOOP_HISTOGRAM_METRIC;
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* @see {@link Meter.createCounter}
|
|
302
|
+
*/
|
|
303
|
+
createCounter(_name, _options) {
|
|
304
|
+
return NOOP_COUNTER_METRIC;
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* @see {@link Meter.createUpDownCounter}
|
|
308
|
+
*/
|
|
309
|
+
createUpDownCounter(_name, _options) {
|
|
310
|
+
return NOOP_UP_DOWN_COUNTER_METRIC;
|
|
311
|
+
}
|
|
312
|
+
/**
|
|
313
|
+
* @see {@link Meter.createObservableGauge}
|
|
314
|
+
*/
|
|
315
|
+
createObservableGauge(_name, _options) {
|
|
316
|
+
return NOOP_OBSERVABLE_GAUGE_METRIC;
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* @see {@link Meter.createObservableCounter}
|
|
320
|
+
*/
|
|
321
|
+
createObservableCounter(_name, _options) {
|
|
322
|
+
return NOOP_OBSERVABLE_COUNTER_METRIC;
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* @see {@link Meter.createObservableUpDownCounter}
|
|
326
|
+
*/
|
|
327
|
+
createObservableUpDownCounter(_name, _options) {
|
|
328
|
+
return NOOP_OBSERVABLE_UP_DOWN_COUNTER_METRIC;
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* @see {@link Meter.addBatchObservableCallback}
|
|
332
|
+
*/
|
|
333
|
+
addBatchObservableCallback(_callback, _observables) {
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* @see {@link Meter.removeBatchObservableCallback}
|
|
337
|
+
*/
|
|
338
|
+
removeBatchObservableCallback(_callback) {
|
|
339
|
+
}
|
|
340
|
+
};
|
|
341
|
+
var NoopMetric = class {
|
|
342
|
+
};
|
|
343
|
+
var NoopCounterMetric = class extends NoopMetric {
|
|
344
|
+
add(_value, _attributes) {
|
|
345
|
+
}
|
|
346
|
+
};
|
|
347
|
+
var NoopUpDownCounterMetric = class extends NoopMetric {
|
|
348
|
+
add(_value, _attributes) {
|
|
349
|
+
}
|
|
350
|
+
};
|
|
351
|
+
var NoopGaugeMetric = class extends NoopMetric {
|
|
352
|
+
record(_value, _attributes) {
|
|
353
|
+
}
|
|
354
|
+
};
|
|
355
|
+
var NoopHistogramMetric = class extends NoopMetric {
|
|
356
|
+
record(_value, _attributes) {
|
|
357
|
+
}
|
|
358
|
+
};
|
|
359
|
+
var NoopObservableMetric = class {
|
|
360
|
+
addCallback(_callback) {
|
|
361
|
+
}
|
|
362
|
+
removeCallback(_callback) {
|
|
363
|
+
}
|
|
364
|
+
};
|
|
365
|
+
var NoopObservableCounterMetric = class extends NoopObservableMetric {
|
|
366
|
+
};
|
|
367
|
+
var NoopObservableGaugeMetric = class extends NoopObservableMetric {
|
|
368
|
+
};
|
|
369
|
+
var NoopObservableUpDownCounterMetric = class extends NoopObservableMetric {
|
|
370
|
+
};
|
|
371
|
+
var NOOP_METER = new NoopMeter();
|
|
372
|
+
var NOOP_COUNTER_METRIC = new NoopCounterMetric();
|
|
373
|
+
var NOOP_GAUGE_METRIC = new NoopGaugeMetric();
|
|
374
|
+
var NOOP_HISTOGRAM_METRIC = new NoopHistogramMetric();
|
|
375
|
+
var NOOP_UP_DOWN_COUNTER_METRIC = new NoopUpDownCounterMetric();
|
|
376
|
+
var NOOP_OBSERVABLE_COUNTER_METRIC = new NoopObservableCounterMetric();
|
|
377
|
+
var NOOP_OBSERVABLE_GAUGE_METRIC = new NoopObservableGaugeMetric();
|
|
378
|
+
var NOOP_OBSERVABLE_UP_DOWN_COUNTER_METRIC = new NoopObservableUpDownCounterMetric();
|
|
379
|
+
|
|
380
|
+
// ../../node_modules/.pnpm/@opentelemetry+api@1.9.1/node_modules/@opentelemetry/api/build/esm/metrics/NoopMeterProvider.js
|
|
381
|
+
var NoopMeterProvider = class {
|
|
382
|
+
getMeter(_name, _version, _options) {
|
|
383
|
+
return NOOP_METER;
|
|
384
|
+
}
|
|
385
|
+
};
|
|
386
|
+
var NOOP_METER_PROVIDER = new NoopMeterProvider();
|
|
387
|
+
|
|
388
|
+
// ../../node_modules/.pnpm/@opentelemetry+api@1.9.1/node_modules/@opentelemetry/api/build/esm/api/metrics.js
|
|
389
|
+
var API_NAME2 = "metrics";
|
|
390
|
+
var MetricsAPI = class _MetricsAPI {
|
|
391
|
+
/** Empty private constructor prevents end users from constructing a new instance of the API */
|
|
392
|
+
constructor() {
|
|
393
|
+
}
|
|
394
|
+
/** Get the singleton instance of the Metrics API */
|
|
395
|
+
static getInstance() {
|
|
396
|
+
if (!this._instance) {
|
|
397
|
+
this._instance = new _MetricsAPI();
|
|
398
|
+
}
|
|
399
|
+
return this._instance;
|
|
400
|
+
}
|
|
401
|
+
/**
|
|
402
|
+
* Set the current global meter provider.
|
|
403
|
+
* Returns true if the meter provider was successfully registered, else false.
|
|
404
|
+
*/
|
|
405
|
+
setGlobalMeterProvider(provider) {
|
|
406
|
+
return registerGlobal(API_NAME2, provider, DiagAPI.instance());
|
|
407
|
+
}
|
|
408
|
+
/**
|
|
409
|
+
* Returns the global meter provider.
|
|
410
|
+
*/
|
|
411
|
+
getMeterProvider() {
|
|
412
|
+
return getGlobal(API_NAME2) || NOOP_METER_PROVIDER;
|
|
413
|
+
}
|
|
414
|
+
/**
|
|
415
|
+
* Returns a meter from the global meter provider.
|
|
416
|
+
*/
|
|
417
|
+
getMeter(name, version, options) {
|
|
418
|
+
return this.getMeterProvider().getMeter(name, version, options);
|
|
419
|
+
}
|
|
420
|
+
/** Remove the global meter provider */
|
|
421
|
+
disable() {
|
|
422
|
+
unregisterGlobal(API_NAME2, DiagAPI.instance());
|
|
423
|
+
}
|
|
424
|
+
};
|
|
425
|
+
|
|
426
|
+
// ../../node_modules/.pnpm/@opentelemetry+api@1.9.1/node_modules/@opentelemetry/api/build/esm/metrics-api.js
|
|
427
|
+
var metrics = MetricsAPI.getInstance();
|
|
428
|
+
|
|
429
|
+
// src/economy/otel.ts
|
|
430
|
+
var METER_NAME = "@nekzus/liop";
|
|
431
|
+
var METER_VERSION = "1.2.0-alpha.9";
|
|
432
|
+
var TOKEN_USAGE_BUCKETS = [
|
|
433
|
+
1,
|
|
434
|
+
4,
|
|
435
|
+
16,
|
|
436
|
+
64,
|
|
437
|
+
256,
|
|
438
|
+
1024,
|
|
439
|
+
4096,
|
|
440
|
+
16384,
|
|
441
|
+
65536,
|
|
442
|
+
262144,
|
|
443
|
+
1048576,
|
|
444
|
+
4194304,
|
|
445
|
+
16777216,
|
|
446
|
+
67108864
|
|
447
|
+
];
|
|
448
|
+
var DURATION_BUCKETS = [
|
|
449
|
+
0.01,
|
|
450
|
+
0.02,
|
|
451
|
+
0.04,
|
|
452
|
+
0.08,
|
|
453
|
+
0.16,
|
|
454
|
+
0.32,
|
|
455
|
+
0.64,
|
|
456
|
+
1.28,
|
|
457
|
+
2.56,
|
|
458
|
+
5.12,
|
|
459
|
+
10.24,
|
|
460
|
+
20.48,
|
|
461
|
+
40.96,
|
|
462
|
+
81.92
|
|
463
|
+
];
|
|
464
|
+
var LiopOTelBridge = class {
|
|
465
|
+
tokenUsage;
|
|
466
|
+
operationDuration;
|
|
467
|
+
active = false;
|
|
468
|
+
constructor() {
|
|
469
|
+
try {
|
|
470
|
+
const meter = metrics.getMeter(METER_NAME, METER_VERSION);
|
|
471
|
+
this.tokenUsage = meter.createHistogram("gen_ai.client.token.usage", {
|
|
472
|
+
description: "Number of tokens used in LIOP Logic-on-Origin operations",
|
|
473
|
+
unit: "{token}",
|
|
474
|
+
advice: { explicitBucketBoundaries: TOKEN_USAGE_BUCKETS }
|
|
475
|
+
});
|
|
476
|
+
this.operationDuration = meter.createHistogram(
|
|
477
|
+
"gen_ai.client.operation.duration",
|
|
478
|
+
{
|
|
479
|
+
description: "Duration of LIOP operations",
|
|
480
|
+
unit: "s",
|
|
481
|
+
advice: { explicitBucketBoundaries: DURATION_BUCKETS }
|
|
482
|
+
}
|
|
483
|
+
);
|
|
484
|
+
this.active = true;
|
|
485
|
+
log.debug("[LIOP-OTel] gen_ai.* metrics bridge initialized");
|
|
486
|
+
} catch (err) {
|
|
487
|
+
log.debug(
|
|
488
|
+
`[LIOP-OTel] Bridge disabled: ${err instanceof Error ? err.message : String(err)}`
|
|
489
|
+
);
|
|
490
|
+
const noopHistogram = {
|
|
491
|
+
record: () => {
|
|
492
|
+
}
|
|
493
|
+
};
|
|
494
|
+
this.tokenUsage = noopHistogram;
|
|
495
|
+
this.operationDuration = noopHistogram;
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
/**
|
|
499
|
+
* Record token usage with gen_ai.* standard attributes.
|
|
500
|
+
*
|
|
501
|
+
* @param tokens - Number of tokens consumed
|
|
502
|
+
* @param tokenType - "input" or "output" (gen_ai.token.type)
|
|
503
|
+
* @param operationName - gen_ai.operation.name (e.g., "execute_tool", "chat")
|
|
504
|
+
* @param toolName - Optional LIOP-specific tool name for attribution
|
|
505
|
+
*/
|
|
506
|
+
recordTokens(tokens, tokenType, operationName, toolName) {
|
|
507
|
+
this.tokenUsage.record(tokens, {
|
|
508
|
+
"gen_ai.system": "liop",
|
|
509
|
+
"gen_ai.operation.name": operationName,
|
|
510
|
+
"gen_ai.token.type": tokenType,
|
|
511
|
+
"gen_ai.request.model": "liop-mesh",
|
|
512
|
+
...toolName ? { "liop.tool.name": toolName } : {}
|
|
513
|
+
});
|
|
514
|
+
}
|
|
515
|
+
/**
|
|
516
|
+
* Record operation duration with gen_ai.* standard attributes.
|
|
517
|
+
*
|
|
518
|
+
* @param durationMs - Duration in milliseconds (converted to seconds for OTel)
|
|
519
|
+
* @param operationName - gen_ai.operation.name
|
|
520
|
+
* @param error - Optional error type string if the operation failed
|
|
521
|
+
*/
|
|
522
|
+
recordDuration(durationMs, operationName, error) {
|
|
523
|
+
this.operationDuration.record(durationMs / 1e3, {
|
|
524
|
+
"gen_ai.system": "liop",
|
|
525
|
+
"gen_ai.operation.name": operationName,
|
|
526
|
+
...error ? { "error.type": error } : {}
|
|
527
|
+
});
|
|
528
|
+
}
|
|
529
|
+
/** Whether the OTel bridge is actively connected to a MeterProvider */
|
|
530
|
+
isActive() {
|
|
531
|
+
return this.active;
|
|
532
|
+
}
|
|
533
|
+
};
|
|
534
|
+
|
|
535
|
+
// src/economy/telemetry.ts
|
|
536
|
+
var OTEL_OPERATION_MAP = {
|
|
537
|
+
tools_list: "chat",
|
|
538
|
+
tool_call: "execute_tool",
|
|
539
|
+
resource_read: "chat",
|
|
540
|
+
resource_list: "chat",
|
|
541
|
+
prompt_get: "chat",
|
|
542
|
+
prompt_list: "chat",
|
|
543
|
+
diagnostic: "chat"
|
|
544
|
+
};
|
|
545
|
+
var TokenTelemetryEngine = class _TokenTelemetryEngine {
|
|
546
|
+
static instance = null;
|
|
547
|
+
operations = [];
|
|
548
|
+
sessionId;
|
|
549
|
+
startedAt;
|
|
550
|
+
estimator;
|
|
551
|
+
otelBridge;
|
|
552
|
+
constructor() {
|
|
553
|
+
this.sessionId = crypto.randomUUID();
|
|
554
|
+
this.startedAt = Date.now();
|
|
555
|
+
this.estimator = createSyncTokenEstimator();
|
|
556
|
+
this.otelBridge = new LiopOTelBridge();
|
|
557
|
+
this.initRealEstimator();
|
|
558
|
+
}
|
|
559
|
+
/** Async upgrade from heuristic to real BPE tokenizer */
|
|
560
|
+
initRealEstimator() {
|
|
561
|
+
createTokenEstimator().then((real) => {
|
|
562
|
+
this.estimator = real;
|
|
563
|
+
}).catch(() => {
|
|
564
|
+
});
|
|
565
|
+
}
|
|
566
|
+
static getInstance() {
|
|
567
|
+
if (!_TokenTelemetryEngine.instance) {
|
|
568
|
+
_TokenTelemetryEngine.instance = new _TokenTelemetryEngine();
|
|
569
|
+
}
|
|
570
|
+
return _TokenTelemetryEngine.instance;
|
|
571
|
+
}
|
|
572
|
+
/**
|
|
573
|
+
* Count tokens in a string using the active estimator.
|
|
574
|
+
* Delegates to o200k_base BPE tokenizer (or heuristic fallback).
|
|
575
|
+
*/
|
|
576
|
+
countTokens(content) {
|
|
577
|
+
try {
|
|
578
|
+
return this.estimator.countTokens(content);
|
|
579
|
+
} catch {
|
|
580
|
+
return Math.ceil(content.length / 4);
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
/**
|
|
584
|
+
* Record a single MCP operation's token footprint.
|
|
585
|
+
* Emits both internal metrics and OTel gen_ai.* histograms.
|
|
586
|
+
*/
|
|
587
|
+
record(metric) {
|
|
588
|
+
const fullMetric = {
|
|
589
|
+
...metric,
|
|
590
|
+
timestamp: Date.now()
|
|
591
|
+
};
|
|
592
|
+
this.operations.push(fullMetric);
|
|
593
|
+
try {
|
|
594
|
+
const otelOp = OTEL_OPERATION_MAP[metric.type] || "chat";
|
|
595
|
+
if (metric.estimatedInputTokens > 0) {
|
|
596
|
+
this.otelBridge.recordTokens(
|
|
597
|
+
metric.estimatedInputTokens,
|
|
598
|
+
"input",
|
|
599
|
+
otelOp,
|
|
600
|
+
metric.toolName
|
|
601
|
+
);
|
|
602
|
+
}
|
|
603
|
+
if (metric.estimatedOutputTokens > 0) {
|
|
604
|
+
this.otelBridge.recordTokens(
|
|
605
|
+
metric.estimatedOutputTokens,
|
|
606
|
+
"output",
|
|
607
|
+
otelOp,
|
|
608
|
+
metric.toolName
|
|
609
|
+
);
|
|
610
|
+
}
|
|
611
|
+
if (metric.durationMs !== void 0) {
|
|
612
|
+
this.otelBridge.recordDuration(metric.durationMs, otelOp);
|
|
613
|
+
}
|
|
614
|
+
} catch {
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
/**
|
|
618
|
+
* @deprecated Use countTokens() instead. Kept for backward compatibility.
|
|
619
|
+
*/
|
|
620
|
+
estimateTokens(content) {
|
|
621
|
+
return this.countTokens(content);
|
|
622
|
+
}
|
|
623
|
+
/** Generate the full session report */
|
|
624
|
+
getReport() {
|
|
625
|
+
return {
|
|
626
|
+
sessionId: this.sessionId,
|
|
627
|
+
operations: [...this.operations],
|
|
628
|
+
totalInputTokens: this.operations.reduce(
|
|
629
|
+
(sum, op) => sum + op.estimatedInputTokens,
|
|
630
|
+
0
|
|
631
|
+
),
|
|
632
|
+
totalOutputTokens: this.operations.reduce(
|
|
633
|
+
(sum, op) => sum + op.estimatedOutputTokens,
|
|
634
|
+
0
|
|
635
|
+
),
|
|
636
|
+
estimatorName: this.estimator.name,
|
|
637
|
+
sessionUptimeMs: Date.now() - this.startedAt
|
|
638
|
+
};
|
|
639
|
+
}
|
|
640
|
+
/** Get per-tool token breakdown for diagnostic display */
|
|
641
|
+
getPerToolReport() {
|
|
642
|
+
const breakdown = /* @__PURE__ */ new Map();
|
|
643
|
+
for (const op of this.operations) {
|
|
644
|
+
const key = op.toolName || op.method;
|
|
645
|
+
const existing = breakdown.get(key) || {
|
|
646
|
+
input: 0,
|
|
647
|
+
output: 0,
|
|
648
|
+
calls: 0,
|
|
649
|
+
avgDurationMs: 0
|
|
650
|
+
};
|
|
651
|
+
const totalDuration = existing.avgDurationMs * existing.calls + (op.durationMs || 0);
|
|
652
|
+
const newCalls = existing.calls + 1;
|
|
653
|
+
breakdown.set(key, {
|
|
654
|
+
input: existing.input + op.estimatedInputTokens,
|
|
655
|
+
output: existing.output + op.estimatedOutputTokens,
|
|
656
|
+
calls: newCalls,
|
|
657
|
+
avgDurationMs: newCalls > 0 ? totalDuration / newCalls : 0
|
|
658
|
+
});
|
|
659
|
+
}
|
|
660
|
+
return breakdown;
|
|
661
|
+
}
|
|
662
|
+
/**
|
|
663
|
+
* Format a rich, human-readable summary block for LiopMeshStatus diagnostic.
|
|
664
|
+
* Returns empty string when no operations have been recorded.
|
|
665
|
+
*/
|
|
666
|
+
formatStatusBlock() {
|
|
667
|
+
const report = this.getReport();
|
|
668
|
+
if (report.operations.length === 0) return "";
|
|
669
|
+
const uptimeStr = this.formatUptime(report.sessionUptimeMs);
|
|
670
|
+
const totalCombined = report.totalInputTokens + report.totalOutputTokens;
|
|
671
|
+
const byType = /* @__PURE__ */ new Map();
|
|
672
|
+
for (const op of report.operations) {
|
|
673
|
+
const key = op.type;
|
|
674
|
+
const existing = byType.get(key) || {
|
|
675
|
+
count: 0,
|
|
676
|
+
input: 0,
|
|
677
|
+
output: 0
|
|
678
|
+
};
|
|
679
|
+
byType.set(key, {
|
|
680
|
+
count: existing.count + 1,
|
|
681
|
+
input: existing.input + op.estimatedInputTokens,
|
|
682
|
+
output: existing.output + op.estimatedOutputTokens
|
|
683
|
+
});
|
|
684
|
+
}
|
|
685
|
+
const typeEntries = Array.from(byType.entries());
|
|
686
|
+
const typeLines = typeEntries.map(([type, data], idx) => {
|
|
687
|
+
const prefix = idx === typeEntries.length - 1 ? "\u2502 \u2514\u2500" : "\u2502 \u251C\u2500";
|
|
688
|
+
const outputPart = data.output > 0 ? ` / ${data.output.toLocaleString()} out` : "";
|
|
689
|
+
return `${prefix} ${type} \xD7${data.count} \u2192 ${data.input.toLocaleString()} in${outputPart}`;
|
|
690
|
+
});
|
|
691
|
+
const toolReport = this.getPerToolReport();
|
|
692
|
+
const toolEntries = Array.from(toolReport.entries()).filter(
|
|
693
|
+
([key]) => key !== "tools/list" && key !== "LiopMeshStatus"
|
|
694
|
+
);
|
|
695
|
+
const toolLines = [];
|
|
696
|
+
if (toolEntries.length > 0) {
|
|
697
|
+
toolLines.push("\u251C\u2500 By Tool:");
|
|
698
|
+
toolEntries.forEach(([name, data], idx) => {
|
|
699
|
+
const prefix = idx === toolEntries.length - 1 ? "\u2502 \u2514\u2500" : "\u2502 \u251C\u2500";
|
|
700
|
+
const outputPart = data.output > 0 ? ` / ${data.output.toLocaleString()} out` : "";
|
|
701
|
+
const durationPart = data.avgDurationMs > 0 ? ` ~${Math.round(data.avgDurationMs)}ms` : "";
|
|
702
|
+
toolLines.push(
|
|
703
|
+
`${prefix} ${name}: ${data.input.toLocaleString()} in${outputPart} (\xD7${data.calls})${durationPart}`
|
|
704
|
+
);
|
|
705
|
+
});
|
|
706
|
+
}
|
|
707
|
+
const timedOps = report.operations.filter(
|
|
708
|
+
(op) => op.durationMs !== void 0
|
|
709
|
+
);
|
|
710
|
+
const avgLatency = timedOps.length > 0 ? Math.round(
|
|
711
|
+
timedOps.reduce((sum, op) => sum + (op.durationMs || 0), 0) / timedOps.length
|
|
712
|
+
) : 0;
|
|
713
|
+
const otelStatus = this.otelBridge.isActive() ? "gen_ai.client.token.usage \u2192 active" : "disabled";
|
|
714
|
+
const lines = [
|
|
715
|
+
"\nToken Economy:",
|
|
716
|
+
`\u251C\u2500 Session: ${report.sessionId.slice(0, 8)} (${uptimeStr})`,
|
|
717
|
+
`\u251C\u2500 Estimator: ${report.estimatorName}`,
|
|
718
|
+
`\u251C\u2500 Operations: ${report.operations.length}`,
|
|
719
|
+
...typeLines,
|
|
720
|
+
`\u251C\u2500 Total: ${report.totalInputTokens.toLocaleString()} in / ${report.totalOutputTokens.toLocaleString()} out (${totalCombined.toLocaleString()} combined)`,
|
|
721
|
+
...toolLines,
|
|
722
|
+
...avgLatency > 0 ? [`\u251C\u2500 Avg Latency: ${avgLatency}ms`] : [],
|
|
723
|
+
`\u2514\u2500 OTel: ${otelStatus}`
|
|
724
|
+
];
|
|
725
|
+
return lines.join("\n");
|
|
726
|
+
}
|
|
727
|
+
/** Format milliseconds into human-readable uptime string */
|
|
728
|
+
formatUptime(ms) {
|
|
729
|
+
const seconds = Math.floor(ms / 1e3);
|
|
730
|
+
if (seconds < 60) return `${seconds}s`;
|
|
731
|
+
const minutes = Math.floor(seconds / 60);
|
|
732
|
+
const remainingSeconds = seconds % 60;
|
|
733
|
+
if (minutes < 60) return `${minutes}m ${remainingSeconds}s`;
|
|
734
|
+
const hours = Math.floor(minutes / 60);
|
|
735
|
+
const remainingMinutes = minutes % 60;
|
|
736
|
+
return `${hours}h ${remainingMinutes}m`;
|
|
737
|
+
}
|
|
738
|
+
/** Reset all recorded metrics (used in tests) */
|
|
739
|
+
reset() {
|
|
740
|
+
this.operations = [];
|
|
741
|
+
}
|
|
742
|
+
/** Destroy the singleton (used in tests to guarantee isolation) */
|
|
743
|
+
static destroy() {
|
|
744
|
+
_TokenTelemetryEngine.instance = null;
|
|
745
|
+
}
|
|
746
|
+
};
|
|
747
|
+
|
|
748
|
+
// src/utils/mcpCompact.ts
|
|
749
|
+
function mcpCompactToolDescriptions() {
|
|
750
|
+
const v = process.env.LIOP_MCP_COMPACT_TOOL_DESCRIPTIONS?.toLowerCase().trim();
|
|
751
|
+
if (v === "0" || v === "false" || v === "no") return false;
|
|
752
|
+
return true;
|
|
753
|
+
}
|
|
754
|
+
function stripVerboseLiopToolDescription(description) {
|
|
755
|
+
let d = description;
|
|
756
|
+
const markers = [
|
|
757
|
+
"\n\n[LIOP-PROTO-V1:",
|
|
758
|
+
"\r\n\r\n[LIOP-PROTO-V1:",
|
|
759
|
+
"\n[LIOP-PROTO-V1:"
|
|
760
|
+
// rare
|
|
761
|
+
];
|
|
762
|
+
for (const m of markers) {
|
|
763
|
+
const i = d.indexOf(m);
|
|
764
|
+
if (i !== -1) {
|
|
765
|
+
d = d.slice(0, i);
|
|
766
|
+
break;
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
return d.trimEnd();
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
// src/gateway/router.ts
|
|
773
|
+
var MANIFEST_CACHE_TTL_S = 300;
|
|
774
|
+
var MANIFEST_DISCOVERY_RETRIES = 5;
|
|
775
|
+
var LiopMcpRouter = class _LiopMcpRouter {
|
|
776
|
+
constructor(liopServer, meshNode = null, defaultRpcPort = 50051) {
|
|
777
|
+
this.liopServer = liopServer;
|
|
778
|
+
this.meshNode = meshNode;
|
|
779
|
+
this.defaultRpcPort = defaultRpcPort;
|
|
780
|
+
if (this.meshNode) {
|
|
781
|
+
this.meshNode.registerManifestHandler(() => {
|
|
782
|
+
const remoteTools = this.liopServer.listTools().map((t) => ({
|
|
783
|
+
name: t.name,
|
|
784
|
+
description: t.description,
|
|
785
|
+
inputSchema: t.inputSchema
|
|
786
|
+
}));
|
|
787
|
+
const resources = this.liopServer.listResources().map((r) => ({
|
|
788
|
+
name: r.name,
|
|
789
|
+
uri: r.uri,
|
|
790
|
+
description: r.description,
|
|
791
|
+
mimeType: r.mimeType
|
|
792
|
+
}));
|
|
793
|
+
const serverConfig = this.liopServer.config;
|
|
794
|
+
return {
|
|
795
|
+
peerId: this.meshNode?.getPeerId() || "unknown",
|
|
796
|
+
grpcPort: this.defaultRpcPort,
|
|
797
|
+
tools: [...remoteTools],
|
|
798
|
+
resources,
|
|
799
|
+
serverInfo: this.liopServer.getServerInfo(),
|
|
800
|
+
authRequired: this.liopServer.jwtValidator !== void 0,
|
|
801
|
+
tokenSlug: serverConfig?.tokenSlug,
|
|
802
|
+
taxonomy: serverConfig?.taxonomy ? {
|
|
803
|
+
domain: serverConfig.taxonomy.domain || "Unknown Domain",
|
|
804
|
+
clearanceTier: serverConfig.taxonomy.clearanceTier ?? 0,
|
|
805
|
+
executionTypes: serverConfig.taxonomy.executionTypes || []
|
|
806
|
+
} : void 0
|
|
807
|
+
};
|
|
808
|
+
});
|
|
809
|
+
this.meshNode.announceManifest().catch((err) => {
|
|
810
|
+
log.info(
|
|
811
|
+
`[LIOP-Router] Failed to announce manifest: ${err instanceof Error ? err.message : String(err)}`
|
|
812
|
+
);
|
|
813
|
+
});
|
|
814
|
+
}
|
|
815
|
+
if (process.env.LIOP_DIAGNOSTIC_LEVEL === "full") {
|
|
816
|
+
process.stderr.write(
|
|
817
|
+
"\u26A0\uFE0F [LIOP-Security] Diagnostic level set to FULL \u2014 PeerIDs and network topology are exposed. Do NOT use in production.\n"
|
|
818
|
+
);
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
/** Cached manifests from remote peers. Key = PeerID */
|
|
822
|
+
manifestCache = /* @__PURE__ */ new Map();
|
|
823
|
+
/** Guards against concurrent discovery storms */
|
|
824
|
+
currentDiscovery = null;
|
|
825
|
+
/** Verifier for Tier-0 integrity checks */
|
|
826
|
+
verifier = new LiopVerifier();
|
|
827
|
+
/** Callback when new remote tools are discovered */
|
|
828
|
+
onToolsChanged;
|
|
829
|
+
/** Circuit-breaker state for peers that repeatedly fail manifest queries. */
|
|
830
|
+
manifestFailureState = /* @__PURE__ */ new Map();
|
|
831
|
+
static MANIFEST_FAILURE_BASE_COOLDOWN_MS = 15e3;
|
|
832
|
+
static MANIFEST_FAILURE_MAX_COOLDOWN_MS = 5 * 6e4;
|
|
833
|
+
static MANIFEST_SKIP_LOG_THROTTLE_MS = 3e4;
|
|
834
|
+
shouldSkipManifestQuery(peerId) {
|
|
835
|
+
const state = this.manifestFailureState.get(peerId);
|
|
836
|
+
if (!state) return false;
|
|
837
|
+
const now = Date.now();
|
|
838
|
+
if (now >= state.cooldownUntil) return false;
|
|
839
|
+
if (now - state.lastSkipLogAt > _LiopMcpRouter.MANIFEST_SKIP_LOG_THROTTLE_MS) {
|
|
840
|
+
log.info(
|
|
841
|
+
`[LIOP-Router] Skipping manifest query for ${peerId} during cooldown (${Math.ceil((state.cooldownUntil - now) / 1e3)}s remaining)`
|
|
842
|
+
);
|
|
843
|
+
state.lastSkipLogAt = now;
|
|
844
|
+
}
|
|
845
|
+
return true;
|
|
846
|
+
}
|
|
847
|
+
recordManifestQuerySuccess(peerId) {
|
|
848
|
+
this.manifestFailureState.delete(peerId);
|
|
849
|
+
}
|
|
850
|
+
recordManifestQueryFailure(peerId) {
|
|
851
|
+
const now = Date.now();
|
|
852
|
+
const prev = this.manifestFailureState.get(peerId);
|
|
853
|
+
const failures = (prev?.failures || 0) + 1;
|
|
854
|
+
const backoff = Math.min(
|
|
855
|
+
_LiopMcpRouter.MANIFEST_FAILURE_BASE_COOLDOWN_MS * 2 ** Math.max(0, failures - 1),
|
|
856
|
+
_LiopMcpRouter.MANIFEST_FAILURE_MAX_COOLDOWN_MS
|
|
857
|
+
);
|
|
858
|
+
this.manifestFailureState.set(peerId, {
|
|
859
|
+
failures,
|
|
860
|
+
cooldownUntil: now + backoff,
|
|
861
|
+
lastSkipLogAt: 0
|
|
862
|
+
});
|
|
863
|
+
}
|
|
864
|
+
async dispatch(request, authInfo) {
|
|
865
|
+
const { method, params, id } = request;
|
|
866
|
+
log.info(`[LIOP-Router] Processing: ${method}`);
|
|
867
|
+
if (this.liopServer.jwtValidator) {
|
|
868
|
+
const authResult = authorizeRequest(method, authInfo ?? null);
|
|
869
|
+
if (!authResult.allowed) {
|
|
870
|
+
log.info(
|
|
871
|
+
`[LIOP-Router] RBAC Access Denied for method '${method}': ${authResult.reason}`
|
|
872
|
+
);
|
|
873
|
+
return {
|
|
874
|
+
jsonrpc: "2.0",
|
|
875
|
+
id,
|
|
876
|
+
error: {
|
|
877
|
+
code: -32099,
|
|
878
|
+
// Custom authentication/authorization failure code
|
|
879
|
+
message: authResult.reason || "Access Denied"
|
|
880
|
+
}
|
|
881
|
+
};
|
|
882
|
+
}
|
|
883
|
+
}
|
|
884
|
+
switch (method) {
|
|
885
|
+
case "initialize":
|
|
886
|
+
return {
|
|
887
|
+
jsonrpc: "2.0",
|
|
888
|
+
id,
|
|
889
|
+
result: {
|
|
890
|
+
protocolVersion: "2025-11-25",
|
|
891
|
+
capabilities: {
|
|
892
|
+
tools: { listChanged: true },
|
|
893
|
+
resources: { listChanged: true },
|
|
894
|
+
prompts: { listChanged: true }
|
|
895
|
+
},
|
|
896
|
+
serverInfo: this.liopServer.getServerInfo()
|
|
897
|
+
}
|
|
898
|
+
};
|
|
899
|
+
case "notifications/initialized":
|
|
900
|
+
this.kickDiscoveryAfterInitialized().catch(() => {
|
|
901
|
+
});
|
|
902
|
+
return null;
|
|
903
|
+
case "notifications/cancelled":
|
|
904
|
+
return null;
|
|
905
|
+
// No-op for MCP spec compliance
|
|
906
|
+
case "ping":
|
|
907
|
+
return { jsonrpc: "2.0", id, result: {} };
|
|
908
|
+
case "tools/list": {
|
|
909
|
+
const localTools = this.liopServer.listTools();
|
|
910
|
+
const remoteTools = await this.getRemoteTools();
|
|
911
|
+
const listedLocals = mcpCompactToolDescriptions() ? localTools.map((t) => ({
|
|
912
|
+
...t,
|
|
913
|
+
description: stripVerboseLiopToolDescription(t.description ?? "")
|
|
914
|
+
})) : localTools;
|
|
915
|
+
log.info(
|
|
916
|
+
`[LIOP-Router] tools/list: ${localTools.length} local, ${remoteTools.length} remote tools found`
|
|
917
|
+
);
|
|
918
|
+
const diagnosticTool = {
|
|
919
|
+
name: "LiopMeshStatus",
|
|
920
|
+
description: "LiopMeshStatus: Returns the current dynamic diagnostic status of the Zero-Trust Neural Mesh.",
|
|
921
|
+
inputSchema: {
|
|
922
|
+
type: "object",
|
|
923
|
+
properties: {},
|
|
924
|
+
additionalProperties: false
|
|
925
|
+
}
|
|
926
|
+
};
|
|
927
|
+
const allTools = [diagnosticTool, ...listedLocals, ...remoteTools];
|
|
928
|
+
const telemetry = TokenTelemetryEngine.getInstance();
|
|
929
|
+
const toolsPayload = JSON.stringify(allTools);
|
|
930
|
+
const toolsResponsePayload = JSON.stringify({ tools: allTools });
|
|
931
|
+
telemetry.record({
|
|
932
|
+
type: "tools_list",
|
|
933
|
+
method: "tools/list",
|
|
934
|
+
estimatedInputTokens: telemetry.countTokens(toolsPayload),
|
|
935
|
+
estimatedOutputTokens: telemetry.countTokens(toolsResponsePayload)
|
|
936
|
+
});
|
|
937
|
+
return {
|
|
938
|
+
jsonrpc: "2.0",
|
|
939
|
+
id,
|
|
940
|
+
result: {
|
|
941
|
+
tools: allTools
|
|
942
|
+
}
|
|
943
|
+
};
|
|
944
|
+
}
|
|
945
|
+
case "tools/call":
|
|
946
|
+
return this.transcodeMcpToLiop(
|
|
947
|
+
id,
|
|
948
|
+
params,
|
|
949
|
+
authInfo?.token
|
|
950
|
+
);
|
|
951
|
+
case "resources/list": {
|
|
952
|
+
const localResources = this.liopServer.listResources();
|
|
953
|
+
const remoteResources = await this.getRemoteResources();
|
|
954
|
+
const allResources = [...localResources, ...remoteResources];
|
|
955
|
+
const rlTelemetry = TokenTelemetryEngine.getInstance();
|
|
956
|
+
const rlPayload = JSON.stringify(allResources);
|
|
957
|
+
rlTelemetry.record({
|
|
958
|
+
type: "resource_list",
|
|
959
|
+
method: "resources/list",
|
|
960
|
+
estimatedInputTokens: 0,
|
|
961
|
+
estimatedOutputTokens: rlTelemetry.countTokens(rlPayload)
|
|
962
|
+
});
|
|
963
|
+
return {
|
|
964
|
+
jsonrpc: "2.0",
|
|
965
|
+
id,
|
|
966
|
+
result: { resources: allResources }
|
|
967
|
+
};
|
|
968
|
+
}
|
|
969
|
+
case "resources/read": {
|
|
970
|
+
const typedParams = params;
|
|
971
|
+
if (!typedParams?.uri)
|
|
972
|
+
return {
|
|
973
|
+
jsonrpc: "2.0",
|
|
974
|
+
id,
|
|
975
|
+
error: { code: -32602, message: "Missing resource uri" }
|
|
976
|
+
};
|
|
977
|
+
try {
|
|
978
|
+
const rrStartTime = Date.now();
|
|
979
|
+
const result = await this.liopServer.readResource(typedParams.uri);
|
|
980
|
+
const rrTelemetry = TokenTelemetryEngine.getInstance();
|
|
981
|
+
const rrOutputPayload = JSON.stringify(result);
|
|
982
|
+
rrTelemetry.record({
|
|
983
|
+
type: "resource_read",
|
|
984
|
+
method: "resources/read",
|
|
985
|
+
toolName: typedParams.uri,
|
|
986
|
+
estimatedInputTokens: rrTelemetry.countTokens(typedParams.uri),
|
|
987
|
+
estimatedOutputTokens: rrTelemetry.countTokens(rrOutputPayload),
|
|
988
|
+
durationMs: Date.now() - rrStartTime
|
|
989
|
+
});
|
|
990
|
+
return { jsonrpc: "2.0", id, result };
|
|
991
|
+
} catch (err) {
|
|
992
|
+
const targetUri = typedParams.uri;
|
|
993
|
+
for (const { manifest } of this.manifestCache.values()) {
|
|
994
|
+
const remoteResource = manifest.resources.find(
|
|
995
|
+
(r) => r.uri === targetUri
|
|
996
|
+
);
|
|
997
|
+
if (remoteResource) {
|
|
998
|
+
log.info(
|
|
999
|
+
`[LIOP-Router] Resolved resource ${targetUri} from cache (Peer: ${manifest.peerId})`
|
|
1000
|
+
);
|
|
1001
|
+
return {
|
|
1002
|
+
jsonrpc: "2.0",
|
|
1003
|
+
id,
|
|
1004
|
+
result: {
|
|
1005
|
+
contents: [
|
|
1006
|
+
{
|
|
1007
|
+
uri: remoteResource.uri,
|
|
1008
|
+
mimeType: remoteResource.mimeType || "text/plain",
|
|
1009
|
+
text: remoteResource.text || remoteResource.description || "No content provided"
|
|
1010
|
+
}
|
|
1011
|
+
]
|
|
1012
|
+
}
|
|
1013
|
+
};
|
|
1014
|
+
}
|
|
1015
|
+
}
|
|
1016
|
+
return {
|
|
1017
|
+
jsonrpc: "2.0",
|
|
1018
|
+
id,
|
|
1019
|
+
error: {
|
|
1020
|
+
code: -32e3,
|
|
1021
|
+
message: err instanceof Error ? err.message : String(err)
|
|
1022
|
+
}
|
|
1023
|
+
};
|
|
1024
|
+
}
|
|
1025
|
+
}
|
|
1026
|
+
case "prompts/list": {
|
|
1027
|
+
const promptsList = this.liopServer.listPrompts();
|
|
1028
|
+
const plTelemetry = TokenTelemetryEngine.getInstance();
|
|
1029
|
+
const plPayload = JSON.stringify(promptsList);
|
|
1030
|
+
plTelemetry.record({
|
|
1031
|
+
type: "prompt_list",
|
|
1032
|
+
method: "prompts/list",
|
|
1033
|
+
estimatedInputTokens: 0,
|
|
1034
|
+
estimatedOutputTokens: plTelemetry.countTokens(plPayload)
|
|
1035
|
+
});
|
|
1036
|
+
return {
|
|
1037
|
+
jsonrpc: "2.0",
|
|
1038
|
+
id,
|
|
1039
|
+
result: { prompts: promptsList }
|
|
1040
|
+
};
|
|
1041
|
+
}
|
|
1042
|
+
case "prompts/get": {
|
|
1043
|
+
const typedParams = params;
|
|
1044
|
+
if (!typedParams?.name)
|
|
1045
|
+
return {
|
|
1046
|
+
jsonrpc: "2.0",
|
|
1047
|
+
id,
|
|
1048
|
+
error: { code: -32602, message: "Missing prompt name" }
|
|
1049
|
+
};
|
|
1050
|
+
try {
|
|
1051
|
+
const pgStartTime = Date.now();
|
|
1052
|
+
const result = await this.liopServer.getPrompt({
|
|
1053
|
+
name: typedParams.name,
|
|
1054
|
+
arguments: typedParams.arguments || {}
|
|
1055
|
+
});
|
|
1056
|
+
const pgTelemetry = TokenTelemetryEngine.getInstance();
|
|
1057
|
+
const pgInputPayload = JSON.stringify({
|
|
1058
|
+
name: typedParams.name,
|
|
1059
|
+
arguments: typedParams.arguments
|
|
1060
|
+
});
|
|
1061
|
+
const pgOutputPayload = JSON.stringify(result);
|
|
1062
|
+
pgTelemetry.record({
|
|
1063
|
+
type: "prompt_get",
|
|
1064
|
+
method: "prompts/get",
|
|
1065
|
+
toolName: typedParams.name,
|
|
1066
|
+
estimatedInputTokens: pgTelemetry.countTokens(pgInputPayload),
|
|
1067
|
+
estimatedOutputTokens: pgTelemetry.countTokens(pgOutputPayload),
|
|
1068
|
+
durationMs: Date.now() - pgStartTime
|
|
1069
|
+
});
|
|
1070
|
+
return { jsonrpc: "2.0", id, result };
|
|
1071
|
+
} catch (err) {
|
|
1072
|
+
return {
|
|
1073
|
+
jsonrpc: "2.0",
|
|
1074
|
+
id,
|
|
1075
|
+
error: {
|
|
1076
|
+
code: -32e3,
|
|
1077
|
+
message: err instanceof Error ? err.message : String(err)
|
|
1078
|
+
}
|
|
1079
|
+
};
|
|
1080
|
+
}
|
|
1081
|
+
}
|
|
1082
|
+
default:
|
|
1083
|
+
return {
|
|
1084
|
+
jsonrpc: "2.0",
|
|
1085
|
+
id,
|
|
1086
|
+
error: { code: -32601, message: `Method not found: ${method}` }
|
|
1087
|
+
};
|
|
1088
|
+
}
|
|
1089
|
+
}
|
|
1090
|
+
/**
|
|
1091
|
+
* MCP clients often send notifications/initialized then immediately tools/list.
|
|
1092
|
+
* Start manifest discovery without blocking the notification handler.
|
|
1093
|
+
*/
|
|
1094
|
+
kickDiscoveryAfterInitialized() {
|
|
1095
|
+
return (async () => {
|
|
1096
|
+
await new Promise((r) => setTimeout(r, 250));
|
|
1097
|
+
await Promise.race([
|
|
1098
|
+
this.refreshManifestCache(true),
|
|
1099
|
+
new Promise((r) => setTimeout(r, 15e3))
|
|
1100
|
+
]).catch(() => {
|
|
1101
|
+
});
|
|
1102
|
+
})();
|
|
1103
|
+
}
|
|
1104
|
+
/**
|
|
1105
|
+
* Discovers and caches manifests from all remote LIOP providers in the mesh.
|
|
1106
|
+
* Uses Kademlia DHT to find "liop:manifest" providers, then opens
|
|
1107
|
+
* /liop/manifest/1.0.0 protocol streams to retrieve their full metadata.
|
|
1108
|
+
*/
|
|
1109
|
+
async refreshManifestCache(silent = false) {
|
|
1110
|
+
if (!this.meshNode) return;
|
|
1111
|
+
if (this.currentDiscovery) return this.currentDiscovery;
|
|
1112
|
+
if (!silent && this.manifestCache.size > 0) {
|
|
1113
|
+
const now = Date.now();
|
|
1114
|
+
const allFresh = Array.from(this.manifestCache.values()).every(
|
|
1115
|
+
({ cachedAt }) => now - cachedAt < MANIFEST_CACHE_TTL_S * 1e3
|
|
1116
|
+
);
|
|
1117
|
+
if (allFresh) return;
|
|
1118
|
+
}
|
|
1119
|
+
this.currentDiscovery = (async () => {
|
|
1120
|
+
try {
|
|
1121
|
+
const prevCount = Array.from(this.manifestCache.values()).reduce(
|
|
1122
|
+
(acc, { manifest }) => acc + manifest.tools.length,
|
|
1123
|
+
0
|
|
1124
|
+
);
|
|
1125
|
+
if (this.manifestCache.size === 0) {
|
|
1126
|
+
for (let i = 0; i < 3; i++) {
|
|
1127
|
+
const connections = (
|
|
1128
|
+
// biome-ignore lint/suspicious/noExplicitAny: access internal nodes for connection count
|
|
1129
|
+
this.meshNode.node?.getConnections().length || 0
|
|
1130
|
+
);
|
|
1131
|
+
if (connections > 0) {
|
|
1132
|
+
log.info(
|
|
1133
|
+
`[LIOP-Router] P2P Connection established. Starting discovery...`
|
|
1134
|
+
);
|
|
1135
|
+
break;
|
|
1136
|
+
}
|
|
1137
|
+
log.info(
|
|
1138
|
+
`[LIOP-Router] Waiting for P2P connections (attempt ${i + 1}/10)...`
|
|
1139
|
+
);
|
|
1140
|
+
await new Promise((r) => setTimeout(r, 1e3));
|
|
1141
|
+
}
|
|
1142
|
+
}
|
|
1143
|
+
let providerIds = [];
|
|
1144
|
+
const MAX_COLD_ATTEMPTS = this.manifestCache.size === 0 ? 5 : 1;
|
|
1145
|
+
for (let coldAttempt = 0; coldAttempt < MAX_COLD_ATTEMPTS; coldAttempt++) {
|
|
1146
|
+
for (let attempt = 0; attempt < MANIFEST_DISCOVERY_RETRIES; attempt++) {
|
|
1147
|
+
providerIds = await this.meshNode?.discoverManifestProviders() || [];
|
|
1148
|
+
const selfId2 = this.meshNode?.getPeerId();
|
|
1149
|
+
const remoteIds = providerIds.filter((id) => id !== selfId2);
|
|
1150
|
+
if (remoteIds.length > 0) break;
|
|
1151
|
+
if (attempt < MANIFEST_DISCOVERY_RETRIES - 1) {
|
|
1152
|
+
log.info(
|
|
1153
|
+
`[LIOP-Router] DHT discovery attempt ${attempt + 1}/${MANIFEST_DISCOVERY_RETRIES}...`
|
|
1154
|
+
);
|
|
1155
|
+
await new Promise((r) => setTimeout(r, 1e3));
|
|
1156
|
+
}
|
|
1157
|
+
}
|
|
1158
|
+
const activePeers = (
|
|
1159
|
+
// biome-ignore lint/suspicious/noExplicitAny: access internal nodes
|
|
1160
|
+
this.meshNode.node?.getConnections().map(
|
|
1161
|
+
(c) => c.remotePeer.toString()
|
|
1162
|
+
) || []
|
|
1163
|
+
);
|
|
1164
|
+
if (activePeers.length > 0) {
|
|
1165
|
+
providerIds = Array.from(/* @__PURE__ */ new Set([...providerIds, ...activePeers]));
|
|
1166
|
+
}
|
|
1167
|
+
const selfIdEnd = this.meshNode?.getPeerId();
|
|
1168
|
+
const remoteIdsEnd = providerIds.filter((id) => id !== selfIdEnd);
|
|
1169
|
+
if (remoteIdsEnd.length > 0) break;
|
|
1170
|
+
if (coldAttempt < MAX_COLD_ATTEMPTS - 1) {
|
|
1171
|
+
log.info(
|
|
1172
|
+
`[LIOP-Router] Initial discovery failed (0 providers). Retrying in 1s (${coldAttempt + 1}/${MAX_COLD_ATTEMPTS})...`
|
|
1173
|
+
);
|
|
1174
|
+
await new Promise((r) => setTimeout(r, 1e3));
|
|
1175
|
+
}
|
|
1176
|
+
}
|
|
1177
|
+
if (providerIds.length === 0) {
|
|
1178
|
+
log.info(
|
|
1179
|
+
`[LIOP-Router] No manifest providers found after all attempts.`
|
|
1180
|
+
);
|
|
1181
|
+
return;
|
|
1182
|
+
}
|
|
1183
|
+
if (!silent) {
|
|
1184
|
+
log.info(
|
|
1185
|
+
`[LIOP-Router] Discovered ${providerIds.length} candidate manifest providers`
|
|
1186
|
+
);
|
|
1187
|
+
}
|
|
1188
|
+
const connectedPeers = new Set(
|
|
1189
|
+
// biome-ignore lint/suspicious/noExplicitAny: internal node access for fast peer ordering
|
|
1190
|
+
(this.meshNode.node?.getConnections?.() || []).map(
|
|
1191
|
+
(c) => c.remotePeer.toString()
|
|
1192
|
+
)
|
|
1193
|
+
);
|
|
1194
|
+
providerIds = [...providerIds].sort((a, b) => {
|
|
1195
|
+
const aConnected = connectedPeers.has(a) ? 1 : 0;
|
|
1196
|
+
const bConnected = connectedPeers.has(b) ? 1 : 0;
|
|
1197
|
+
return bConnected - aConnected;
|
|
1198
|
+
});
|
|
1199
|
+
let successCount = 0;
|
|
1200
|
+
let errorCount = 0;
|
|
1201
|
+
let cacheUpdated = false;
|
|
1202
|
+
const selfId = this.meshNode?.getPeerId();
|
|
1203
|
+
const eligiblePeers = providerIds.filter((peerId) => {
|
|
1204
|
+
if (!this.meshNode) return false;
|
|
1205
|
+
if (peerId === selfId) return false;
|
|
1206
|
+
if (this.shouldSkipManifestQuery(peerId)) return false;
|
|
1207
|
+
const cached = this.manifestCache.get(peerId);
|
|
1208
|
+
if (cached && Date.now() - cached.cachedAt < MANIFEST_CACHE_TTL_S * 1e3) {
|
|
1209
|
+
successCount++;
|
|
1210
|
+
return false;
|
|
1211
|
+
}
|
|
1212
|
+
return true;
|
|
1213
|
+
});
|
|
1214
|
+
const queryResults = await Promise.allSettled(
|
|
1215
|
+
eligiblePeers.map(async (peerId) => {
|
|
1216
|
+
if (!this.meshNode) return null;
|
|
1217
|
+
log.info(`[LIOP-Router] Querying manifest from: ${peerId}`);
|
|
1218
|
+
return {
|
|
1219
|
+
peerId,
|
|
1220
|
+
manifest: await this.meshNode.queryManifest(peerId)
|
|
1221
|
+
};
|
|
1222
|
+
})
|
|
1223
|
+
);
|
|
1224
|
+
for (const result of queryResults) {
|
|
1225
|
+
if (result.status === "fulfilled" && result.value?.manifest) {
|
|
1226
|
+
const { peerId, manifest } = result.value;
|
|
1227
|
+
this.manifestCache.set(peerId, {
|
|
1228
|
+
manifest,
|
|
1229
|
+
cachedAt: Date.now()
|
|
1230
|
+
});
|
|
1231
|
+
this.recordManifestQuerySuccess(peerId);
|
|
1232
|
+
cacheUpdated = true;
|
|
1233
|
+
successCount++;
|
|
1234
|
+
log.info(
|
|
1235
|
+
`[LIOP-Router] Manifest received from ${peerId} (${manifest.tools.length} tools)`
|
|
1236
|
+
);
|
|
1237
|
+
} else if (result.status === "fulfilled" && result.value) {
|
|
1238
|
+
this.recordManifestQueryFailure(result.value.peerId);
|
|
1239
|
+
errorCount++;
|
|
1240
|
+
log.info(
|
|
1241
|
+
`[LIOP-Router] Manifest query returned NULL for ${result.value.peerId}`
|
|
1242
|
+
);
|
|
1243
|
+
} else if (result.status === "rejected") {
|
|
1244
|
+
errorCount++;
|
|
1245
|
+
log.info(
|
|
1246
|
+
`[LIOP-Router] Fatal error querying manifest:`,
|
|
1247
|
+
result.reason instanceof Error ? result.reason.message : String(result.reason)
|
|
1248
|
+
);
|
|
1249
|
+
}
|
|
1250
|
+
}
|
|
1251
|
+
this._discoveryStats = {
|
|
1252
|
+
candidates: providerIds.length,
|
|
1253
|
+
success: successCount,
|
|
1254
|
+
failures: errorCount,
|
|
1255
|
+
lastDiscovery: Date.now()
|
|
1256
|
+
};
|
|
1257
|
+
if (cacheUpdated) {
|
|
1258
|
+
const newCount = Array.from(this.manifestCache.values()).reduce(
|
|
1259
|
+
(acc, { manifest }) => acc + manifest.tools.length,
|
|
1260
|
+
0
|
|
1261
|
+
);
|
|
1262
|
+
if (newCount !== prevCount && this.onToolsChanged) {
|
|
1263
|
+
process.stderr.write(
|
|
1264
|
+
"[LIOP-Router] Mesh topology updated! Emitting notifications/tools/list_changed.\n"
|
|
1265
|
+
);
|
|
1266
|
+
this.onToolsChanged();
|
|
1267
|
+
}
|
|
1268
|
+
}
|
|
1269
|
+
} finally {
|
|
1270
|
+
this.currentDiscovery = null;
|
|
1271
|
+
}
|
|
1272
|
+
})();
|
|
1273
|
+
return this.currentDiscovery;
|
|
1274
|
+
}
|
|
1275
|
+
/**
|
|
1276
|
+
* Returns the current manifest cache size for external telemetry.
|
|
1277
|
+
* Used by the adaptive polling system to detect topology stabilization.
|
|
1278
|
+
*/
|
|
1279
|
+
getCacheSize() {
|
|
1280
|
+
return this.manifestCache.size;
|
|
1281
|
+
}
|
|
1282
|
+
/**
|
|
1283
|
+
* Returns all remote tools discovered via the manifest protocol.
|
|
1284
|
+
*/
|
|
1285
|
+
async getRemoteTools() {
|
|
1286
|
+
const EXPECTED_PROVIDERS = Number.parseInt(
|
|
1287
|
+
process.env.LIOP_EXPECTED_PROVIDERS ?? "1",
|
|
1288
|
+
10
|
|
1289
|
+
);
|
|
1290
|
+
if (this.manifestCache.size < EXPECTED_PROVIDERS && this.meshNode) {
|
|
1291
|
+
const initialTimeoutMs = Number.parseInt(
|
|
1292
|
+
process.env.LIOP_INITIAL_DISCOVERY_TIMEOUT_MS ?? "8000",
|
|
1293
|
+
10
|
|
1294
|
+
);
|
|
1295
|
+
const boundedTimeoutMs = Number.isFinite(initialTimeoutMs) && initialTimeoutMs > 0 ? initialTimeoutMs : 12e3;
|
|
1296
|
+
const deadline = Date.now() + boundedTimeoutMs;
|
|
1297
|
+
let stableCount = 0;
|
|
1298
|
+
let lastCacheSize = -1;
|
|
1299
|
+
while (Date.now() < deadline) {
|
|
1300
|
+
if (this.manifestCache.size >= EXPECTED_PROVIDERS) break;
|
|
1301
|
+
await Promise.race([
|
|
1302
|
+
this.refreshManifestCache(true),
|
|
1303
|
+
new Promise((resolve) => setTimeout(resolve, 3e3))
|
|
1304
|
+
]).catch(() => {
|
|
1305
|
+
});
|
|
1306
|
+
if (this.manifestCache.size >= EXPECTED_PROVIDERS) break;
|
|
1307
|
+
if (this.manifestCache.size === lastCacheSize) {
|
|
1308
|
+
stableCount++;
|
|
1309
|
+
if (stableCount >= 3 && this.manifestCache.size > 0) {
|
|
1310
|
+
log.info(
|
|
1311
|
+
`[LIOP-Router] Provider count stabilized at ${this.manifestCache.size}/${EXPECTED_PROVIDERS}. Proceeding with available mesh.`
|
|
1312
|
+
);
|
|
1313
|
+
break;
|
|
1314
|
+
}
|
|
1315
|
+
} else {
|
|
1316
|
+
stableCount = 0;
|
|
1317
|
+
lastCacheSize = this.manifestCache.size;
|
|
1318
|
+
}
|
|
1319
|
+
await new Promise((r) => setTimeout(r, 1e3));
|
|
1320
|
+
}
|
|
1321
|
+
if (this.manifestCache.size < EXPECTED_PROVIDERS) {
|
|
1322
|
+
log.info(
|
|
1323
|
+
`[LIOP-Router] \u26A0\uFE0F Mesh partially available: ${this.manifestCache.size}/${EXPECTED_PROVIDERS} providers. Some tools may be unavailable. Check Docker containers.`
|
|
1324
|
+
);
|
|
1325
|
+
this.refreshManifestCache(true).catch(() => {
|
|
1326
|
+
});
|
|
1327
|
+
}
|
|
1328
|
+
}
|
|
1329
|
+
const tools = [];
|
|
1330
|
+
const seenNames = /* @__PURE__ */ new Set();
|
|
1331
|
+
const localToolNames = new Set(
|
|
1332
|
+
this.liopServer.listTools().map((t) => t.name)
|
|
1333
|
+
);
|
|
1334
|
+
for (const [peerId, { manifest }] of this.manifestCache.entries()) {
|
|
1335
|
+
for (const tool of manifest.tools) {
|
|
1336
|
+
if (tool.name === "LiopMeshStatus") continue;
|
|
1337
|
+
let finalName = tool.name;
|
|
1338
|
+
if (seenNames.has(tool.name) || localToolNames.has(tool.name)) {
|
|
1339
|
+
finalName = `${tool.name}_${peerId.slice(-4)}`;
|
|
1340
|
+
}
|
|
1341
|
+
seenNames.add(finalName);
|
|
1342
|
+
const providerName = manifest.serverInfo?.name || "Unknown Provider";
|
|
1343
|
+
const baseDesc = tool.description || `Remote tool from ${providerName}`;
|
|
1344
|
+
const cleanTool = {
|
|
1345
|
+
name: finalName,
|
|
1346
|
+
description: mcpCompactToolDescriptions() ? stripVerboseLiopToolDescription(baseDesc) : baseDesc,
|
|
1347
|
+
inputSchema: tool.inputSchema || {
|
|
1348
|
+
type: "object",
|
|
1349
|
+
properties: {}
|
|
1350
|
+
}
|
|
1351
|
+
};
|
|
1352
|
+
if (typeof cleanTool.inputSchema === "object" && !cleanTool.inputSchema.type) {
|
|
1353
|
+
cleanTool.inputSchema.type = "object";
|
|
1354
|
+
}
|
|
1355
|
+
if (typeof cleanTool.inputSchema === "object" && !cleanTool.inputSchema.properties) {
|
|
1356
|
+
cleanTool.inputSchema.properties = {};
|
|
1357
|
+
}
|
|
1358
|
+
let blueprint = "";
|
|
1359
|
+
if (manifest.taxonomy) {
|
|
1360
|
+
blueprint = `
|
|
1361
|
+
[LIOP-DOMAIN: ${manifest.taxonomy.domain}]`;
|
|
1362
|
+
}
|
|
1363
|
+
const properties = cleanTool.inputSchema.properties || {};
|
|
1364
|
+
let envelopeDoc = "";
|
|
1365
|
+
if (!mcpCompactToolDescriptions() && properties.payload) {
|
|
1366
|
+
envelopeDoc = `
|
|
1367
|
+
[REQUIRES: LIOP-PROTO-V1 ENVELOPE]`;
|
|
1368
|
+
}
|
|
1369
|
+
if (!mcpCompactToolDescriptions() && cleanTool.description.includes("STRICT SCHEMA ADHERENCE")) {
|
|
1370
|
+
cleanTool.description = cleanTool.description.replace(
|
|
1371
|
+
"STRICT SCHEMA ADHERENCE:",
|
|
1372
|
+
"[INDUSTRIAL-REQUISITE] STRICT SCHEMA ADHERENCE (MANDATORY):"
|
|
1373
|
+
);
|
|
1374
|
+
}
|
|
1375
|
+
const originStamp = mcpCompactToolDescriptions() ? `
|
|
1376
|
+
(Peer: ${peerId.slice(-8)})${blueprint}` : `
|
|
1377
|
+
(Origin: ${peerId.slice(-8)})${blueprint}${envelopeDoc}`;
|
|
1378
|
+
cleanTool.description = `${cleanTool.description}${originStamp}`;
|
|
1379
|
+
tools.push(cleanTool);
|
|
1380
|
+
}
|
|
1381
|
+
}
|
|
1382
|
+
return tools;
|
|
1383
|
+
}
|
|
1384
|
+
/**
|
|
1385
|
+
* Returns all remote resources discovered via the manifest protocol.
|
|
1386
|
+
*/
|
|
1387
|
+
async getRemoteResources() {
|
|
1388
|
+
if (!this.currentDiscovery) {
|
|
1389
|
+
this.refreshManifestCache(true).catch(() => {
|
|
1390
|
+
});
|
|
1391
|
+
}
|
|
1392
|
+
const resources = [];
|
|
1393
|
+
const seenUris = new Set(this.liopServer.listResources().map((r) => r.uri));
|
|
1394
|
+
for (const [peerId, { manifest }] of this.manifestCache.entries()) {
|
|
1395
|
+
for (const resource of manifest.resources) {
|
|
1396
|
+
if (!seenUris.has(resource.uri)) {
|
|
1397
|
+
const augmentedResource = { ...resource };
|
|
1398
|
+
const providerName = manifest.serverInfo?.name || "Unknown Provider";
|
|
1399
|
+
let blueprint = "";
|
|
1400
|
+
if (manifest.taxonomy) {
|
|
1401
|
+
blueprint = `
|
|
1402
|
+
|
|
1403
|
+
[LIOP Zero-Trust Blueprint]
|
|
1404
|
+
Domain: ${manifest.taxonomy.domain}
|
|
1405
|
+
Clearance Tier: ${manifest.taxonomy.clearanceTier}`;
|
|
1406
|
+
if (manifest.taxonomy.executionTypes && manifest.taxonomy.executionTypes.length > 0) {
|
|
1407
|
+
blueprint += `
|
|
1408
|
+
Execution Types: ${manifest.taxonomy.executionTypes.join(", ")}`;
|
|
1409
|
+
}
|
|
1410
|
+
}
|
|
1411
|
+
const originStamp = `
|
|
1412
|
+
|
|
1413
|
+
[LIOP Zero-Trust Origin]
|
|
1414
|
+
Provider: ${providerName}
|
|
1415
|
+
Network ID: ${peerId}${blueprint}`;
|
|
1416
|
+
if (augmentedResource.uri.startsWith("liop://schema/")) {
|
|
1417
|
+
augmentedResource.name = `[SCHEMA] ${augmentedResource.name}`;
|
|
1418
|
+
augmentedResource.description = `[CRITICAL SCHEMA] ${augmentedResource.description || "Data Dictionary for Zero-Shot Autonomy"}${originStamp}`;
|
|
1419
|
+
} else {
|
|
1420
|
+
augmentedResource.description = augmentedResource.description ? `${augmentedResource.description}${originStamp}` : originStamp.trim();
|
|
1421
|
+
}
|
|
1422
|
+
resources.push(augmentedResource);
|
|
1423
|
+
seenUris.add(resource.uri);
|
|
1424
|
+
}
|
|
1425
|
+
}
|
|
1426
|
+
}
|
|
1427
|
+
return resources;
|
|
1428
|
+
}
|
|
1429
|
+
/**
|
|
1430
|
+
* Resolves the gRPC target (host:port) AND the peerId for a given tool name
|
|
1431
|
+
* by searching the manifest cache. Supports exact names and suffixed names.
|
|
1432
|
+
*/
|
|
1433
|
+
resolveManifestTarget(toolName) {
|
|
1434
|
+
for (const [peerId, { manifest }] of this.manifestCache.entries()) {
|
|
1435
|
+
const tool = manifest.tools.find((t) => t.name === toolName);
|
|
1436
|
+
if (tool) {
|
|
1437
|
+
return {
|
|
1438
|
+
peerId,
|
|
1439
|
+
originalToolName: toolName
|
|
1440
|
+
};
|
|
1441
|
+
}
|
|
1442
|
+
}
|
|
1443
|
+
const parts = toolName.split("_");
|
|
1444
|
+
if (parts.length > 1) {
|
|
1445
|
+
const suffix = parts.pop();
|
|
1446
|
+
const baseName = parts.join("_");
|
|
1447
|
+
for (const [peerId, { manifest }] of this.manifestCache.entries()) {
|
|
1448
|
+
if (peerId.endsWith(suffix || "")) {
|
|
1449
|
+
const tool = manifest.tools.find((t) => t.name === baseName);
|
|
1450
|
+
if (tool) {
|
|
1451
|
+
return {
|
|
1452
|
+
peerId,
|
|
1453
|
+
originalToolName: baseName
|
|
1454
|
+
};
|
|
1455
|
+
}
|
|
1456
|
+
}
|
|
1457
|
+
}
|
|
1458
|
+
}
|
|
1459
|
+
return null;
|
|
1460
|
+
}
|
|
1461
|
+
/**
|
|
1462
|
+
* Redacts a PeerID for external-facing diagnostics.
|
|
1463
|
+
* LIOP_DIAGNOSTIC_LEVEL controls verbosity:
|
|
1464
|
+
* - "redacted" (default): truncated to last 8 chars
|
|
1465
|
+
* - "full": complete PeerID (development only)
|
|
1466
|
+
*/
|
|
1467
|
+
redactPeerId(peerId) {
|
|
1468
|
+
const level = process.env.LIOP_DIAGNOSTIC_LEVEL || "redacted";
|
|
1469
|
+
if (level === "full") return peerId;
|
|
1470
|
+
return `***${peerId.slice(-8)}`;
|
|
1471
|
+
}
|
|
1472
|
+
async transcodeMcpToLiop(id, params, token) {
|
|
1473
|
+
const toolName = params.name;
|
|
1474
|
+
if (toolName === "LiopMeshStatus") {
|
|
1475
|
+
this.refreshManifestCache(true).catch(() => {
|
|
1476
|
+
});
|
|
1477
|
+
const stats = this._discoveryStats || {
|
|
1478
|
+
candidates: 0,
|
|
1479
|
+
success: 0,
|
|
1480
|
+
failures: 0
|
|
1481
|
+
};
|
|
1482
|
+
const providerCount = this.manifestCache.size;
|
|
1483
|
+
const meshState = this.meshNode ? "Active" : "Offline";
|
|
1484
|
+
const cachedTools = Array.from(this.manifestCache.values()).reduce(
|
|
1485
|
+
(acc, { manifest }) => acc + manifest.tools.length,
|
|
1486
|
+
0
|
|
1487
|
+
);
|
|
1488
|
+
const connections = this.meshNode ? (
|
|
1489
|
+
// biome-ignore lint/suspicious/noExplicitAny: access internal nodes
|
|
1490
|
+
this.meshNode.node?.getConnections().length
|
|
1491
|
+
) : 0;
|
|
1492
|
+
const bootstrapNodes = this.meshNode && // biome-ignore lint/suspicious/noExplicitAny: access internal config
|
|
1493
|
+
this.meshNode.config?.bootstrapNodes ? (
|
|
1494
|
+
// biome-ignore lint/suspicious/noExplicitAny: access internal config
|
|
1495
|
+
this.meshNode.config.bootstrapNodes
|
|
1496
|
+
) : [];
|
|
1497
|
+
const bootstrapCount = bootstrapNodes.length;
|
|
1498
|
+
const diagLevel = process.env.LIOP_DIAGNOSTIC_LEVEL || "redacted";
|
|
1499
|
+
const showBootstraps = diagLevel !== "minimal";
|
|
1500
|
+
const bootstrapList = showBootstraps ? bootstrapNodes.map((addr) => {
|
|
1501
|
+
const parts = addr.split("/");
|
|
1502
|
+
const id2 = parts[parts.length - 1];
|
|
1503
|
+
return ` \u2022 ${id2 ? id2.slice(-8) : "Unknown"} (bootstrap)`;
|
|
1504
|
+
}).join("\n") : "";
|
|
1505
|
+
const routingTableSize = this.meshNode ? (
|
|
1506
|
+
// biome-ignore lint/suspicious/noExplicitAny: access internal nodes
|
|
1507
|
+
this.meshNode.getRoutingTableSize()
|
|
1508
|
+
) : 0;
|
|
1509
|
+
const rawPeerId = this.meshNode?.getPeerId() || "Offline";
|
|
1510
|
+
const localPeerId = rawPeerId === "Offline" ? rawPeerId : this.redactPeerId(rawPeerId);
|
|
1511
|
+
const cachedToolList = Array.from(this.manifestCache.entries()).flatMap(
|
|
1512
|
+
([peerId, { manifest }]) => manifest.tools.map(
|
|
1513
|
+
(t) => ` \u2022 ${t.name} (from origin: ${this.redactPeerId(peerId)})`
|
|
1514
|
+
)
|
|
1515
|
+
).join("\n");
|
|
1516
|
+
const statusText = [
|
|
1517
|
+
`LIOP Mesh Status: ${meshState === "Active" ? "Active" : "Offline"}`,
|
|
1518
|
+
`Local Agent Identity: ${localPeerId}`,
|
|
1519
|
+
`Network: ${connections} Conns | ${routingTableSize} Mesh Nodes | ${bootstrapCount} Bootstraps`,
|
|
1520
|
+
showBootstraps && bootstrapCount > 0 ? `
|
|
1521
|
+
Active Bootstraps:
|
|
1522
|
+
${bootstrapList}
|
|
1523
|
+
` : "",
|
|
1524
|
+
`Discovery: ${stats.candidates} Candidates | ${stats.success} OK | ${stats.failures} FAIL`,
|
|
1525
|
+
`Tooling: ${providerCount} Providers | ${cachedTools} Total Remote Tools`,
|
|
1526
|
+
cachedTools > 0 ? `
|
|
1527
|
+
Discovered Remote Tools (Zero-Trust Origins):
|
|
1528
|
+
${cachedToolList}` : "\nNo remote tools discovered yet.",
|
|
1529
|
+
// [Token Economy] Telemetry block (only appears when operations exist)
|
|
1530
|
+
TokenTelemetryEngine.getInstance().formatStatusBlock()
|
|
1531
|
+
].filter((line) => line !== "").join("\n");
|
|
1532
|
+
const diagTelemetry = TokenTelemetryEngine.getInstance();
|
|
1533
|
+
diagTelemetry.record({
|
|
1534
|
+
type: "diagnostic",
|
|
1535
|
+
method: "tools/call",
|
|
1536
|
+
toolName: "LiopMeshStatus",
|
|
1537
|
+
estimatedInputTokens: 0,
|
|
1538
|
+
estimatedOutputTokens: diagTelemetry.countTokens(statusText)
|
|
1539
|
+
});
|
|
1540
|
+
return {
|
|
1541
|
+
jsonrpc: "2.0",
|
|
1542
|
+
id,
|
|
1543
|
+
result: {
|
|
1544
|
+
content: [
|
|
1545
|
+
{
|
|
1546
|
+
type: "text",
|
|
1547
|
+
text: statusText
|
|
1548
|
+
}
|
|
1549
|
+
]
|
|
1550
|
+
}
|
|
1551
|
+
};
|
|
1552
|
+
}
|
|
1553
|
+
const isLocal = this.liopServer.listTools().some((t) => t.name === toolName);
|
|
1554
|
+
if (!isLocal && this.meshNode) {
|
|
1555
|
+
let target = this.resolveManifestTarget(toolName);
|
|
1556
|
+
if (!target) {
|
|
1557
|
+
await this.refreshManifestCache();
|
|
1558
|
+
target = this.resolveManifestTarget(toolName);
|
|
1559
|
+
}
|
|
1560
|
+
if (target) {
|
|
1561
|
+
log.info(
|
|
1562
|
+
`[LIOP-Router] Resolved ${toolName} via manifest cache (Peer: ${target.peerId}, Original: ${target.originalToolName})`
|
|
1563
|
+
);
|
|
1564
|
+
const manifestEntry = this.manifestCache.get(target.peerId);
|
|
1565
|
+
let effectiveToken = token;
|
|
1566
|
+
if (manifestEntry?.manifest.authRequired) {
|
|
1567
|
+
const resolvedToken = token || await this.getOrAcquireMeshAgentToken(target.peerId);
|
|
1568
|
+
if (!resolvedToken) {
|
|
1569
|
+
const providerName = manifestEntry.manifest.serverInfo?.name?.toLowerCase() || "unknown";
|
|
1570
|
+
const slug = manifestEntry.manifest.tokenSlug;
|
|
1571
|
+
const shortId = target.peerId.slice(-8).toUpperCase();
|
|
1572
|
+
const primaryVar = slug ? `LIOP_TOKEN_${slug}` : `LIOP_TOKEN_${providerName.toUpperCase().replace(/[^A-Z0-9_]/g, "_")}`;
|
|
1573
|
+
return {
|
|
1574
|
+
jsonrpc: "2.0",
|
|
1575
|
+
id,
|
|
1576
|
+
result: {
|
|
1577
|
+
content: [
|
|
1578
|
+
{
|
|
1579
|
+
type: "text",
|
|
1580
|
+
text: `Authentication Required: The restricted node (${providerName}) requires an access token. Please define the ${primaryVar} or LIOP_TOKEN_${shortId} environment variable on your agent/client host.`
|
|
1581
|
+
}
|
|
1582
|
+
],
|
|
1583
|
+
isError: true
|
|
1584
|
+
}
|
|
1585
|
+
};
|
|
1586
|
+
}
|
|
1587
|
+
effectiveToken = resolvedToken;
|
|
1588
|
+
}
|
|
1589
|
+
return this.routeToRemoteProvider(
|
|
1590
|
+
id,
|
|
1591
|
+
target.originalToolName,
|
|
1592
|
+
target.peerId,
|
|
1593
|
+
params,
|
|
1594
|
+
effectiveToken
|
|
1595
|
+
);
|
|
1596
|
+
}
|
|
1597
|
+
let providers = [];
|
|
1598
|
+
for (let i = 0; i < 3; i++) {
|
|
1599
|
+
providers = await this.meshNode.findProviders(toolName);
|
|
1600
|
+
if (providers.length > 0) break;
|
|
1601
|
+
if (i < 2) await new Promise((r) => setTimeout(r, 1e3));
|
|
1602
|
+
}
|
|
1603
|
+
if (providers.length > 0) {
|
|
1604
|
+
return this.routeToRemoteProvider(
|
|
1605
|
+
id,
|
|
1606
|
+
toolName,
|
|
1607
|
+
providers[0],
|
|
1608
|
+
params,
|
|
1609
|
+
token
|
|
1610
|
+
);
|
|
1611
|
+
}
|
|
1612
|
+
}
|
|
1613
|
+
if (isLocal) {
|
|
1614
|
+
try {
|
|
1615
|
+
const localStartTime = Date.now();
|
|
1616
|
+
const result = await this.liopServer.callTool({
|
|
1617
|
+
name: toolName,
|
|
1618
|
+
arguments: params.arguments || {}
|
|
1619
|
+
});
|
|
1620
|
+
const localTelemetry = TokenTelemetryEngine.getInstance();
|
|
1621
|
+
const localInputPayload = JSON.stringify(params.arguments || {});
|
|
1622
|
+
const localOutputPayload = JSON.stringify(result);
|
|
1623
|
+
localTelemetry.record({
|
|
1624
|
+
type: "tool_call",
|
|
1625
|
+
method: "tools/call",
|
|
1626
|
+
toolName,
|
|
1627
|
+
estimatedInputTokens: localTelemetry.countTokens(localInputPayload),
|
|
1628
|
+
estimatedOutputTokens: localTelemetry.countTokens(localOutputPayload),
|
|
1629
|
+
durationMs: Date.now() - localStartTime
|
|
1630
|
+
});
|
|
1631
|
+
return { jsonrpc: "2.0", id, result };
|
|
1632
|
+
} catch (err) {
|
|
1633
|
+
return {
|
|
1634
|
+
jsonrpc: "2.0",
|
|
1635
|
+
id,
|
|
1636
|
+
error: {
|
|
1637
|
+
code: -32e3,
|
|
1638
|
+
message: err instanceof Error ? err.message : String(err)
|
|
1639
|
+
}
|
|
1640
|
+
};
|
|
1641
|
+
}
|
|
1642
|
+
}
|
|
1643
|
+
return {
|
|
1644
|
+
jsonrpc: "2.0",
|
|
1645
|
+
id,
|
|
1646
|
+
error: {
|
|
1647
|
+
code: -32002,
|
|
1648
|
+
message: `No provider found for tool: ${toolName}. Ensure the provider node is active and connected to the mesh.`
|
|
1649
|
+
}
|
|
1650
|
+
};
|
|
1651
|
+
}
|
|
1652
|
+
async routeToRemoteProvider(id, toolName, peerId, params, token) {
|
|
1653
|
+
if (!this.meshNode)
|
|
1654
|
+
return {
|
|
1655
|
+
jsonrpc: "2.0",
|
|
1656
|
+
id,
|
|
1657
|
+
error: { code: -32603, message: "Mesh Node inactive" }
|
|
1658
|
+
};
|
|
1659
|
+
let manifestEntry = this.manifestCache.get(peerId);
|
|
1660
|
+
let grpcPort = this.defaultRpcPort;
|
|
1661
|
+
if (manifestEntry) {
|
|
1662
|
+
grpcPort = manifestEntry.manifest.grpcPort;
|
|
1663
|
+
} else {
|
|
1664
|
+
const manifest = await this.meshNode.queryManifest(peerId);
|
|
1665
|
+
if (manifest) {
|
|
1666
|
+
grpcPort = manifest.grpcPort;
|
|
1667
|
+
this.manifestCache.set(peerId, {
|
|
1668
|
+
manifest,
|
|
1669
|
+
cachedAt: Date.now()
|
|
1670
|
+
});
|
|
1671
|
+
manifestEntry = this.manifestCache.get(peerId);
|
|
1672
|
+
}
|
|
1673
|
+
}
|
|
1674
|
+
const shouldRemapGrpcPorts = process.env.LIOP_USE_PUBLISHED_GRPC_PORTS === "1" || process.env.LIOP_DOCKER_MAP === "true" || process.env.LIOP_DEV_MODE === "true" || process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test";
|
|
1675
|
+
if (shouldRemapGrpcPorts && manifestEntry) {
|
|
1676
|
+
const providerName = manifestEntry.manifest.serverInfo?.name?.toLowerCase() || "";
|
|
1677
|
+
if (providerName.includes("vault")) grpcPort = 13011;
|
|
1678
|
+
else if (providerName.includes("bank")) grpcPort = 13021;
|
|
1679
|
+
else if (providerName.includes("oracle")) grpcPort = 13031;
|
|
1680
|
+
}
|
|
1681
|
+
const addrs = await this.meshNode.resolvePeer(peerId);
|
|
1682
|
+
let targetAddr = null;
|
|
1683
|
+
const os = await import('os');
|
|
1684
|
+
const localInterfaces = Object.values(os.networkInterfaces()).flat().filter((i) => i?.family === "IPv4").map((i) => i?.address);
|
|
1685
|
+
for (const addr of addrs) {
|
|
1686
|
+
const parts = addr.split("/");
|
|
1687
|
+
const ipIdx = parts.indexOf("ip4");
|
|
1688
|
+
if (ipIdx !== -1) {
|
|
1689
|
+
const advertisedIp = parts[ipIdx + 1];
|
|
1690
|
+
if (advertisedIp === "127.0.0.1" || localInterfaces.includes(advertisedIp)) {
|
|
1691
|
+
targetAddr = `127.0.0.1:${grpcPort}`;
|
|
1692
|
+
break;
|
|
1693
|
+
}
|
|
1694
|
+
if (!targetAddr) {
|
|
1695
|
+
targetAddr = `${advertisedIp}:${grpcPort}`;
|
|
1696
|
+
}
|
|
1697
|
+
}
|
|
1698
|
+
}
|
|
1699
|
+
if (!targetAddr) {
|
|
1700
|
+
targetAddr = `127.0.0.1:${grpcPort}`;
|
|
1701
|
+
}
|
|
1702
|
+
log.info(
|
|
1703
|
+
`[LIOP-Router] Dynamic route: ${toolName} -> ${targetAddr} (PeerID: ${peerId})`
|
|
1704
|
+
);
|
|
1705
|
+
const remoteClient = new liopV1.LogicMesh(
|
|
1706
|
+
targetAddr,
|
|
1707
|
+
createChannelCredentials()
|
|
1708
|
+
);
|
|
1709
|
+
return this.performTranscoding(
|
|
1710
|
+
id,
|
|
1711
|
+
remoteClient,
|
|
1712
|
+
toolName,
|
|
1713
|
+
params,
|
|
1714
|
+
peerId,
|
|
1715
|
+
token
|
|
1716
|
+
);
|
|
1717
|
+
}
|
|
1718
|
+
/** Cached M2M token for dynamic gateway-to-node routing */
|
|
1719
|
+
meshAgentToken;
|
|
1720
|
+
/**
|
|
1721
|
+
* Dynamically acquires an M2M access token from the Nexus Authorization Server.
|
|
1722
|
+
* If peerId is provided, checks if there are node-specific environment tokens
|
|
1723
|
+
* before falling back to the global static token or Nexus acquisition.
|
|
1724
|
+
*/
|
|
1725
|
+
async getOrAcquireMeshAgentToken(peerId) {
|
|
1726
|
+
if (peerId) {
|
|
1727
|
+
const manifestEntry = this.manifestCache.get(peerId);
|
|
1728
|
+
const providerName = manifestEntry?.manifest.serverInfo?.name?.toLowerCase() || "";
|
|
1729
|
+
let nodeToken;
|
|
1730
|
+
const slug = manifestEntry?.manifest.tokenSlug;
|
|
1731
|
+
if (slug) {
|
|
1732
|
+
const envKey = `LIOP_TOKEN_${slug}`;
|
|
1733
|
+
nodeToken = process.env[envKey] || process.env[`LIOP_OAUTH_TOKEN_${slug}`];
|
|
1734
|
+
log.info(
|
|
1735
|
+
`[LIOP-Router] Step0 tokenSlug=${slug} envKey=${envKey} found=${!!nodeToken} peer=${peerId.slice(-8)}`
|
|
1736
|
+
);
|
|
1737
|
+
} else {
|
|
1738
|
+
log.info(
|
|
1739
|
+
`[LIOP-Router] Step0 tokenSlug=MISSING (manifest has no tokenSlug) peer=${peerId.slice(-8)} provider=${providerName}`
|
|
1740
|
+
);
|
|
1741
|
+
}
|
|
1742
|
+
if (!nodeToken) {
|
|
1743
|
+
const shortId = peerId.slice(-8).toUpperCase();
|
|
1744
|
+
nodeToken = process.env[`LIOP_TOKEN_${shortId}`] || process.env[`LIOP_OAUTH_TOKEN_${shortId}`];
|
|
1745
|
+
}
|
|
1746
|
+
if (!nodeToken && providerName) {
|
|
1747
|
+
const cleanName = providerName.toUpperCase().replace(/[^A-Z0-9_]/g, "_");
|
|
1748
|
+
nodeToken = process.env[`LIOP_TOKEN_${cleanName}`] || process.env[`LIOP_OAUTH_TOKEN_${cleanName}`];
|
|
1749
|
+
}
|
|
1750
|
+
if (nodeToken) {
|
|
1751
|
+
log.info(
|
|
1752
|
+
`[LIOP-Router] Resolved node-specific token for peer ${peerId.slice(-8)} (${providerName || "unknown"})`
|
|
1753
|
+
);
|
|
1754
|
+
return nodeToken;
|
|
1755
|
+
}
|
|
1756
|
+
}
|
|
1757
|
+
if (this.meshAgentToken) return this.meshAgentToken;
|
|
1758
|
+
const staticToken = process.env.LIOP_OAUTH_TOKEN || process.env.LIOP_TOKEN;
|
|
1759
|
+
if (staticToken) {
|
|
1760
|
+
this.meshAgentToken = staticToken;
|
|
1761
|
+
return this.meshAgentToken;
|
|
1762
|
+
}
|
|
1763
|
+
const nexusUrl = process.env.LIOP_NEXUS_URL;
|
|
1764
|
+
if (!nexusUrl) return void 0;
|
|
1765
|
+
const clientId = process.env.LIOP_OAUTH_CLIENT_ID || process.env.LIOP_CLIENT_ID || "liop-mesh-agent";
|
|
1766
|
+
const clientSecret = process.env.LIOP_OAUTH_CLIENT_SECRET || process.env.LIOP_CLIENT_SECRET || "dev-secret-change-me";
|
|
1767
|
+
const audience = process.env.LIOP_OAUTH_AUDIENCE || process.env.LIOP_AUDIENCE || "urn:liop:mesh:api";
|
|
1768
|
+
try {
|
|
1769
|
+
const baseUrl = nexusUrl.endsWith("/oidc") ? nexusUrl : `${nexusUrl}/oidc`;
|
|
1770
|
+
const tokenUrl = `${baseUrl}/token`;
|
|
1771
|
+
log.info(
|
|
1772
|
+
`[LIOP-Router] Proactively acquiring M2M token from Nexus: ${tokenUrl}`
|
|
1773
|
+
);
|
|
1774
|
+
const params = new URLSearchParams({
|
|
1775
|
+
grant_type: "client_credentials",
|
|
1776
|
+
scope: "liop:tools:call liop:tools:list liop:resources:read liop:schema:read liop:mesh:query",
|
|
1777
|
+
resource: audience,
|
|
1778
|
+
client_id: clientId,
|
|
1779
|
+
client_secret: clientSecret
|
|
1780
|
+
});
|
|
1781
|
+
const response = await fetch(tokenUrl, {
|
|
1782
|
+
method: "POST",
|
|
1783
|
+
headers: {
|
|
1784
|
+
"Content-Type": "application/x-www-form-urlencoded"
|
|
1785
|
+
},
|
|
1786
|
+
body: params.toString()
|
|
1787
|
+
});
|
|
1788
|
+
if (!response.ok) {
|
|
1789
|
+
const text = await response.text();
|
|
1790
|
+
log.warn(
|
|
1791
|
+
`[LIOP-Router] M2M Token acquisition failed: ${response.status} ${text}`
|
|
1792
|
+
);
|
|
1793
|
+
return void 0;
|
|
1794
|
+
}
|
|
1795
|
+
const data = await response.json();
|
|
1796
|
+
if (data.access_token) {
|
|
1797
|
+
this.meshAgentToken = data.access_token;
|
|
1798
|
+
log.info(
|
|
1799
|
+
"[LIOP-Router] M2M Token acquired successfully for router routing."
|
|
1800
|
+
);
|
|
1801
|
+
return this.meshAgentToken;
|
|
1802
|
+
}
|
|
1803
|
+
} catch (err) {
|
|
1804
|
+
log.warn(
|
|
1805
|
+
`[LIOP-Router] Failed to acquire M2M token: ${err instanceof Error ? err.message : String(err)}`
|
|
1806
|
+
);
|
|
1807
|
+
}
|
|
1808
|
+
return void 0;
|
|
1809
|
+
}
|
|
1810
|
+
async performTranscoding(id, client, toolName, params, peerId, token) {
|
|
1811
|
+
const capabilityHash = toolName;
|
|
1812
|
+
const proofOfIntent = this.meshNode ? await this.meshNode.sign(Buffer.from(capabilityHash)) : Buffer.from([]);
|
|
1813
|
+
const transcodingStartTime = Date.now();
|
|
1814
|
+
let activeToken = token;
|
|
1815
|
+
if (!activeToken) {
|
|
1816
|
+
activeToken = await this.getOrAcquireMeshAgentToken(peerId);
|
|
1817
|
+
}
|
|
1818
|
+
if (peerId) {
|
|
1819
|
+
const manifestEntry = this.manifestCache.get(peerId);
|
|
1820
|
+
if (manifestEntry?.manifest.authRequired && !activeToken) {
|
|
1821
|
+
const providerName = manifestEntry.manifest.serverInfo?.name?.toLowerCase() || "unknown";
|
|
1822
|
+
const slug = manifestEntry.manifest.tokenSlug;
|
|
1823
|
+
const shortId = peerId.slice(-8).toUpperCase();
|
|
1824
|
+
const primaryVar = slug ? `LIOP_TOKEN_${slug}` : `LIOP_TOKEN_${providerName.toUpperCase().replace(/[^A-Z0-9_]/g, "_")}`;
|
|
1825
|
+
return {
|
|
1826
|
+
jsonrpc: "2.0",
|
|
1827
|
+
id,
|
|
1828
|
+
result: {
|
|
1829
|
+
content: [
|
|
1830
|
+
{
|
|
1831
|
+
type: "text",
|
|
1832
|
+
text: `Authentication Required: The restricted node (${providerName}) requires an access token. Please define the ${primaryVar} or LIOP_TOKEN_${shortId} environment variable on your agent/client host.`
|
|
1833
|
+
}
|
|
1834
|
+
],
|
|
1835
|
+
isError: true
|
|
1836
|
+
}
|
|
1837
|
+
};
|
|
1838
|
+
}
|
|
1839
|
+
}
|
|
1840
|
+
return new Promise((resolve) => {
|
|
1841
|
+
const metadata = new grpc.Metadata();
|
|
1842
|
+
if (activeToken) {
|
|
1843
|
+
metadata.add("authorization", `Bearer ${activeToken}`);
|
|
1844
|
+
}
|
|
1845
|
+
client.negotiateIntent(
|
|
1846
|
+
{
|
|
1847
|
+
agent_did: `did:liop:${this.meshNode?.getPeerId() || "mcp-proxy"}`,
|
|
1848
|
+
capability_hash: capabilityHash,
|
|
1849
|
+
proof_of_intent: proofOfIntent
|
|
1850
|
+
},
|
|
1851
|
+
metadata,
|
|
1852
|
+
async (err, response) => {
|
|
1853
|
+
if (err || !response.accepted) {
|
|
1854
|
+
return resolve({
|
|
1855
|
+
jsonrpc: "2.0",
|
|
1856
|
+
id,
|
|
1857
|
+
result: {
|
|
1858
|
+
content: [
|
|
1859
|
+
{
|
|
1860
|
+
type: "text",
|
|
1861
|
+
text: `PQC Handshake Failed: ${err?.message || "Rejected"}`
|
|
1862
|
+
}
|
|
1863
|
+
],
|
|
1864
|
+
isError: true
|
|
1865
|
+
}
|
|
1866
|
+
});
|
|
1867
|
+
}
|
|
1868
|
+
const { ciphertext, sharedSecret } = await Kyber768Wrapper.encapsulateAsymmetric(
|
|
1869
|
+
response.kyber_public_key
|
|
1870
|
+
);
|
|
1871
|
+
const embeddedArgsJson = JSON.stringify(params.arguments || {});
|
|
1872
|
+
const proxyLogic = `return { "__liop_proxy_tool": "${toolName}", "__liop_proxy_args": ${embeddedArgsJson} };`;
|
|
1873
|
+
const nonce = crypto2.randomBytes(12);
|
|
1874
|
+
const sealedLogic = this.encryptWithNonce(
|
|
1875
|
+
Buffer.from(proxyLogic),
|
|
1876
|
+
sharedSecret,
|
|
1877
|
+
nonce
|
|
1878
|
+
);
|
|
1879
|
+
const metadataCall = new grpc.Metadata();
|
|
1880
|
+
if (activeToken) {
|
|
1881
|
+
metadataCall.add("authorization", `Bearer ${activeToken}`);
|
|
1882
|
+
}
|
|
1883
|
+
const call = client.executeLogic(
|
|
1884
|
+
{
|
|
1885
|
+
session_token: response.session_token,
|
|
1886
|
+
wasm_binary: new Uint8Array(sealedLogic),
|
|
1887
|
+
inputs: {},
|
|
1888
|
+
pqc_ciphertext: ciphertext,
|
|
1889
|
+
aes_nonce: nonce
|
|
1890
|
+
},
|
|
1891
|
+
metadataCall
|
|
1892
|
+
);
|
|
1893
|
+
let resultBody = "";
|
|
1894
|
+
let lastResponse = null;
|
|
1895
|
+
call.on("data", (grpcRes) => {
|
|
1896
|
+
resultBody += grpcRes.semantic_evidence;
|
|
1897
|
+
lastResponse = grpcRes;
|
|
1898
|
+
});
|
|
1899
|
+
call.on("end", async () => {
|
|
1900
|
+
try {
|
|
1901
|
+
if (lastResponse) {
|
|
1902
|
+
if (!lastResponse.is_error) {
|
|
1903
|
+
const proofHex = Buffer.from(
|
|
1904
|
+
lastResponse.cryptographic_proof
|
|
1905
|
+
).toString("hex");
|
|
1906
|
+
const isValid = await this.verifier.verifyZkReceipt(
|
|
1907
|
+
Buffer.from(proxyLogic),
|
|
1908
|
+
proofHex,
|
|
1909
|
+
Buffer.from(lastResponse.zk_receipt),
|
|
1910
|
+
Buffer.from(sharedSecret),
|
|
1911
|
+
resultBody
|
|
1912
|
+
);
|
|
1913
|
+
if (!isValid) {
|
|
1914
|
+
return resolve({
|
|
1915
|
+
jsonrpc: "2.0",
|
|
1916
|
+
id,
|
|
1917
|
+
result: {
|
|
1918
|
+
content: [
|
|
1919
|
+
{
|
|
1920
|
+
type: "text",
|
|
1921
|
+
text: "SECURITY ALERT: Remote response failed cryptographic integrity audit."
|
|
1922
|
+
}
|
|
1923
|
+
],
|
|
1924
|
+
isError: true
|
|
1925
|
+
}
|
|
1926
|
+
});
|
|
1927
|
+
}
|
|
1928
|
+
}
|
|
1929
|
+
}
|
|
1930
|
+
const parsedResult = JSON.parse(resultBody);
|
|
1931
|
+
const remoteTelemetry = TokenTelemetryEngine.getInstance();
|
|
1932
|
+
remoteTelemetry.record({
|
|
1933
|
+
type: "tool_call",
|
|
1934
|
+
method: "tools/call",
|
|
1935
|
+
toolName,
|
|
1936
|
+
peerId,
|
|
1937
|
+
estimatedInputTokens: remoteTelemetry.countTokens(embeddedArgsJson),
|
|
1938
|
+
estimatedOutputTokens: remoteTelemetry.countTokens(resultBody),
|
|
1939
|
+
durationMs: Date.now() - transcodingStartTime
|
|
1940
|
+
});
|
|
1941
|
+
resolve({ jsonrpc: "2.0", id, result: parsedResult });
|
|
1942
|
+
} catch (_e) {
|
|
1943
|
+
resolve({
|
|
1944
|
+
jsonrpc: "2.0",
|
|
1945
|
+
id,
|
|
1946
|
+
result: { content: [{ type: "text", text: resultBody }] }
|
|
1947
|
+
});
|
|
1948
|
+
}
|
|
1949
|
+
});
|
|
1950
|
+
call.on(
|
|
1951
|
+
"error",
|
|
1952
|
+
(e) => resolve({
|
|
1953
|
+
jsonrpc: "2.0",
|
|
1954
|
+
id,
|
|
1955
|
+
result: {
|
|
1956
|
+
content: [
|
|
1957
|
+
{ type: "text", text: `LIOP gRPC Error: ${e.message}` }
|
|
1958
|
+
],
|
|
1959
|
+
isError: true
|
|
1960
|
+
}
|
|
1961
|
+
})
|
|
1962
|
+
);
|
|
1963
|
+
}
|
|
1964
|
+
);
|
|
1965
|
+
});
|
|
1966
|
+
}
|
|
1967
|
+
encryptWithNonce(payload, key, nonce) {
|
|
1968
|
+
const cipher = crypto2.createCipheriv("aes-256-gcm", key, nonce);
|
|
1969
|
+
const encrypted = Buffer.concat([cipher.update(payload), cipher.final()]);
|
|
1970
|
+
return Buffer.concat([encrypted, cipher.getAuthTag()]);
|
|
1971
|
+
}
|
|
1972
|
+
};
|
|
1973
|
+
|
|
1974
|
+
export { HeuristicTokenEstimator, LiopMcpRouter, LiopOTelBridge, RealTokenEstimator, TokenTelemetryEngine, createSyncTokenEstimator, createTokenEstimator };
|
|
1975
|
+
//# sourceMappingURL=chunk-HB5DXX3Q.js.map
|
|
1976
|
+
//# sourceMappingURL=chunk-HB5DXX3Q.js.map
|