@nekzus/liop 2.0.0-alpha.1 → 2.0.0-alpha.3
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 -20
- package/dist/bin/agent.d.ts +0 -1
- package/dist/bin/agent.js +5 -306
- package/dist/bin/agent.js.map +1 -0
- package/dist/{bridge/stream.d.ts → bridge.d.ts} +44 -3
- package/dist/bridge.js +2 -0
- package/dist/bridge.js.map +1 -0
- package/dist/chunk-7MAGL6ON.js +33 -0
- package/dist/chunk-7MAGL6ON.js.map +1 -0
- package/dist/chunk-ANFXJGMP.js +2 -0
- package/dist/chunk-ANFXJGMP.js.map +1 -0
- package/dist/chunk-DBXGYHKY.js +2 -0
- package/dist/chunk-DBXGYHKY.js.map +1 -0
- package/dist/chunk-FW6CICSY.js +29 -0
- package/dist/chunk-FW6CICSY.js.map +1 -0
- package/dist/chunk-HM77MWB6.js +2 -0
- package/dist/chunk-HM77MWB6.js.map +1 -0
- package/dist/chunk-HNDVAKEK.js +24 -0
- package/dist/chunk-HNDVAKEK.js.map +1 -0
- package/dist/chunk-HQZHZM6U.js +2 -0
- package/dist/chunk-HQZHZM6U.js.map +1 -0
- package/dist/chunk-JBMEAXYU.js +13 -0
- package/dist/chunk-JBMEAXYU.js.map +1 -0
- package/dist/chunk-LYULZHZO.js +3 -0
- package/dist/chunk-LYULZHZO.js.map +1 -0
- package/dist/chunk-P52IE4L6.js +2 -0
- package/dist/chunk-P52IE4L6.js.map +1 -0
- package/dist/chunk-PPCOS2NU.js +2 -0
- package/dist/chunk-PPCOS2NU.js.map +1 -0
- package/dist/chunk-RWRRBYG4.js +2 -0
- package/dist/chunk-RWRRBYG4.js.map +1 -0
- package/dist/chunk-S6RJHZV2.js +2 -0
- package/dist/chunk-S6RJHZV2.js.map +1 -0
- package/dist/chunk-UVTEJYHN.js +2 -0
- package/dist/chunk-UVTEJYHN.js.map +1 -0
- package/dist/client.d.ts +5 -0
- package/dist/client.js +2 -0
- package/dist/client.js.map +1 -0
- package/dist/{gateway/router.d.ts → gateway.d.ts} +30 -5
- package/dist/gateway.js +2 -0
- package/dist/gateway.js.map +1 -0
- package/dist/{client/index.d.ts → index-CyxNLlz7.d.ts} +24 -5
- package/dist/index.d.ts +313 -12
- package/dist/index.js +31 -12
- package/dist/index.js.map +1 -0
- package/dist/kyber-2WDOTUQX.js +2 -0
- package/dist/kyber-2WDOTUQX.js.map +1 -0
- package/dist/{mesh/node.d.ts → mesh.d.ts} +5 -3
- package/dist/mesh.js +2 -0
- package/dist/mesh.js.map +1 -0
- package/dist/{server/index.d.ts → server.d.ts} +125 -12
- package/dist/server.js +2 -0
- package/dist/server.js.map +1 -0
- package/dist/types.d.ts +17 -14
- package/dist/types.js +2 -26
- package/dist/types.js.map +1 -0
- package/dist/{crypto/verifier.d.ts → verifier-DTCD9imJ.d.ts} +3 -1
- package/dist/verifier-RQRYXA4C.js +2 -0
- package/dist/verifier-RQRYXA4C.js.map +1 -0
- package/dist/workers/logic-execution.d.ts +4 -2
- package/dist/workers/logic-execution.js +2 -123
- package/dist/workers/logic-execution.js.map +1 -0
- package/dist/workers/zk-verifier.d.ts +4 -2
- package/dist/workers/zk-verifier.js +2 -98
- package/dist/workers/zk-verifier.js.map +1 -0
- package/package.json +32 -19
- package/dist/bridge/index.d.ts +0 -37
- package/dist/bridge/index.js +0 -249
- package/dist/bridge/stream.js +0 -210
- package/dist/client/index.js +0 -275
- package/dist/crypto/logic-image-id.d.ts +0 -3
- package/dist/crypto/logic-image-id.js +0 -27
- package/dist/crypto/verifier.js +0 -97
- package/dist/economy/estimator.d.ts +0 -53
- package/dist/economy/estimator.js +0 -69
- package/dist/economy/index.d.ts +0 -5
- package/dist/economy/index.js +0 -3
- package/dist/economy/otel.d.ts +0 -38
- package/dist/economy/otel.js +0 -100
- package/dist/economy/telemetry.d.ts +0 -77
- package/dist/economy/telemetry.js +0 -224
- package/dist/errors.d.ts +0 -14
- package/dist/errors.js +0 -19
- package/dist/gateway/hybrid.d.ts +0 -23
- package/dist/gateway/hybrid.js +0 -199
- package/dist/gateway/router.js +0 -1054
- package/dist/mesh/index.d.ts +0 -1
- package/dist/mesh/index.js +0 -1
- package/dist/mesh/node.js +0 -853
- package/dist/prompts/adapters.d.ts +0 -16
- package/dist/prompts/adapters.js +0 -55
- package/dist/rpc/client.d.ts +0 -22
- package/dist/rpc/client.js +0 -40
- package/dist/rpc/codec/lpm.d.ts +0 -20
- package/dist/rpc/codec/lpm.js +0 -36
- package/dist/rpc/crypto/aes.d.ts +0 -22
- package/dist/rpc/crypto/aes.js +0 -47
- package/dist/rpc/crypto/kyber.d.ts +0 -27
- package/dist/rpc/crypto/kyber.js +0 -70
- package/dist/rpc/proto.d.ts +0 -2
- package/dist/rpc/proto.js +0 -33
- package/dist/rpc/server.d.ts +0 -13
- package/dist/rpc/server.js +0 -50
- package/dist/rpc/tls.d.ts +0 -26
- package/dist/rpc/tls.js +0 -54
- package/dist/rpc/types.d.ts +0 -28
- package/dist/rpc/types.js +0 -5
- package/dist/sandbox/guardian.d.ts +0 -18
- package/dist/sandbox/guardian.js +0 -58
- package/dist/sandbox/wasi.d.ts +0 -36
- package/dist/sandbox/wasi.js +0 -233
- package/dist/security/guardian.d.ts +0 -22
- package/dist/security/guardian.js +0 -52
- package/dist/security/zk.d.ts +0 -37
- package/dist/security/zk.js +0 -76
- package/dist/server/index.js +0 -1047
- package/dist/server/ner-scanner.d.ts +0 -29
- package/dist/server/ner-scanner.js +0 -141
- package/dist/server/pii.d.ts +0 -66
- package/dist/server/pii.js +0 -428
- package/dist/utils/logger.d.ts +0 -21
- package/dist/utils/logger.js +0 -70
- package/dist/utils/mcpCompact.d.ts +0 -11
- package/dist/utils/mcpCompact.js +0 -29
package/dist/client/index.js
DELETED
|
@@ -1,275 +0,0 @@
|
|
|
1
|
-
import { LiopVerifier } from "../crypto/verifier.js";
|
|
2
|
-
import { MeshNode, } from "../mesh/node.js";
|
|
3
|
-
import { LiopRpcClient } from "../rpc/client.js";
|
|
4
|
-
import { AesGcmWrapper } from "../rpc/crypto/aes.js";
|
|
5
|
-
import { Kyber768Wrapper } from "../rpc/crypto/kyber.js";
|
|
6
|
-
import { log } from "../utils/logger.js";
|
|
7
|
-
/**
|
|
8
|
-
* LIOP Client
|
|
9
|
-
* High-level orchestration for discovery and execution in the Logic-Injection-on-Origin mesh.
|
|
10
|
-
*/
|
|
11
|
-
export class LiopClient {
|
|
12
|
-
meshNode = null;
|
|
13
|
-
rpcClients = new Map();
|
|
14
|
-
manifests = new Map();
|
|
15
|
-
tlsOptions;
|
|
16
|
-
serverInfo;
|
|
17
|
-
verifier = new LiopVerifier();
|
|
18
|
-
constructor(tls) {
|
|
19
|
-
this.tlsOptions = tls;
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Discovers and connects to the target server or mesh capability.
|
|
23
|
-
* If address is omitted, it sets up the MeshNode to act purely dynamically.
|
|
24
|
-
*/
|
|
25
|
-
async connect(address, options) {
|
|
26
|
-
this.meshNode = new MeshNode(options?.meshConfig);
|
|
27
|
-
await this.meshNode.start();
|
|
28
|
-
log.info(`[LiopClient] Mesh Node synchronized. PeerID: ${this.meshNode.getPeerId()}`);
|
|
29
|
-
if (address) {
|
|
30
|
-
this.rpcClients.set("static", new LiopRpcClient(address, this.tlsOptions));
|
|
31
|
-
this.serverInfo = { name: `LiopServer (${address})`, version: "1.0.0" };
|
|
32
|
-
log.info(`[LiopClient] Static gRPC configured for: ${address}`);
|
|
33
|
-
}
|
|
34
|
-
else {
|
|
35
|
-
this.serverInfo = { name: "LiopServer (Mesh Alpha)", version: "1.0.0" };
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Dynamically queries Kademlia DHT to find the optimal PeerID providing the Capability
|
|
40
|
-
* and returns the physical gRPC target (host:port) resolved from the provider's manifest.
|
|
41
|
-
*/
|
|
42
|
-
async resolveCapability(toolName) {
|
|
43
|
-
if (!this.meshNode)
|
|
44
|
-
throw new Error("Client must be connected to Mesh to resolve capabilities.");
|
|
45
|
-
log.info(`[LiopClient] Querying Mesh DHT for Provider: ${toolName}...`);
|
|
46
|
-
const providers = await this.meshNode.findProviders(toolName);
|
|
47
|
-
if (providers.length === 0) {
|
|
48
|
-
throw new Error(`Kademlia DHT found zero providers for capability: ${toolName}`);
|
|
49
|
-
}
|
|
50
|
-
const providerId = providers[0];
|
|
51
|
-
log.info(`[LiopClient] Identified Alpha Provider PeerID: ${providerId}`);
|
|
52
|
-
let grpcPort = 50051;
|
|
53
|
-
const manifest = await this.meshNode.queryManifest(providerId);
|
|
54
|
-
if (manifest) {
|
|
55
|
-
grpcPort = manifest.grpcPort;
|
|
56
|
-
log.info(`[LiopClient] Manifest resolved: gRPC port ${grpcPort}`);
|
|
57
|
-
}
|
|
58
|
-
const addrs = await this.meshNode.resolvePeer(providerId);
|
|
59
|
-
for (const maddr of addrs) {
|
|
60
|
-
const parts = maddr.split("/");
|
|
61
|
-
if (parts[1] === "ip4") {
|
|
62
|
-
const grpcHost = `${parts[2]}:${grpcPort}`;
|
|
63
|
-
log.info(`[LiopClient] Translated Multiaddr to gRPC Target: ${grpcHost}`);
|
|
64
|
-
return grpcHost;
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
return `127.0.0.1:${grpcPort}`;
|
|
68
|
-
}
|
|
69
|
-
/**
|
|
70
|
-
* Discovers remote capabilities via the LIOP Manifest Protocol.
|
|
71
|
-
*/
|
|
72
|
-
async discoverTools() {
|
|
73
|
-
if (!this.meshNode) {
|
|
74
|
-
throw new Error("Client must be connected before discovering tools.");
|
|
75
|
-
}
|
|
76
|
-
log.info(`[LiopClient] Discovery started...`);
|
|
77
|
-
const providerIds = await this.meshNode.discoverManifestProviders();
|
|
78
|
-
const tools = [];
|
|
79
|
-
const seenNames = new Set();
|
|
80
|
-
for (const peerId of providerIds) {
|
|
81
|
-
try {
|
|
82
|
-
log.info(`[LiopClient] Querying manifest from: ${peerId}`);
|
|
83
|
-
const manifest = await this.meshNode.queryManifest(peerId);
|
|
84
|
-
if (manifest) {
|
|
85
|
-
this.manifests.set(peerId, manifest);
|
|
86
|
-
for (const tool of manifest.tools) {
|
|
87
|
-
if (!seenNames.has(tool.name)) {
|
|
88
|
-
tools.push({ name: tool.name, description: tool.description });
|
|
89
|
-
seenNames.add(tool.name);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
catch (err) {
|
|
95
|
-
log.info(`[LiopClient] Error querying manifest from ${peerId}:`, err instanceof Error ? err.message : String(err));
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
log.info(`[LiopClient] Discovery finished. Found ${tools.length} unique tools.`);
|
|
99
|
-
return tools;
|
|
100
|
-
}
|
|
101
|
-
/**
|
|
102
|
-
* Invokes a tool.
|
|
103
|
-
*/
|
|
104
|
-
async callTool(request, _wasmPayload) {
|
|
105
|
-
if (!this.meshNode) {
|
|
106
|
-
throw new Error("Client must be connected before calling tools.");
|
|
107
|
-
}
|
|
108
|
-
const toolName = request.name;
|
|
109
|
-
log.info(`[LiopClient] Resolving Tool: ${toolName}`);
|
|
110
|
-
// [ALPHA-FIX] Bypass DHT discovery if we are already statically connected to a provider (Enterprise/Test mode)
|
|
111
|
-
let rpcClient = this.rpcClients.get("static");
|
|
112
|
-
if (!rpcClient) {
|
|
113
|
-
const dynamicAddress = await this.resolveCapability(toolName);
|
|
114
|
-
rpcClient = this.getOrCreateRpcClient(toolName, dynamicAddress);
|
|
115
|
-
}
|
|
116
|
-
else {
|
|
117
|
-
log.info(`[LiopClient] Using existing static gRPC connection for ${toolName}.`);
|
|
118
|
-
}
|
|
119
|
-
log.info(`[LiopClient] Negotiating intent for ${toolName}...`);
|
|
120
|
-
const agentDid = this.meshNode
|
|
121
|
-
? `did:liop:${this.meshNode.getPeerId()}`
|
|
122
|
-
: "did:liop:ephemeral";
|
|
123
|
-
const intentPayload = Buffer.from(`${toolName}:${Date.now()}`);
|
|
124
|
-
const proofOfIntent = this.meshNode
|
|
125
|
-
? await this.meshNode.sign(intentPayload)
|
|
126
|
-
: intentPayload;
|
|
127
|
-
const intentResponse = (await rpcClient.negotiateIntent({
|
|
128
|
-
agent_did: agentDid,
|
|
129
|
-
capability_hash: toolName,
|
|
130
|
-
proof_of_intent: proofOfIntent,
|
|
131
|
-
}));
|
|
132
|
-
if (!intentResponse.accepted) {
|
|
133
|
-
throw new Error(`Intent denied by host: ${intentResponse.error_message}`);
|
|
134
|
-
}
|
|
135
|
-
// LIOP Robust Field Extraction (Supports both snake_case and camelCase via gRPC-JS)
|
|
136
|
-
const publicKey = intentResponse.kyber_public_key || intentResponse.kyberPublicKey;
|
|
137
|
-
const sessionToken = intentResponse.session_token || intentResponse.sessionToken;
|
|
138
|
-
if (!publicKey) {
|
|
139
|
-
log.info("[LiopClient] Critical Error: Kyber Public Key not found in IntentResponse.", intentResponse);
|
|
140
|
-
throw new Error("Handshake failed: Remote host did not provide a valid Kyber Public Key.");
|
|
141
|
-
}
|
|
142
|
-
// 2. Post-Quantum Encapsulation (ML-KEM-768)
|
|
143
|
-
log.info(`[LiopClient] Encapsulating Post-Quantum Shared Secret for ${request.name}...`);
|
|
144
|
-
const { ciphertext: kyberCiphertext, sharedSecret } = await Kyber768Wrapper.encapsulateAsymmetric(publicKey);
|
|
145
|
-
// 3. Symmetric Sealing (AES-256-GCM)
|
|
146
|
-
log.info(`[LiopClient] Sealing WASM Payload and Inputs...`);
|
|
147
|
-
const _safePayload = _wasmPayload || Buffer.from("");
|
|
148
|
-
// Encrypt WASM binary
|
|
149
|
-
const { ciphertext: encryptedWasm, nonce: aesNonce } = AesGcmWrapper.encryptPayload(_safePayload, sharedSecret);
|
|
150
|
-
// Encrypt inputs using a fresh random nonce per input to prevent AES-GCM nonce reuse
|
|
151
|
-
const encryptedInputs = {};
|
|
152
|
-
const crypto = await import("node:crypto");
|
|
153
|
-
for (const [key, value] of Object.entries(request.arguments || {})) {
|
|
154
|
-
const inputNonce = crypto.randomBytes(12);
|
|
155
|
-
const cipher = crypto.createCipheriv("aes-256-gcm", sharedSecret, inputNonce);
|
|
156
|
-
const encrypted = Buffer.concat([
|
|
157
|
-
cipher.update(JSON.stringify(value)),
|
|
158
|
-
cipher.final(),
|
|
159
|
-
]);
|
|
160
|
-
const authTag = cipher.getAuthTag();
|
|
161
|
-
// Prepend the 12-byte nonce to the ciphertext
|
|
162
|
-
encryptedInputs[key] = Buffer.concat([inputNonce, encrypted, authTag]);
|
|
163
|
-
}
|
|
164
|
-
// 4. Assemble and Execute gRPC LogicRequest
|
|
165
|
-
const logicRequest = {
|
|
166
|
-
session_token: sessionToken,
|
|
167
|
-
wasm_binary: encryptedWasm,
|
|
168
|
-
inputs: encryptedInputs,
|
|
169
|
-
pqc_ciphertext: kyberCiphertext,
|
|
170
|
-
aes_nonce: aesNonce,
|
|
171
|
-
};
|
|
172
|
-
return new Promise((resolve, reject) => {
|
|
173
|
-
const stream = rpcClient.executeLogic(logicRequest);
|
|
174
|
-
if (!stream) {
|
|
175
|
-
reject(new Error("RPC Client unavailable or failed to create stream."));
|
|
176
|
-
return;
|
|
177
|
-
}
|
|
178
|
-
let resultFulfilled = false;
|
|
179
|
-
let hasReceivedData = false;
|
|
180
|
-
stream.on("data", async (response) => {
|
|
181
|
-
if (resultFulfilled)
|
|
182
|
-
return;
|
|
183
|
-
hasReceivedData = true;
|
|
184
|
-
log.info("[LiopClient] Logic Executed. Verification in progress...");
|
|
185
|
-
try {
|
|
186
|
-
const isValid = await this.verifier.verifyZkReceipt(_safePayload, Buffer.from(response.cryptographic_proof).toString("hex"), Buffer.from(response.zk_receipt), Buffer.from(sharedSecret));
|
|
187
|
-
if (!isValid) {
|
|
188
|
-
reject(new Error("PROTOCOL INTEGRITY VIOLATION: ZK-Receipt verification failed."));
|
|
189
|
-
return;
|
|
190
|
-
}
|
|
191
|
-
resultFulfilled = true;
|
|
192
|
-
resolve({
|
|
193
|
-
content: [
|
|
194
|
-
{
|
|
195
|
-
type: "text",
|
|
196
|
-
text: response.semantic_evidence,
|
|
197
|
-
},
|
|
198
|
-
],
|
|
199
|
-
isError: response.is_error,
|
|
200
|
-
});
|
|
201
|
-
}
|
|
202
|
-
catch (err) {
|
|
203
|
-
reject(err);
|
|
204
|
-
}
|
|
205
|
-
});
|
|
206
|
-
stream.on("error", (err) => {
|
|
207
|
-
if (resultFulfilled)
|
|
208
|
-
return;
|
|
209
|
-
log.error("[LiopClient] Stream Error:", err);
|
|
210
|
-
reject(err);
|
|
211
|
-
});
|
|
212
|
-
stream.on("end", () => {
|
|
213
|
-
// We don't throw here if we already received a response block that is currently
|
|
214
|
-
// undergoing ZK Verification in the Piscina worker pool.
|
|
215
|
-
if (!hasReceivedData && !resultFulfilled) {
|
|
216
|
-
reject(new Error("Logic-on-Origin stream closed without results."));
|
|
217
|
-
}
|
|
218
|
-
});
|
|
219
|
-
});
|
|
220
|
-
}
|
|
221
|
-
getOrCreateRpcClient(peerId, address) {
|
|
222
|
-
let client = this.rpcClients.get(peerId);
|
|
223
|
-
if (!client) {
|
|
224
|
-
client = new LiopRpcClient(address, this.tlsOptions);
|
|
225
|
-
this.rpcClients.set(peerId, client);
|
|
226
|
-
}
|
|
227
|
-
return client;
|
|
228
|
-
}
|
|
229
|
-
/**
|
|
230
|
-
* Reads a specific resource by URI.
|
|
231
|
-
* In LIOP, resources can be static definitions or dynamic streams.
|
|
232
|
-
*/
|
|
233
|
-
async readResource(uri) {
|
|
234
|
-
if (!this.meshNode) {
|
|
235
|
-
throw new Error("Client must be connected before reading resources.");
|
|
236
|
-
}
|
|
237
|
-
log.info(`[LiopClient] Querying Mesh for Resource: ${uri}...`);
|
|
238
|
-
// We search for the peer hosting the resource in the P2P Mesh
|
|
239
|
-
const providers = await this.meshNode.findProviders(uri);
|
|
240
|
-
if (providers.length === 0) {
|
|
241
|
-
throw new Error(`No mesh providers found for resource: ${uri}`);
|
|
242
|
-
}
|
|
243
|
-
// Query the remote peer's manifest
|
|
244
|
-
const manifest = await this.meshNode.queryManifest(providers[0]);
|
|
245
|
-
if (!manifest) {
|
|
246
|
-
throw new Error("Target peer did not return a valid LIOP Manifest.");
|
|
247
|
-
}
|
|
248
|
-
// Locate the exact resource metadata
|
|
249
|
-
const resourceDef = manifest.resources?.find((r) => r.uri === uri);
|
|
250
|
-
if (!resourceDef) {
|
|
251
|
-
throw new Error(`Resource ${uri} not listed in remote manifest.`);
|
|
252
|
-
}
|
|
253
|
-
// Return the declarative metadata (Logic-Injection is required for actual data extraction)
|
|
254
|
-
return {
|
|
255
|
-
contents: [
|
|
256
|
-
{
|
|
257
|
-
uri,
|
|
258
|
-
mimeType: resourceDef.mimeType || "application/json",
|
|
259
|
-
text: JSON.stringify(resourceDef, null, 2),
|
|
260
|
-
},
|
|
261
|
-
],
|
|
262
|
-
};
|
|
263
|
-
}
|
|
264
|
-
getServerInfo() {
|
|
265
|
-
return this.serverInfo;
|
|
266
|
-
}
|
|
267
|
-
/**
|
|
268
|
-
* Destroys the active Mesh Node resources.
|
|
269
|
-
*/
|
|
270
|
-
async close() {
|
|
271
|
-
if (this.meshNode) {
|
|
272
|
-
await this.meshNode.stop();
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
}
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import crypto from "node:crypto";
|
|
2
|
-
/**
|
|
3
|
-
* Top-level LIOP v1 envelope only. Must NOT use multiline (^/$) mode:
|
|
4
|
-
* proxy logic embeds a full envelope inside JSON strings; `^` per line would
|
|
5
|
-
* incorrectly treat that as the document root and desync ImageID vs the worker.
|
|
6
|
-
*/
|
|
7
|
-
const TOP_LEVEL_ENVELOPE = /^\s*@LIOP\{[^}]+\}\n?([\s\S]*?)\n?@END\s*$/;
|
|
8
|
-
export function normalizeLogicSource(logicUtf8) {
|
|
9
|
-
const match = logicUtf8.match(TOP_LEVEL_ENVELOPE);
|
|
10
|
-
if (match?.[1] !== undefined) {
|
|
11
|
-
return match[1].trim();
|
|
12
|
-
}
|
|
13
|
-
return logicUtf8.trim();
|
|
14
|
-
}
|
|
15
|
-
/** SHA-256 digest of logic bytes (WASM raw; JS UTF-8 after top-level envelope strip). */
|
|
16
|
-
export function deriveLogicImageDigest(logicPayload) {
|
|
17
|
-
const isWasm = logicPayload[0] === 0x00 && logicPayload[1] === 0x61;
|
|
18
|
-
if (isWasm) {
|
|
19
|
-
return crypto.createHash("sha256").update(logicPayload).digest();
|
|
20
|
-
}
|
|
21
|
-
const text = Buffer.from(logicPayload).toString("utf-8");
|
|
22
|
-
const normalized = normalizeLogicSource(text);
|
|
23
|
-
return crypto
|
|
24
|
-
.createHash("sha256")
|
|
25
|
-
.update(Buffer.from(normalized, "utf-8"))
|
|
26
|
-
.digest();
|
|
27
|
-
}
|
package/dist/crypto/verifier.js
DELETED
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
import { createRequire } from "node:module";
|
|
2
|
-
import path from "node:path";
|
|
3
|
-
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
4
|
-
import { Piscina } from "piscina";
|
|
5
|
-
import { log } from "../utils/logger.js";
|
|
6
|
-
import { deriveLogicImageDigest } from "./logic-image-id.js";
|
|
7
|
-
const __filename = fileURLToPath(import.meta.url);
|
|
8
|
-
const __dirname = path.dirname(__filename);
|
|
9
|
-
/**
|
|
10
|
-
* LIOP Tier-0 Industrial Verifier
|
|
11
|
-
*
|
|
12
|
-
* This engine is responsible for the trustless verification of remote logic execution.
|
|
13
|
-
* It validates both the integrity of the code (ZkImageID) and the mathematical proof
|
|
14
|
-
* of its execution (ZkSeal), as well as hardware-level attestation (TEE).
|
|
15
|
-
*/
|
|
16
|
-
export class LiopVerifier {
|
|
17
|
-
// Singleton Worker Pool for heavy ZK verification
|
|
18
|
-
static zkWorkerPool = null;
|
|
19
|
-
getZkPool() {
|
|
20
|
-
if (!LiopVerifier.zkWorkerPool) {
|
|
21
|
-
const isTS = import.meta.url.endsWith(".ts");
|
|
22
|
-
const workerExt = isTS ? ".ts" : ".js";
|
|
23
|
-
let execArgv = [];
|
|
24
|
-
if (isTS) {
|
|
25
|
-
try {
|
|
26
|
-
const req = createRequire(import.meta.url);
|
|
27
|
-
const tsxPkg = req.resolve("tsx/package.json");
|
|
28
|
-
const absoluteTsx = pathToFileURL(path.join(path.dirname(tsxPkg), "dist", "loader.mjs")).href;
|
|
29
|
-
execArgv = ["--import", absoluteTsx];
|
|
30
|
-
}
|
|
31
|
-
catch (_e) {
|
|
32
|
-
execArgv = ["--import", "tsx"];
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
LiopVerifier.zkWorkerPool = new Piscina({
|
|
36
|
-
filename: path.resolve(__dirname, `../workers/zk-verifier${workerExt}`),
|
|
37
|
-
minThreads: 1,
|
|
38
|
-
maxThreads: 2, // Minimal footprint since verification is fast compared to generation
|
|
39
|
-
idleTimeout: 30000,
|
|
40
|
-
execArgv,
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
return LiopVerifier.zkWorkerPool;
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Verifies a Zero-Knowledge Receipt from a remote LIOP node via Worker Pool.
|
|
47
|
-
*
|
|
48
|
-
* @param logicPayload The raw WASM or JS logic that was sent to the provider.
|
|
49
|
-
* @param remoteImageIdHex The ImageID reported by the provider (must match our local calculation).
|
|
50
|
-
* @param zkReceipt The mathematical proof (Seal + Journal) from the zkVM.
|
|
51
|
-
*/
|
|
52
|
-
async verifyZkReceipt(logicPayload, remoteImageIdHex, zkReceipt, sessionSecret) {
|
|
53
|
-
const pool = this.getZkPool();
|
|
54
|
-
if (!pool)
|
|
55
|
-
throw new Error("Worker pool initialization failed");
|
|
56
|
-
const result = await pool.run({
|
|
57
|
-
action: "verify_receipt",
|
|
58
|
-
logicPayload: new Uint8Array(logicPayload),
|
|
59
|
-
remoteImageIdHex,
|
|
60
|
-
zkReceipt: new Uint8Array(zkReceipt),
|
|
61
|
-
sessionSecret: sessionSecret ? new Uint8Array(sessionSecret) : undefined,
|
|
62
|
-
});
|
|
63
|
-
if (result.verified) {
|
|
64
|
-
log.info(`[LiopVerifier] ${result.message}`);
|
|
65
|
-
return true;
|
|
66
|
-
}
|
|
67
|
-
log.error(`[LiopVerifier] FAILED: ${result.message}`);
|
|
68
|
-
return false;
|
|
69
|
-
}
|
|
70
|
-
/**
|
|
71
|
-
* Verifies if a node is running inside an authenticated TEE (e.g. AWS Nitro).
|
|
72
|
-
*
|
|
73
|
-
* @param attestationReport The COSE-signed attestation document from the hardware.
|
|
74
|
-
*/
|
|
75
|
-
async verifyTeeAttestation(attestationReport) {
|
|
76
|
-
if (attestationReport.length === 0)
|
|
77
|
-
return true; // Optional in Mesh Alpha
|
|
78
|
-
try {
|
|
79
|
-
// Architecture for AWS Nitro Enclaves:
|
|
80
|
-
// 1. Decode CBOR/COSE
|
|
81
|
-
// 2. Verify Signature against AWS Nitro Root CA
|
|
82
|
-
// 3. Compare PCRs
|
|
83
|
-
log.info("[LiopVerifier] TEE Attestation: Not configured (no-op).");
|
|
84
|
-
return true;
|
|
85
|
-
}
|
|
86
|
-
catch (err) {
|
|
87
|
-
log.error("[LiopVerifier] TEE Verification Failed:", err);
|
|
88
|
-
return false;
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
/**
|
|
92
|
-
* Derives the ImageID of a logic payload following the LIOP v1 Standard.
|
|
93
|
-
*/
|
|
94
|
-
deriveImageId(logicPayload) {
|
|
95
|
-
return deriveLogicImageDigest(logicPayload);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* TokenEstimator — Pluggable strategy for counting tokens in text content.
|
|
3
|
-
*
|
|
4
|
-
* Implementations range from exact BPE tokenization to lightweight heuristics,
|
|
5
|
-
* allowing the SDK to choose the best trade-off for the runtime environment.
|
|
6
|
-
*/
|
|
7
|
-
export interface TokenEstimator {
|
|
8
|
-
/** Count the number of tokens in the given text */
|
|
9
|
-
countTokens(text: string): number;
|
|
10
|
-
/** Human-readable name of the estimation strategy */
|
|
11
|
-
readonly name: string;
|
|
12
|
-
}
|
|
13
|
-
/**
|
|
14
|
-
* Exact BPE tokenizer using o200k_base encoding.
|
|
15
|
-
*
|
|
16
|
-
* o200k_base is the standard encoding for all modern OpenAI models
|
|
17
|
-
* (GPT-4o, GPT-4.1, o1, o3, o4) and provides a reasonable baseline
|
|
18
|
-
* for Anthropic/Google models as well (~±5% variance).
|
|
19
|
-
*
|
|
20
|
-
* - Synchronous: safe for hot-path usage without async overhead
|
|
21
|
-
* - Merge cache reduced to 10K entries for long-running server processes
|
|
22
|
-
* - Zero runtime dependencies beyond gpt-tokenizer itself
|
|
23
|
-
*/
|
|
24
|
-
export declare class RealTokenEstimator implements TokenEstimator {
|
|
25
|
-
readonly name = "o200k_base";
|
|
26
|
-
private countFn;
|
|
27
|
-
constructor(countFn: (text: string) => number, setMergeCacheSizeFn?: (size: number) => void);
|
|
28
|
-
countTokens(text: string): number;
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* Fallback heuristic estimator: ~4 characters per token.
|
|
32
|
-
*
|
|
33
|
-
* Industry-standard approximation (±10% for English/code content).
|
|
34
|
-
* Used only when gpt-tokenizer fails to load in constrained environments.
|
|
35
|
-
*/
|
|
36
|
-
export declare class HeuristicTokenEstimator implements TokenEstimator {
|
|
37
|
-
readonly name = "heuristic (chars/4)";
|
|
38
|
-
countTokens(text: string): number;
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* Factory: creates a RealTokenEstimator with gpt-tokenizer,
|
|
42
|
-
* falling back to HeuristicTokenEstimator if the import fails.
|
|
43
|
-
*
|
|
44
|
-
* Uses dynamic import to avoid blocking SDK initialization and to
|
|
45
|
-
* gracefully degrade in environments where gpt-tokenizer is unavailable.
|
|
46
|
-
*/
|
|
47
|
-
export declare function createTokenEstimator(): Promise<TokenEstimator>;
|
|
48
|
-
/**
|
|
49
|
-
* Synchronous factory: creates a HeuristicTokenEstimator immediately.
|
|
50
|
-
* Used when the async factory cannot be awaited (e.g., constructor contexts).
|
|
51
|
-
* The engine should upgrade to the real estimator via setEstimator() later.
|
|
52
|
-
*/
|
|
53
|
-
export declare function createSyncTokenEstimator(): TokenEstimator;
|
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
import { log } from "../utils/logger.js";
|
|
2
|
-
/**
|
|
3
|
-
* Exact BPE tokenizer using o200k_base encoding.
|
|
4
|
-
*
|
|
5
|
-
* o200k_base is the standard encoding for all modern OpenAI models
|
|
6
|
-
* (GPT-4o, GPT-4.1, o1, o3, o4) and provides a reasonable baseline
|
|
7
|
-
* for Anthropic/Google models as well (~±5% variance).
|
|
8
|
-
*
|
|
9
|
-
* - Synchronous: safe for hot-path usage without async overhead
|
|
10
|
-
* - Merge cache reduced to 10K entries for long-running server processes
|
|
11
|
-
* - Zero runtime dependencies beyond gpt-tokenizer itself
|
|
12
|
-
*/
|
|
13
|
-
export class RealTokenEstimator {
|
|
14
|
-
name = "o200k_base";
|
|
15
|
-
countFn;
|
|
16
|
-
constructor(countFn, setMergeCacheSizeFn) {
|
|
17
|
-
this.countFn = countFn;
|
|
18
|
-
// Reduce merge cache from default 100K to 10K for server processes
|
|
19
|
-
if (setMergeCacheSizeFn) {
|
|
20
|
-
setMergeCacheSizeFn(10_000);
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
countTokens(text) {
|
|
24
|
-
if (text.length === 0)
|
|
25
|
-
return 0;
|
|
26
|
-
return this.countFn(text);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* Fallback heuristic estimator: ~4 characters per token.
|
|
31
|
-
*
|
|
32
|
-
* Industry-standard approximation (±10% for English/code content).
|
|
33
|
-
* Used only when gpt-tokenizer fails to load in constrained environments.
|
|
34
|
-
*/
|
|
35
|
-
export class HeuristicTokenEstimator {
|
|
36
|
-
name = "heuristic (chars/4)";
|
|
37
|
-
countTokens(text) {
|
|
38
|
-
if (text.length === 0)
|
|
39
|
-
return 0;
|
|
40
|
-
return Math.ceil(text.length / 4);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* Factory: creates a RealTokenEstimator with gpt-tokenizer,
|
|
45
|
-
* falling back to HeuristicTokenEstimator if the import fails.
|
|
46
|
-
*
|
|
47
|
-
* Uses dynamic import to avoid blocking SDK initialization and to
|
|
48
|
-
* gracefully degrade in environments where gpt-tokenizer is unavailable.
|
|
49
|
-
*/
|
|
50
|
-
export async function createTokenEstimator() {
|
|
51
|
-
try {
|
|
52
|
-
const mod = await import("gpt-tokenizer");
|
|
53
|
-
const estimator = new RealTokenEstimator(mod.countTokens, mod.setMergeCacheSize);
|
|
54
|
-
log.debug("[LIOP-Economy] Token estimator initialized: o200k_base");
|
|
55
|
-
return estimator;
|
|
56
|
-
}
|
|
57
|
-
catch {
|
|
58
|
-
log.info("[LIOP-Economy] gpt-tokenizer unavailable, falling back to heuristic estimator");
|
|
59
|
-
return new HeuristicTokenEstimator();
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
/**
|
|
63
|
-
* Synchronous factory: creates a HeuristicTokenEstimator immediately.
|
|
64
|
-
* Used when the async factory cannot be awaited (e.g., constructor contexts).
|
|
65
|
-
* The engine should upgrade to the real estimator via setEstimator() later.
|
|
66
|
-
*/
|
|
67
|
-
export function createSyncTokenEstimator() {
|
|
68
|
-
return new HeuristicTokenEstimator();
|
|
69
|
-
}
|
package/dist/economy/index.d.ts
DELETED
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
export type { TokenEstimator } from "./estimator.js";
|
|
2
|
-
export { createSyncTokenEstimator, createTokenEstimator, HeuristicTokenEstimator, RealTokenEstimator, } from "./estimator.js";
|
|
3
|
-
export { LiopOTelBridge } from "./otel.js";
|
|
4
|
-
export type { TokenOperationMetric, TokenSessionReport, ToolTokenBreakdown, } from "./telemetry.js";
|
|
5
|
-
export { TokenTelemetryEngine } from "./telemetry.js";
|
package/dist/economy/index.js
DELETED
package/dist/economy/otel.d.ts
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* LiopOTelBridge — OpenTelemetry gen_ai.* metric emitter.
|
|
3
|
-
*
|
|
4
|
-
* Pattern: Library Instrumentation (uses global MeterProvider only).
|
|
5
|
-
* Per official OTel JS documentation:
|
|
6
|
-
* - Libraries MUST NOT create their own MeterProvider
|
|
7
|
-
* - Libraries SHOULD use metrics.getMeter() from the global API
|
|
8
|
-
* - If no MeterProvider is registered by the application, all operations are NoOp
|
|
9
|
-
* with zero runtime overhead (confirmed by OTel JS source: NoopMeterProvider)
|
|
10
|
-
*
|
|
11
|
-
* Follows OpenTelemetry Generative AI Semantic Conventions (Development status).
|
|
12
|
-
* @see https://opentelemetry.io/docs/specs/semconv/gen-ai/
|
|
13
|
-
*/
|
|
14
|
-
export declare class LiopOTelBridge {
|
|
15
|
-
private tokenUsage;
|
|
16
|
-
private operationDuration;
|
|
17
|
-
private active;
|
|
18
|
-
constructor();
|
|
19
|
-
/**
|
|
20
|
-
* Record token usage with gen_ai.* standard attributes.
|
|
21
|
-
*
|
|
22
|
-
* @param tokens - Number of tokens consumed
|
|
23
|
-
* @param tokenType - "input" or "output" (gen_ai.token.type)
|
|
24
|
-
* @param operationName - gen_ai.operation.name (e.g., "execute_tool", "chat")
|
|
25
|
-
* @param toolName - Optional LIOP-specific tool name for attribution
|
|
26
|
-
*/
|
|
27
|
-
recordTokens(tokens: number, tokenType: "input" | "output", operationName: string, toolName?: string): void;
|
|
28
|
-
/**
|
|
29
|
-
* Record operation duration with gen_ai.* standard attributes.
|
|
30
|
-
*
|
|
31
|
-
* @param durationMs - Duration in milliseconds (converted to seconds for OTel)
|
|
32
|
-
* @param operationName - gen_ai.operation.name
|
|
33
|
-
* @param error - Optional error type string if the operation failed
|
|
34
|
-
*/
|
|
35
|
-
recordDuration(durationMs: number, operationName: string, error?: string): void;
|
|
36
|
-
/** Whether the OTel bridge is actively connected to a MeterProvider */
|
|
37
|
-
isActive(): boolean;
|
|
38
|
-
}
|
package/dist/economy/otel.js
DELETED
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
import { metrics } from "@opentelemetry/api";
|
|
2
|
-
import { log } from "../utils/logger.js";
|
|
3
|
-
/** SDK identifier for the OTel Meter */
|
|
4
|
-
const METER_NAME = "@nekzus/liop";
|
|
5
|
-
const METER_VERSION = "1.2.0-alpha.9";
|
|
6
|
-
/**
|
|
7
|
-
* gen_ai.client.token.usage — Recommended explicit bucket boundaries.
|
|
8
|
-
* Source: OpenTelemetry Generative AI Semantic Conventions (experimental).
|
|
9
|
-
*/
|
|
10
|
-
const TOKEN_USAGE_BUCKETS = [
|
|
11
|
-
1, 4, 16, 64, 256, 1024, 4096, 16384, 65536, 262144, 1048576, 4194304,
|
|
12
|
-
16777216, 67108864,
|
|
13
|
-
];
|
|
14
|
-
/**
|
|
15
|
-
* gen_ai.client.operation.duration — Recommended bucket boundaries (seconds).
|
|
16
|
-
* Source: OpenTelemetry Generative AI Semantic Conventions.
|
|
17
|
-
*/
|
|
18
|
-
const DURATION_BUCKETS = [
|
|
19
|
-
0.01, 0.02, 0.04, 0.08, 0.16, 0.32, 0.64, 1.28, 2.56, 5.12, 10.24, 20.48,
|
|
20
|
-
40.96, 81.92,
|
|
21
|
-
];
|
|
22
|
-
/**
|
|
23
|
-
* LiopOTelBridge — OpenTelemetry gen_ai.* metric emitter.
|
|
24
|
-
*
|
|
25
|
-
* Pattern: Library Instrumentation (uses global MeterProvider only).
|
|
26
|
-
* Per official OTel JS documentation:
|
|
27
|
-
* - Libraries MUST NOT create their own MeterProvider
|
|
28
|
-
* - Libraries SHOULD use metrics.getMeter() from the global API
|
|
29
|
-
* - If no MeterProvider is registered by the application, all operations are NoOp
|
|
30
|
-
* with zero runtime overhead (confirmed by OTel JS source: NoopMeterProvider)
|
|
31
|
-
*
|
|
32
|
-
* Follows OpenTelemetry Generative AI Semantic Conventions (Development status).
|
|
33
|
-
* @see https://opentelemetry.io/docs/specs/semconv/gen-ai/
|
|
34
|
-
*/
|
|
35
|
-
export class LiopOTelBridge {
|
|
36
|
-
tokenUsage;
|
|
37
|
-
operationDuration;
|
|
38
|
-
active = false;
|
|
39
|
-
constructor() {
|
|
40
|
-
try {
|
|
41
|
-
const meter = metrics.getMeter(METER_NAME, METER_VERSION);
|
|
42
|
-
this.tokenUsage = meter.createHistogram("gen_ai.client.token.usage", {
|
|
43
|
-
description: "Number of tokens used in LIOP Logic-on-Origin operations",
|
|
44
|
-
unit: "{token}",
|
|
45
|
-
advice: { explicitBucketBoundaries: TOKEN_USAGE_BUCKETS },
|
|
46
|
-
});
|
|
47
|
-
this.operationDuration = meter.createHistogram("gen_ai.client.operation.duration", {
|
|
48
|
-
description: "Duration of LIOP operations",
|
|
49
|
-
unit: "s",
|
|
50
|
-
advice: { explicitBucketBoundaries: DURATION_BUCKETS },
|
|
51
|
-
});
|
|
52
|
-
this.active = true;
|
|
53
|
-
log.debug("[LIOP-OTel] gen_ai.* metrics bridge initialized");
|
|
54
|
-
}
|
|
55
|
-
catch (err) {
|
|
56
|
-
// OTel API failed to load — degrade gracefully without affecting protocol
|
|
57
|
-
log.debug(`[LIOP-OTel] Bridge disabled: ${err instanceof Error ? err.message : String(err)}`);
|
|
58
|
-
const noopHistogram = {
|
|
59
|
-
record: () => { },
|
|
60
|
-
};
|
|
61
|
-
this.tokenUsage = noopHistogram;
|
|
62
|
-
this.operationDuration = noopHistogram;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* Record token usage with gen_ai.* standard attributes.
|
|
67
|
-
*
|
|
68
|
-
* @param tokens - Number of tokens consumed
|
|
69
|
-
* @param tokenType - "input" or "output" (gen_ai.token.type)
|
|
70
|
-
* @param operationName - gen_ai.operation.name (e.g., "execute_tool", "chat")
|
|
71
|
-
* @param toolName - Optional LIOP-specific tool name for attribution
|
|
72
|
-
*/
|
|
73
|
-
recordTokens(tokens, tokenType, operationName, toolName) {
|
|
74
|
-
this.tokenUsage.record(tokens, {
|
|
75
|
-
"gen_ai.system": "liop",
|
|
76
|
-
"gen_ai.operation.name": operationName,
|
|
77
|
-
"gen_ai.token.type": tokenType,
|
|
78
|
-
"gen_ai.request.model": "liop-mesh",
|
|
79
|
-
...(toolName ? { "liop.tool.name": toolName } : {}),
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
/**
|
|
83
|
-
* Record operation duration with gen_ai.* standard attributes.
|
|
84
|
-
*
|
|
85
|
-
* @param durationMs - Duration in milliseconds (converted to seconds for OTel)
|
|
86
|
-
* @param operationName - gen_ai.operation.name
|
|
87
|
-
* @param error - Optional error type string if the operation failed
|
|
88
|
-
*/
|
|
89
|
-
recordDuration(durationMs, operationName, error) {
|
|
90
|
-
this.operationDuration.record(durationMs / 1000, {
|
|
91
|
-
"gen_ai.system": "liop",
|
|
92
|
-
"gen_ai.operation.name": operationName,
|
|
93
|
-
...(error ? { "error.type": error } : {}),
|
|
94
|
-
});
|
|
95
|
-
}
|
|
96
|
-
/** Whether the OTel bridge is actively connected to a MeterProvider */
|
|
97
|
-
isActive() {
|
|
98
|
-
return this.active;
|
|
99
|
-
}
|
|
100
|
-
}
|