@nekzus/liop 1.2.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.
Files changed (74) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +413 -0
  3. package/dist/bin/agent.d.ts +2 -0
  4. package/dist/bin/agent.js +307 -0
  5. package/dist/bridge/index.d.ts +37 -0
  6. package/dist/bridge/index.js +249 -0
  7. package/dist/bridge/stream.d.ts +62 -0
  8. package/dist/bridge/stream.js +202 -0
  9. package/dist/client/index.d.ts +60 -0
  10. package/dist/client/index.js +275 -0
  11. package/dist/crypto/logic-image-id.d.ts +3 -0
  12. package/dist/crypto/logic-image-id.js +27 -0
  13. package/dist/crypto/verifier.d.ts +29 -0
  14. package/dist/crypto/verifier.js +96 -0
  15. package/dist/economy/estimator.d.ts +53 -0
  16. package/dist/economy/estimator.js +69 -0
  17. package/dist/economy/index.d.ts +5 -0
  18. package/dist/economy/index.js +3 -0
  19. package/dist/economy/otel.d.ts +38 -0
  20. package/dist/economy/otel.js +100 -0
  21. package/dist/economy/telemetry.d.ts +77 -0
  22. package/dist/economy/telemetry.js +224 -0
  23. package/dist/gateway/hybrid.d.ts +23 -0
  24. package/dist/gateway/hybrid.js +199 -0
  25. package/dist/gateway/router.d.ts +69 -0
  26. package/dist/gateway/router.js +1036 -0
  27. package/dist/index.d.ts +11 -0
  28. package/dist/index.js +11 -0
  29. package/dist/mesh/index.d.ts +1 -0
  30. package/dist/mesh/index.js +1 -0
  31. package/dist/mesh/node.d.ts +129 -0
  32. package/dist/mesh/node.js +853 -0
  33. package/dist/prompts/adapters.d.ts +16 -0
  34. package/dist/prompts/adapters.js +55 -0
  35. package/dist/protocol/liop_core.proto +44 -0
  36. package/dist/rpc/client.d.ts +22 -0
  37. package/dist/rpc/client.js +40 -0
  38. package/dist/rpc/codec/lpm.d.ts +20 -0
  39. package/dist/rpc/codec/lpm.js +36 -0
  40. package/dist/rpc/crypto/aes.d.ts +22 -0
  41. package/dist/rpc/crypto/aes.js +47 -0
  42. package/dist/rpc/crypto/kyber.d.ts +27 -0
  43. package/dist/rpc/crypto/kyber.js +70 -0
  44. package/dist/rpc/proto.d.ts +2 -0
  45. package/dist/rpc/proto.js +33 -0
  46. package/dist/rpc/server.d.ts +13 -0
  47. package/dist/rpc/server.js +50 -0
  48. package/dist/rpc/tls.d.ts +26 -0
  49. package/dist/rpc/tls.js +54 -0
  50. package/dist/rpc/types.d.ts +28 -0
  51. package/dist/rpc/types.js +5 -0
  52. package/dist/sandbox/guardian.d.ts +18 -0
  53. package/dist/sandbox/guardian.js +35 -0
  54. package/dist/sandbox/wasi.d.ts +36 -0
  55. package/dist/sandbox/wasi.js +179 -0
  56. package/dist/security/guardian.d.ts +22 -0
  57. package/dist/security/guardian.js +52 -0
  58. package/dist/security/zk.d.ts +37 -0
  59. package/dist/security/zk.js +66 -0
  60. package/dist/server/index.d.ts +184 -0
  61. package/dist/server/index.js +933 -0
  62. package/dist/server/pii.d.ts +40 -0
  63. package/dist/server/pii.js +266 -0
  64. package/dist/types.d.ts +145 -0
  65. package/dist/types.js +26 -0
  66. package/dist/utils/logger.d.ts +21 -0
  67. package/dist/utils/logger.js +70 -0
  68. package/dist/utils/mcpCompact.d.ts +11 -0
  69. package/dist/utils/mcpCompact.js +29 -0
  70. package/dist/workers/logic-execution.d.ts +17 -0
  71. package/dist/workers/logic-execution.js +121 -0
  72. package/dist/workers/zk-verifier.d.ts +20 -0
  73. package/dist/workers/zk-verifier.js +84 -0
  74. package/package.json +147 -0
@@ -0,0 +1,96 @@
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) {
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
+ });
62
+ if (result.verified) {
63
+ log.info(`[LiopVerifier] ${result.message}`);
64
+ return true;
65
+ }
66
+ log.error(`[LiopVerifier] FAILED: ${result.message}`);
67
+ return false;
68
+ }
69
+ /**
70
+ * Verifies if a node is running inside an authenticated TEE (e.g. AWS Nitro).
71
+ *
72
+ * @param attestationReport The COSE-signed attestation document from the hardware.
73
+ */
74
+ async verifyTeeAttestation(attestationReport) {
75
+ if (attestationReport.length === 0)
76
+ return true; // Optional in Mesh Alpha
77
+ try {
78
+ // Architecture for AWS Nitro Enclaves:
79
+ // 1. Decode CBOR/COSE
80
+ // 2. Verify Signature against AWS Nitro Root CA
81
+ // 3. Compare PCRs
82
+ log.info("[LiopVerifier] TEE Attestation: Not configured (no-op).");
83
+ return true;
84
+ }
85
+ catch (err) {
86
+ log.error("[LiopVerifier] TEE Verification Failed:", err);
87
+ return false;
88
+ }
89
+ }
90
+ /**
91
+ * Derives the ImageID of a logic payload following the LIOP v1 Standard.
92
+ */
93
+ deriveImageId(logicPayload) {
94
+ return deriveLogicImageDigest(logicPayload);
95
+ }
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
+ }
@@ -0,0 +1,224 @@
1
+ import { createSyncTokenEstimator, createTokenEstimator, } from "./estimator.js";
2
+ import { LiopOTelBridge } from "./otel.js";
3
+ /**
4
+ * Maps operation types to OTel gen_ai.operation.name values.
5
+ * @see https://opentelemetry.io/docs/specs/semconv/gen-ai/
6
+ */
7
+ const OTEL_OPERATION_MAP = {
8
+ tools_list: "chat",
9
+ tool_call: "execute_tool",
10
+ resource_read: "chat",
11
+ resource_list: "chat",
12
+ prompt_get: "chat",
13
+ prompt_list: "chat",
14
+ diagnostic: "chat",
15
+ };
16
+ /**
17
+ * TokenTelemetryEngine — Full-spectrum observational singleton for token cost measurement.
18
+ *
19
+ * Design principles:
20
+ * - Pure observer pattern: NEVER mutates MCP payloads or protocol flow.
21
+ * - Real tokenization: o200k_base BPE via gpt-tokenizer (async init, sync counting).
22
+ * - OTel gen_ai.* emission: standard metrics via @opentelemetry/api (NoOp if no provider).
23
+ * - Error isolation: telemetry failures never propagate to protocol operations.
24
+ */
25
+ export class TokenTelemetryEngine {
26
+ static instance = null;
27
+ operations = [];
28
+ sessionId;
29
+ startedAt;
30
+ estimator;
31
+ otelBridge;
32
+ constructor() {
33
+ this.sessionId = crypto.randomUUID();
34
+ this.startedAt = Date.now();
35
+ // Start with sync heuristic estimator (available immediately)
36
+ this.estimator = createSyncTokenEstimator();
37
+ this.otelBridge = new LiopOTelBridge();
38
+ // Upgrade to real tokenizer asynchronously
39
+ this.initRealEstimator();
40
+ }
41
+ /** Async upgrade from heuristic to real BPE tokenizer */
42
+ initRealEstimator() {
43
+ createTokenEstimator()
44
+ .then((real) => {
45
+ this.estimator = real;
46
+ })
47
+ .catch(() => {
48
+ // Keep heuristic fallback — already assigned in constructor
49
+ });
50
+ }
51
+ static getInstance() {
52
+ if (!TokenTelemetryEngine.instance) {
53
+ TokenTelemetryEngine.instance = new TokenTelemetryEngine();
54
+ }
55
+ return TokenTelemetryEngine.instance;
56
+ }
57
+ /**
58
+ * Count tokens in a string using the active estimator.
59
+ * Delegates to o200k_base BPE tokenizer (or heuristic fallback).
60
+ */
61
+ countTokens(content) {
62
+ try {
63
+ return this.estimator.countTokens(content);
64
+ }
65
+ catch {
66
+ // Fallback: never let counting failures break protocol flow
67
+ return Math.ceil(content.length / 4);
68
+ }
69
+ }
70
+ /**
71
+ * Record a single MCP operation's token footprint.
72
+ * Emits both internal metrics and OTel gen_ai.* histograms.
73
+ */
74
+ record(metric) {
75
+ const fullMetric = {
76
+ ...metric,
77
+ timestamp: Date.now(),
78
+ };
79
+ this.operations.push(fullMetric);
80
+ // Emit to OTel bridge (NoOp if no MeterProvider configured)
81
+ try {
82
+ const otelOp = OTEL_OPERATION_MAP[metric.type] || "chat";
83
+ if (metric.estimatedInputTokens > 0) {
84
+ this.otelBridge.recordTokens(metric.estimatedInputTokens, "input", otelOp, metric.toolName);
85
+ }
86
+ if (metric.estimatedOutputTokens > 0) {
87
+ this.otelBridge.recordTokens(metric.estimatedOutputTokens, "output", otelOp, metric.toolName);
88
+ }
89
+ if (metric.durationMs !== undefined) {
90
+ this.otelBridge.recordDuration(metric.durationMs, otelOp);
91
+ }
92
+ }
93
+ catch {
94
+ // OTel emission failure must never affect protocol operations
95
+ }
96
+ }
97
+ /**
98
+ * @deprecated Use countTokens() instead. Kept for backward compatibility.
99
+ */
100
+ estimateTokens(content) {
101
+ return this.countTokens(content);
102
+ }
103
+ /** Generate the full session report */
104
+ getReport() {
105
+ return {
106
+ sessionId: this.sessionId,
107
+ operations: [...this.operations],
108
+ totalInputTokens: this.operations.reduce((sum, op) => sum + op.estimatedInputTokens, 0),
109
+ totalOutputTokens: this.operations.reduce((sum, op) => sum + op.estimatedOutputTokens, 0),
110
+ estimatorName: this.estimator.name,
111
+ sessionUptimeMs: Date.now() - this.startedAt,
112
+ };
113
+ }
114
+ /** Get per-tool token breakdown for diagnostic display */
115
+ getPerToolReport() {
116
+ const breakdown = new Map();
117
+ for (const op of this.operations) {
118
+ const key = op.toolName || op.method;
119
+ const existing = breakdown.get(key) || {
120
+ input: 0,
121
+ output: 0,
122
+ calls: 0,
123
+ avgDurationMs: 0,
124
+ };
125
+ const totalDuration = existing.avgDurationMs * existing.calls + (op.durationMs || 0);
126
+ const newCalls = existing.calls + 1;
127
+ breakdown.set(key, {
128
+ input: existing.input + op.estimatedInputTokens,
129
+ output: existing.output + op.estimatedOutputTokens,
130
+ calls: newCalls,
131
+ avgDurationMs: newCalls > 0 ? totalDuration / newCalls : 0,
132
+ });
133
+ }
134
+ return breakdown;
135
+ }
136
+ /**
137
+ * Format a rich, human-readable summary block for LiopMeshStatus diagnostic.
138
+ * Returns empty string when no operations have been recorded.
139
+ */
140
+ formatStatusBlock() {
141
+ const report = this.getReport();
142
+ if (report.operations.length === 0)
143
+ return "";
144
+ const uptimeStr = this.formatUptime(report.sessionUptimeMs);
145
+ const totalCombined = report.totalInputTokens + report.totalOutputTokens;
146
+ // Aggregate operations by type
147
+ const byType = new Map();
148
+ for (const op of report.operations) {
149
+ const key = op.type;
150
+ const existing = byType.get(key) || {
151
+ count: 0,
152
+ input: 0,
153
+ output: 0,
154
+ };
155
+ byType.set(key, {
156
+ count: existing.count + 1,
157
+ input: existing.input + op.estimatedInputTokens,
158
+ output: existing.output + op.estimatedOutputTokens,
159
+ });
160
+ }
161
+ // Build type breakdown lines
162
+ const typeEntries = Array.from(byType.entries());
163
+ const typeLines = typeEntries.map(([type, data], idx) => {
164
+ const prefix = idx === typeEntries.length - 1 ? "│ └─" : "│ ├─";
165
+ const outputPart = data.output > 0 ? ` / ${data.output.toLocaleString()} out` : "";
166
+ return `${prefix} ${type} ×${data.count} → ${data.input.toLocaleString()} in${outputPart}`;
167
+ });
168
+ // Build per-tool breakdown
169
+ const toolReport = this.getPerToolReport();
170
+ const toolEntries = Array.from(toolReport.entries()).filter(([key]) => key !== "tools/list" && key !== "LiopMeshStatus");
171
+ const toolLines = [];
172
+ if (toolEntries.length > 0) {
173
+ toolLines.push("├─ By Tool:");
174
+ toolEntries.forEach(([name, data], idx) => {
175
+ const prefix = idx === toolEntries.length - 1 ? "│ └─" : "│ ├─";
176
+ const outputPart = data.output > 0 ? ` / ${data.output.toLocaleString()} out` : "";
177
+ const durationPart = data.avgDurationMs > 0 ? ` ~${Math.round(data.avgDurationMs)}ms` : "";
178
+ toolLines.push(`${prefix} ${name}: ${data.input.toLocaleString()} in${outputPart} (×${data.calls})${durationPart}`);
179
+ });
180
+ }
181
+ // Calculate average latency across all timed operations
182
+ const timedOps = report.operations.filter((op) => op.durationMs !== undefined);
183
+ const avgLatency = timedOps.length > 0
184
+ ? Math.round(timedOps.reduce((sum, op) => sum + (op.durationMs || 0), 0) /
185
+ timedOps.length)
186
+ : 0;
187
+ const otelStatus = this.otelBridge.isActive()
188
+ ? "gen_ai.client.token.usage → active"
189
+ : "disabled";
190
+ const lines = [
191
+ "\nToken Economy:",
192
+ `├─ Session: ${report.sessionId.slice(0, 8)} (${uptimeStr})`,
193
+ `├─ Estimator: ${report.estimatorName}`,
194
+ `├─ Operations: ${report.operations.length}`,
195
+ ...typeLines,
196
+ `├─ Total: ${report.totalInputTokens.toLocaleString()} in / ${report.totalOutputTokens.toLocaleString()} out (${totalCombined.toLocaleString()} combined)`,
197
+ ...toolLines,
198
+ ...(avgLatency > 0 ? [`├─ Avg Latency: ${avgLatency}ms`] : []),
199
+ `└─ OTel: ${otelStatus}`,
200
+ ];
201
+ return lines.join("\n");
202
+ }
203
+ /** Format milliseconds into human-readable uptime string */
204
+ formatUptime(ms) {
205
+ const seconds = Math.floor(ms / 1000);
206
+ if (seconds < 60)
207
+ return `${seconds}s`;
208
+ const minutes = Math.floor(seconds / 60);
209
+ const remainingSeconds = seconds % 60;
210
+ if (minutes < 60)
211
+ return `${minutes}m ${remainingSeconds}s`;
212
+ const hours = Math.floor(minutes / 60);
213
+ const remainingMinutes = minutes % 60;
214
+ return `${hours}h ${remainingMinutes}m`;
215
+ }
216
+ /** Reset all recorded metrics (used in tests) */
217
+ reset() {
218
+ this.operations = [];
219
+ }
220
+ /** Destroy the singleton (used in tests to guarantee isolation) */
221
+ static destroy() {
222
+ TokenTelemetryEngine.instance = null;
223
+ }
224
+ }