@nekzus/liop 1.2.0-alpha.9 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/README.md +12 -3
  2. package/dist/bin/agent.js +222 -51
  3. package/dist/bridge/index.js +7 -6
  4. package/dist/bridge/stream.js +11 -11
  5. package/dist/client/index.js +46 -35
  6. package/dist/crypto/logic-image-id.d.ts +3 -0
  7. package/dist/crypto/logic-image-id.js +27 -0
  8. package/dist/crypto/verifier.js +7 -19
  9. package/dist/economy/estimator.d.ts +53 -0
  10. package/dist/economy/estimator.js +69 -0
  11. package/dist/economy/index.d.ts +5 -0
  12. package/dist/economy/index.js +3 -0
  13. package/dist/economy/otel.d.ts +38 -0
  14. package/dist/economy/otel.js +100 -0
  15. package/dist/economy/telemetry.d.ts +77 -0
  16. package/dist/economy/telemetry.js +224 -0
  17. package/dist/errors.d.ts +14 -0
  18. package/dist/errors.js +19 -0
  19. package/dist/gateway/hybrid.d.ts +3 -1
  20. package/dist/gateway/hybrid.js +38 -13
  21. package/dist/gateway/router.d.ts +25 -9
  22. package/dist/gateway/router.js +484 -133
  23. package/dist/index.d.ts +3 -0
  24. package/dist/index.js +3 -0
  25. package/dist/mesh/node.d.ts +16 -0
  26. package/dist/mesh/node.js +394 -113
  27. package/dist/prompts/adapters.d.ts +16 -0
  28. package/dist/prompts/adapters.js +55 -0
  29. package/dist/rpc/proto.js +2 -1
  30. package/dist/rpc/server.d.ts +1 -1
  31. package/dist/rpc/server.js +4 -3
  32. package/dist/rpc/tls.js +3 -2
  33. package/dist/sandbox/wasi.d.ts +1 -1
  34. package/dist/sandbox/wasi.js +43 -3
  35. package/dist/security/guardian.js +3 -2
  36. package/dist/security/zk.d.ts +2 -3
  37. package/dist/security/zk.js +22 -9
  38. package/dist/server/index.d.ts +53 -4
  39. package/dist/server/index.js +362 -49
  40. package/dist/server/pii.d.ts +12 -0
  41. package/dist/server/pii.js +90 -0
  42. package/dist/types.d.ts +16 -0
  43. package/dist/utils/logger.d.ts +21 -0
  44. package/dist/utils/logger.js +70 -0
  45. package/dist/utils/mcpCompact.d.ts +11 -0
  46. package/dist/utils/mcpCompact.js +29 -0
  47. package/dist/workers/logic-execution.d.ts +1 -1
  48. package/dist/workers/logic-execution.js +38 -20
  49. package/dist/workers/zk-verifier.js +37 -33
  50. package/package.json +14 -2
@@ -3,6 +3,7 @@ import { MeshNode, } from "../mesh/node.js";
3
3
  import { LiopRpcClient } from "../rpc/client.js";
4
4
  import { AesGcmWrapper } from "../rpc/crypto/aes.js";
5
5
  import { Kyber768Wrapper } from "../rpc/crypto/kyber.js";
6
+ import { log } from "../utils/logger.js";
6
7
  /**
7
8
  * LIOP Client
8
9
  * High-level orchestration for discovery and execution in the Logic-Injection-on-Origin mesh.
@@ -24,11 +25,11 @@ export class LiopClient {
24
25
  async connect(address, options) {
25
26
  this.meshNode = new MeshNode(options?.meshConfig);
26
27
  await this.meshNode.start();
27
- console.error(`[LiopClient] Mesh Node synchronized. PeerID: ${this.meshNode.getPeerId()}`);
28
+ log.info(`[LiopClient] Mesh Node synchronized. PeerID: ${this.meshNode.getPeerId()}`);
28
29
  if (address) {
29
30
  this.rpcClients.set("static", new LiopRpcClient(address, this.tlsOptions));
30
31
  this.serverInfo = { name: `LiopServer (${address})`, version: "1.0.0" };
31
- console.error(`[LiopClient] Static gRPC configured for: ${address}`);
32
+ log.info(`[LiopClient] Static gRPC configured for: ${address}`);
32
33
  }
33
34
  else {
34
35
  this.serverInfo = { name: "LiopServer (Mesh Alpha)", version: "1.0.0" };
@@ -41,25 +42,25 @@ export class LiopClient {
41
42
  async resolveCapability(toolName) {
42
43
  if (!this.meshNode)
43
44
  throw new Error("Client must be connected to Mesh to resolve capabilities.");
44
- console.error(`[LiopClient] Querying Mesh DHT for Provider: ${toolName}...`);
45
+ log.info(`[LiopClient] Querying Mesh DHT for Provider: ${toolName}...`);
45
46
  const providers = await this.meshNode.findProviders(toolName);
46
47
  if (providers.length === 0) {
47
48
  throw new Error(`Kademlia DHT found zero providers for capability: ${toolName}`);
48
49
  }
49
50
  const providerId = providers[0];
50
- console.error(`[LiopClient] Identified Alpha Provider PeerID: ${providerId}`);
51
+ log.info(`[LiopClient] Identified Alpha Provider PeerID: ${providerId}`);
51
52
  let grpcPort = 50051;
52
53
  const manifest = await this.meshNode.queryManifest(providerId);
53
54
  if (manifest) {
54
55
  grpcPort = manifest.grpcPort;
55
- console.error(`[LiopClient] Manifest resolved: gRPC port ${grpcPort}`);
56
+ log.info(`[LiopClient] Manifest resolved: gRPC port ${grpcPort}`);
56
57
  }
57
58
  const addrs = await this.meshNode.resolvePeer(providerId);
58
59
  for (const maddr of addrs) {
59
60
  const parts = maddr.split("/");
60
61
  if (parts[1] === "ip4") {
61
62
  const grpcHost = `${parts[2]}:${grpcPort}`;
62
- console.error(`[LiopClient] Translated Multiaddr to gRPC Target: ${grpcHost}`);
63
+ log.info(`[LiopClient] Translated Multiaddr to gRPC Target: ${grpcHost}`);
63
64
  return grpcHost;
64
65
  }
65
66
  }
@@ -72,13 +73,13 @@ export class LiopClient {
72
73
  if (!this.meshNode) {
73
74
  throw new Error("Client must be connected before discovering tools.");
74
75
  }
75
- console.error(`[LiopClient] Discovery started...`);
76
+ log.info(`[LiopClient] Discovery started...`);
76
77
  const providerIds = await this.meshNode.discoverManifestProviders();
77
78
  const tools = [];
78
79
  const seenNames = new Set();
79
80
  for (const peerId of providerIds) {
80
81
  try {
81
- console.error(`[LiopClient] Querying manifest from: ${peerId}`);
82
+ log.info(`[LiopClient] Querying manifest from: ${peerId}`);
82
83
  const manifest = await this.meshNode.queryManifest(peerId);
83
84
  if (manifest) {
84
85
  this.manifests.set(peerId, manifest);
@@ -91,10 +92,10 @@ export class LiopClient {
91
92
  }
92
93
  }
93
94
  catch (err) {
94
- console.error(`[LiopClient] Error querying manifest from ${peerId}:`, err instanceof Error ? err.message : String(err));
95
+ log.info(`[LiopClient] Error querying manifest from ${peerId}:`, err instanceof Error ? err.message : String(err));
95
96
  }
96
97
  }
97
- console.error(`[LiopClient] Discovery finished. Found ${tools.length} unique tools.`);
98
+ log.info(`[LiopClient] Discovery finished. Found ${tools.length} unique tools.`);
98
99
  return tools;
99
100
  }
100
101
  /**
@@ -105,7 +106,7 @@ export class LiopClient {
105
106
  throw new Error("Client must be connected before calling tools.");
106
107
  }
107
108
  const toolName = request.name;
108
- console.error(`[LiopClient] Resolving Tool: ${toolName}`);
109
+ log.info(`[LiopClient] Resolving Tool: ${toolName}`);
109
110
  // [ALPHA-FIX] Bypass DHT discovery if we are already statically connected to a provider (Enterprise/Test mode)
110
111
  let rpcClient = this.rpcClients.get("static");
111
112
  if (!rpcClient) {
@@ -113,13 +114,20 @@ export class LiopClient {
113
114
  rpcClient = this.getOrCreateRpcClient(toolName, dynamicAddress);
114
115
  }
115
116
  else {
116
- console.error(`[LiopClient] Using existing static gRPC connection for ${toolName}.`);
117
+ log.info(`[LiopClient] Using existing static gRPC connection for ${toolName}.`);
117
118
  }
118
- console.error(`[LiopClient] Negotiating intent for ${toolName}...`);
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;
119
127
  const intentResponse = (await rpcClient.negotiateIntent({
120
- agent_did: "liop-client-alpha",
128
+ agent_did: agentDid,
121
129
  capability_hash: toolName,
122
- proof_of_intent: Buffer.from("alpha-intent-proof"),
130
+ proof_of_intent: proofOfIntent,
123
131
  }));
124
132
  if (!intentResponse.accepted) {
125
133
  throw new Error(`Intent denied by host: ${intentResponse.error_message}`);
@@ -128,14 +136,14 @@ export class LiopClient {
128
136
  const publicKey = intentResponse.kyber_public_key || intentResponse.kyberPublicKey;
129
137
  const sessionToken = intentResponse.session_token || intentResponse.sessionToken;
130
138
  if (!publicKey) {
131
- console.error("[LiopClient] Critical Error: Kyber Public Key not found in IntentResponse.", intentResponse);
139
+ log.info("[LiopClient] Critical Error: Kyber Public Key not found in IntentResponse.", intentResponse);
132
140
  throw new Error("Handshake failed: Remote host did not provide a valid Kyber Public Key.");
133
141
  }
134
142
  // 2. Post-Quantum Encapsulation (ML-KEM-768)
135
- console.error(`[LiopClient] Encapsulating Post-Quantum Shared Secret for ${request.name}...`);
143
+ log.info(`[LiopClient] Encapsulating Post-Quantum Shared Secret for ${request.name}...`);
136
144
  const { ciphertext: kyberCiphertext, sharedSecret } = await Kyber768Wrapper.encapsulateAsymmetric(publicKey);
137
145
  // 3. Symmetric Sealing (AES-256-GCM)
138
- console.error(`[LiopClient] Sealing WASM Payload and Inputs...`);
146
+ log.info(`[LiopClient] Sealing WASM Payload and Inputs...`);
139
147
  const _safePayload = _wasmPayload || Buffer.from("");
140
148
  // Encrypt WASM binary
141
149
  const { ciphertext: encryptedWasm, nonce: aesNonce } = AesGcmWrapper.encryptPayload(_safePayload, sharedSecret);
@@ -173,7 +181,7 @@ export class LiopClient {
173
181
  if (resultFulfilled)
174
182
  return;
175
183
  hasReceivedData = true;
176
- console.error("[LiopClient] Logic Executed. Verification in progress...");
184
+ log.info("[LiopClient] Logic Executed. Verification in progress...");
177
185
  try {
178
186
  const isValid = await this.verifier.verifyZkReceipt(_safePayload, Buffer.from(response.cryptographic_proof).toString("hex"), Buffer.from(response.zk_receipt));
179
187
  if (!isValid) {
@@ -198,7 +206,7 @@ export class LiopClient {
198
206
  stream.on("error", (err) => {
199
207
  if (resultFulfilled)
200
208
  return;
201
- console.error("[LiopClient] Stream Error:", err);
209
+ log.error("[LiopClient] Stream Error:", err);
202
210
  reject(err);
203
211
  });
204
212
  stream.on("end", () => {
@@ -226,26 +234,29 @@ export class LiopClient {
226
234
  if (!this.meshNode) {
227
235
  throw new Error("Client must be connected before reading resources.");
228
236
  }
229
- console.error(`[LiopClient] Querying Mesh for Resource: ${uri}...`);
230
- // For now, in Alpha v3, we assume the resource is provided by an active provider.
231
- // A more complex implementation would use resolveCapability(uri).
232
- // For the industrial demo, we'll simulate a direct read if connected or throw.
233
- const rpcClient = this.rpcClients.get("static") || Array.from(this.rpcClients.values())[0];
234
- if (!rpcClient) {
235
- throw new Error("Resource reading requires an active RPC connection to a provider.");
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.`);
236
252
  }
237
- // This emulates the resource retrieval.
238
- // In a full implementation, this might be a gRPC call.
253
+ // Return the declarative metadata (Logic-Injection is required for actual data extraction)
239
254
  return {
240
255
  contents: [
241
256
  {
242
257
  uri,
243
- mimeType: "application/json",
244
- text: JSON.stringify({
245
- status: "Alpha-Resource-Read-Success",
246
- uri,
247
- timestamp: new Date().toISOString(),
248
- }),
258
+ mimeType: resourceDef.mimeType || "application/json",
259
+ text: JSON.stringify(resourceDef, null, 2),
249
260
  },
250
261
  ],
251
262
  };
@@ -0,0 +1,3 @@
1
+ export declare function normalizeLogicSource(logicUtf8: string): string;
2
+ /** SHA-256 digest of logic bytes (WASM raw; JS UTF-8 after top-level envelope strip). */
3
+ export declare function deriveLogicImageDigest(logicPayload: Uint8Array): Buffer;
@@ -0,0 +1,27 @@
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
+ }
@@ -1,8 +1,9 @@
1
- import crypto from "node:crypto";
2
1
  import { createRequire } from "node:module";
3
2
  import path from "node:path";
4
3
  import { fileURLToPath, pathToFileURL } from "node:url";
5
4
  import { Piscina } from "piscina";
5
+ import { log } from "../utils/logger.js";
6
+ import { deriveLogicImageDigest } from "./logic-image-id.js";
6
7
  const __filename = fileURLToPath(import.meta.url);
7
8
  const __dirname = path.dirname(__filename);
8
9
  /**
@@ -59,10 +60,10 @@ export class LiopVerifier {
59
60
  zkReceipt: new Uint8Array(zkReceipt),
60
61
  });
61
62
  if (result.verified) {
62
- console.error(`[LiopVerifier] ${result.message}`);
63
+ log.info(`[LiopVerifier] ${result.message}`);
63
64
  return true;
64
65
  }
65
- console.error(`[LiopVerifier] FAILED: ${result.message}`);
66
+ log.error(`[LiopVerifier] FAILED: ${result.message}`);
66
67
  return false;
67
68
  }
68
69
  /**
@@ -78,11 +79,11 @@ export class LiopVerifier {
78
79
  // 1. Decode CBOR/COSE
79
80
  // 2. Verify Signature against AWS Nitro Root CA
80
81
  // 3. Compare PCRs
81
- console.error("[LiopVerifier] TEE Attestation: AWS Nitro Enclave Signature Verified.");
82
+ log.info("[LiopVerifier] TEE Attestation: Not configured (no-op).");
82
83
  return true;
83
84
  }
84
85
  catch (err) {
85
- console.error("[LiopVerifier] TEE Verification Failed:", err);
86
+ log.error("[LiopVerifier] TEE Verification Failed:", err);
86
87
  return false;
87
88
  }
88
89
  }
@@ -90,19 +91,6 @@ export class LiopVerifier {
90
91
  * Derives the ImageID of a logic payload following the LIOP v1 Standard.
91
92
  */
92
93
  deriveImageId(logicPayload) {
93
- // Sanitization logic for JS payloads (Magic headers, etc.)
94
- let processed = logicPayload;
95
- const isWasm = logicPayload[0] === 0x00 && logicPayload[1] === 0x61; // \0asm
96
- if (!isWasm) {
97
- const text = logicPayload.toString("utf-8");
98
- const clean = text
99
- .replace(/^LIOP_MAGIC:.*?\n/g, "")
100
- .replace(/^MANIFEST:.*?\n/g, "")
101
- .replace(/---BEGIN_LOGIC---\n?/g, "")
102
- .replace(/\n?---END_LOGIC---/g, "")
103
- .trim();
104
- processed = Buffer.from(clean);
105
- }
106
- return crypto.createHash("sha256").update(processed).digest();
94
+ return deriveLogicImageDigest(logicPayload);
107
95
  }
108
96
  }
@@ -0,0 +1,53 @@
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;
@@ -0,0 +1,69 @@
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
+ }
@@ -0,0 +1,5 @@
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";
@@ -0,0 +1,3 @@
1
+ export { createSyncTokenEstimator, createTokenEstimator, HeuristicTokenEstimator, RealTokenEstimator, } from "./estimator.js";
2
+ export { LiopOTelBridge } from "./otel.js";
3
+ export { TokenTelemetryEngine } from "./telemetry.js";
@@ -0,0 +1,38 @@
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
+ }
@@ -0,0 +1,100 @@
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
+ }
@@ -0,0 +1,77 @@
1
+ /** Single MCP operation token footprint */
2
+ export interface TokenOperationMetric {
3
+ readonly type: "tools_list" | "tool_call" | "resource_read" | "resource_list" | "prompt_get" | "prompt_list" | "diagnostic";
4
+ readonly method: string;
5
+ readonly estimatedInputTokens: number;
6
+ readonly estimatedOutputTokens: number;
7
+ readonly timestamp: number;
8
+ readonly toolName?: string;
9
+ readonly peerId?: string;
10
+ readonly durationMs?: number;
11
+ }
12
+ /** Session-level aggregate report */
13
+ export interface TokenSessionReport {
14
+ readonly sessionId: string;
15
+ readonly operations: ReadonlyArray<TokenOperationMetric>;
16
+ readonly totalInputTokens: number;
17
+ readonly totalOutputTokens: number;
18
+ readonly estimatorName: string;
19
+ readonly sessionUptimeMs: number;
20
+ }
21
+ /** Per-tool aggregate breakdown */
22
+ export interface ToolTokenBreakdown {
23
+ readonly input: number;
24
+ readonly output: number;
25
+ readonly calls: number;
26
+ readonly avgDurationMs: number;
27
+ }
28
+ /**
29
+ * TokenTelemetryEngine — Full-spectrum observational singleton for token cost measurement.
30
+ *
31
+ * Design principles:
32
+ * - Pure observer pattern: NEVER mutates MCP payloads or protocol flow.
33
+ * - Real tokenization: o200k_base BPE via gpt-tokenizer (async init, sync counting).
34
+ * - OTel gen_ai.* emission: standard metrics via @opentelemetry/api (NoOp if no provider).
35
+ * - Error isolation: telemetry failures never propagate to protocol operations.
36
+ */
37
+ export declare class TokenTelemetryEngine {
38
+ private static instance;
39
+ private operations;
40
+ private readonly sessionId;
41
+ private readonly startedAt;
42
+ private estimator;
43
+ private otelBridge;
44
+ private constructor();
45
+ /** Async upgrade from heuristic to real BPE tokenizer */
46
+ private initRealEstimator;
47
+ static getInstance(): TokenTelemetryEngine;
48
+ /**
49
+ * Count tokens in a string using the active estimator.
50
+ * Delegates to o200k_base BPE tokenizer (or heuristic fallback).
51
+ */
52
+ countTokens(content: string): number;
53
+ /**
54
+ * Record a single MCP operation's token footprint.
55
+ * Emits both internal metrics and OTel gen_ai.* histograms.
56
+ */
57
+ record(metric: Omit<TokenOperationMetric, "timestamp">): void;
58
+ /**
59
+ * @deprecated Use countTokens() instead. Kept for backward compatibility.
60
+ */
61
+ estimateTokens(content: string): number;
62
+ /** Generate the full session report */
63
+ getReport(): TokenSessionReport;
64
+ /** Get per-tool token breakdown for diagnostic display */
65
+ getPerToolReport(): Map<string, ToolTokenBreakdown>;
66
+ /**
67
+ * Format a rich, human-readable summary block for LiopMeshStatus diagnostic.
68
+ * Returns empty string when no operations have been recorded.
69
+ */
70
+ formatStatusBlock(): string;
71
+ /** Format milliseconds into human-readable uptime string */
72
+ private formatUptime;
73
+ /** Reset all recorded metrics (used in tests) */
74
+ reset(): void;
75
+ /** Destroy the singleton (used in tests to guarantee isolation) */
76
+ static destroy(): void;
77
+ }