@revealui/ai 0.1.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.
- package/LICENSE +22 -0
- package/LICENSE.commercial +112 -0
- package/README.md +314 -0
- package/dist/a2a/card.d.ts +26 -0
- package/dist/a2a/card.d.ts.map +1 -0
- package/dist/a2a/card.js +173 -0
- package/dist/a2a/handler.d.ts +26 -0
- package/dist/a2a/handler.d.ts.map +1 -0
- package/dist/a2a/handler.js +170 -0
- package/dist/a2a/index.d.ts +10 -0
- package/dist/a2a/index.d.ts.map +1 -0
- package/dist/a2a/index.js +9 -0
- package/dist/a2a/task-store.d.ts +42 -0
- package/dist/a2a/task-store.d.ts.map +1 -0
- package/dist/a2a/task-store.js +99 -0
- package/dist/audit/emitter.d.ts +34 -0
- package/dist/audit/emitter.d.ts.map +1 -0
- package/dist/audit/emitter.js +34 -0
- package/dist/audit/index.d.ts +44 -0
- package/dist/audit/index.d.ts.map +1 -0
- package/dist/audit/index.js +48 -0
- package/dist/audit/observer.d.ts +108 -0
- package/dist/audit/observer.d.ts.map +1 -0
- package/dist/audit/observer.js +271 -0
- package/dist/audit/policy.d.ts +70 -0
- package/dist/audit/policy.d.ts.map +1 -0
- package/dist/audit/policy.js +209 -0
- package/dist/audit/store.d.ts +42 -0
- package/dist/audit/store.d.ts.map +1 -0
- package/dist/audit/store.js +80 -0
- package/dist/audit/types.d.ts +169 -0
- package/dist/audit/types.d.ts.map +1 -0
- package/dist/audit/types.js +80 -0
- package/dist/client/hooks/index.d.ts +22 -0
- package/dist/client/hooks/index.d.ts.map +1 -0
- package/dist/client/hooks/index.js +21 -0
- package/dist/client/hooks/useAgentContext.d.ts +30 -0
- package/dist/client/hooks/useAgentContext.d.ts.map +1 -0
- package/dist/client/hooks/useAgentContext.js +161 -0
- package/dist/client/hooks/useAgentEvents.d.ts +126 -0
- package/dist/client/hooks/useAgentEvents.d.ts.map +1 -0
- package/dist/client/hooks/useAgentEvents.js +232 -0
- package/dist/client/hooks/useAgentStream.d.ts +44 -0
- package/dist/client/hooks/useAgentStream.d.ts.map +1 -0
- package/dist/client/hooks/useAgentStream.js +101 -0
- package/dist/client/hooks/useEpisodicMemory.d.ts +25 -0
- package/dist/client/hooks/useEpisodicMemory.d.ts.map +1 -0
- package/dist/client/hooks/useEpisodicMemory.js +174 -0
- package/dist/client/hooks/useWorkingMemory.d.ts +57 -0
- package/dist/client/hooks/useWorkingMemory.d.ts.map +1 -0
- package/dist/client/hooks/useWorkingMemory.js +276 -0
- package/dist/client/index.d.ts +14 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +13 -0
- package/dist/embeddings/index.d.ts +51 -0
- package/dist/embeddings/index.d.ts.map +1 -0
- package/dist/embeddings/index.js +73 -0
- package/dist/index.d.ts +83 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +103 -0
- package/dist/inference/context-assembly.d.ts +27 -0
- package/dist/inference/context-assembly.d.ts.map +1 -0
- package/dist/inference/context-assembly.js +81 -0
- package/dist/inference/overflow-compressor.d.ts +17 -0
- package/dist/inference/overflow-compressor.d.ts.map +1 -0
- package/dist/inference/overflow-compressor.js +40 -0
- package/dist/inference/runRag.d.ts +35 -0
- package/dist/inference/runRag.d.ts.map +1 -0
- package/dist/inference/runRag.js +53 -0
- package/dist/ingestion/bm25.d.ts +29 -0
- package/dist/ingestion/bm25.d.ts.map +1 -0
- package/dist/ingestion/bm25.js +161 -0
- package/dist/ingestion/cms-indexer.d.ts +39 -0
- package/dist/ingestion/cms-indexer.d.ts.map +1 -0
- package/dist/ingestion/cms-indexer.js +74 -0
- package/dist/ingestion/file-parsers.d.ts +51 -0
- package/dist/ingestion/file-parsers.d.ts.map +1 -0
- package/dist/ingestion/file-parsers.js +247 -0
- package/dist/ingestion/hybrid-search.d.ts +22 -0
- package/dist/ingestion/hybrid-search.d.ts.map +1 -0
- package/dist/ingestion/hybrid-search.js +63 -0
- package/dist/ingestion/index.d.ts +9 -0
- package/dist/ingestion/index.d.ts.map +1 -0
- package/dist/ingestion/index.js +8 -0
- package/dist/ingestion/pipeline.d.ts +35 -0
- package/dist/ingestion/pipeline.d.ts.map +1 -0
- package/dist/ingestion/pipeline.js +114 -0
- package/dist/ingestion/rag-vector-service.d.ts +34 -0
- package/dist/ingestion/rag-vector-service.d.ts.map +1 -0
- package/dist/ingestion/rag-vector-service.js +98 -0
- package/dist/ingestion/reranker.d.ts +10 -0
- package/dist/ingestion/reranker.d.ts.map +1 -0
- package/dist/ingestion/reranker.js +41 -0
- package/dist/ingestion/text-splitter.d.ts +25 -0
- package/dist/ingestion/text-splitter.d.ts.map +1 -0
- package/dist/ingestion/text-splitter.js +119 -0
- package/dist/llm/cache-utils.d.ts +146 -0
- package/dist/llm/cache-utils.d.ts.map +1 -0
- package/dist/llm/cache-utils.js +204 -0
- package/dist/llm/client.d.ts +134 -0
- package/dist/llm/client.d.ts.map +1 -0
- package/dist/llm/client.js +497 -0
- package/dist/llm/key-validator.d.ts +25 -0
- package/dist/llm/key-validator.d.ts.map +1 -0
- package/dist/llm/key-validator.js +101 -0
- package/dist/llm/provider-health.d.ts +40 -0
- package/dist/llm/provider-health.d.ts.map +1 -0
- package/dist/llm/provider-health.js +97 -0
- package/dist/llm/providers/anthropic.d.ts +31 -0
- package/dist/llm/providers/anthropic.d.ts.map +1 -0
- package/dist/llm/providers/anthropic.js +248 -0
- package/dist/llm/providers/base.d.ts +111 -0
- package/dist/llm/providers/base.d.ts.map +1 -0
- package/dist/llm/providers/base.js +6 -0
- package/dist/llm/providers/groq.d.ts +23 -0
- package/dist/llm/providers/groq.d.ts.map +1 -0
- package/dist/llm/providers/groq.js +27 -0
- package/dist/llm/providers/ollama.d.ts +27 -0
- package/dist/llm/providers/ollama.d.ts.map +1 -0
- package/dist/llm/providers/ollama.js +48 -0
- package/dist/llm/providers/openai.d.ts +19 -0
- package/dist/llm/providers/openai.d.ts.map +1 -0
- package/dist/llm/providers/openai.js +245 -0
- package/dist/llm/providers/vultr.d.ts +18 -0
- package/dist/llm/providers/vultr.d.ts.map +1 -0
- package/dist/llm/providers/vultr.js +168 -0
- package/dist/llm/response-cache.d.ts +166 -0
- package/dist/llm/response-cache.d.ts.map +1 -0
- package/dist/llm/response-cache.js +233 -0
- package/dist/llm/semantic-cache.d.ts +179 -0
- package/dist/llm/semantic-cache.d.ts.map +1 -0
- package/dist/llm/semantic-cache.js +306 -0
- package/dist/llm/server.d.ts +14 -0
- package/dist/llm/server.d.ts.map +1 -0
- package/dist/llm/server.js +15 -0
- package/dist/llm/token-counter.d.ts +48 -0
- package/dist/llm/token-counter.d.ts.map +1 -0
- package/dist/llm/token-counter.js +77 -0
- package/dist/llm/workspace-provider-config.d.ts +38 -0
- package/dist/llm/workspace-provider-config.d.ts.map +1 -0
- package/dist/llm/workspace-provider-config.js +47 -0
- package/dist/memory/agent/context-manager.d.ts +148 -0
- package/dist/memory/agent/context-manager.d.ts.map +1 -0
- package/dist/memory/agent/context-manager.js +284 -0
- package/dist/memory/agent/index.d.ts +7 -0
- package/dist/memory/agent/index.d.ts.map +1 -0
- package/dist/memory/agent/index.js +6 -0
- package/dist/memory/crdt/index.d.ts +13 -0
- package/dist/memory/crdt/index.d.ts.map +1 -0
- package/dist/memory/crdt/index.js +12 -0
- package/dist/memory/crdt/lww-register.d.ts +108 -0
- package/dist/memory/crdt/lww-register.d.ts.map +1 -0
- package/dist/memory/crdt/lww-register.js +169 -0
- package/dist/memory/crdt/or-set.d.ts +141 -0
- package/dist/memory/crdt/or-set.d.ts.map +1 -0
- package/dist/memory/crdt/or-set.js +291 -0
- package/dist/memory/crdt/pn-counter.d.ts +116 -0
- package/dist/memory/crdt/pn-counter.d.ts.map +1 -0
- package/dist/memory/crdt/pn-counter.js +174 -0
- package/dist/memory/crdt/vector-clock.d.ts +115 -0
- package/dist/memory/crdt/vector-clock.d.ts.map +1 -0
- package/dist/memory/crdt/vector-clock.js +179 -0
- package/dist/memory/errors/index.d.ts +56 -0
- package/dist/memory/errors/index.d.ts.map +1 -0
- package/dist/memory/errors/index.js +85 -0
- package/dist/memory/index.d.ts +21 -0
- package/dist/memory/index.d.ts.map +1 -0
- package/dist/memory/index.js +20 -0
- package/dist/memory/persistence/crdt-persistence.d.ts +85 -0
- package/dist/memory/persistence/crdt-persistence.d.ts.map +1 -0
- package/dist/memory/persistence/crdt-persistence.js +204 -0
- package/dist/memory/persistence/index.d.ts +7 -0
- package/dist/memory/persistence/index.d.ts.map +1 -0
- package/dist/memory/persistence/index.js +6 -0
- package/dist/memory/preferences/index.d.ts +7 -0
- package/dist/memory/preferences/index.d.ts.map +1 -0
- package/dist/memory/preferences/index.js +6 -0
- package/dist/memory/preferences/user-preferences-manager.d.ts +133 -0
- package/dist/memory/preferences/user-preferences-manager.d.ts.map +1 -0
- package/dist/memory/preferences/user-preferences-manager.js +342 -0
- package/dist/memory/services/index.d.ts +8 -0
- package/dist/memory/services/index.d.ts.map +1 -0
- package/dist/memory/services/index.js +6 -0
- package/dist/memory/services/node-id-service.d.ts +75 -0
- package/dist/memory/services/node-id-service.d.ts.map +1 -0
- package/dist/memory/services/node-id-service.js +190 -0
- package/dist/memory/stores/episodic-memory.d.ts +182 -0
- package/dist/memory/stores/episodic-memory.d.ts.map +1 -0
- package/dist/memory/stores/episodic-memory.js +378 -0
- package/dist/memory/stores/index.d.ts +16 -0
- package/dist/memory/stores/index.d.ts.map +1 -0
- package/dist/memory/stores/index.js +15 -0
- package/dist/memory/stores/procedural-memory.d.ts +89 -0
- package/dist/memory/stores/procedural-memory.d.ts.map +1 -0
- package/dist/memory/stores/procedural-memory.js +152 -0
- package/dist/memory/stores/semantic-memory.d.ts +92 -0
- package/dist/memory/stores/semantic-memory.d.ts.map +1 -0
- package/dist/memory/stores/semantic-memory.js +155 -0
- package/dist/memory/stores/working-memory.d.ts +225 -0
- package/dist/memory/stores/working-memory.d.ts.map +1 -0
- package/dist/memory/stores/working-memory.js +336 -0
- package/dist/memory/utils/deep-clone.d.ts +10 -0
- package/dist/memory/utils/deep-clone.d.ts.map +1 -0
- package/dist/memory/utils/deep-clone.js +9 -0
- package/dist/memory/utils/index.d.ts +8 -0
- package/dist/memory/utils/index.d.ts.map +1 -0
- package/dist/memory/utils/index.js +7 -0
- package/dist/memory/utils/logger.d.ts +21 -0
- package/dist/memory/utils/logger.d.ts.map +1 -0
- package/dist/memory/utils/logger.js +62 -0
- package/dist/memory/utils/sql-helpers.d.ts +97 -0
- package/dist/memory/utils/sql-helpers.d.ts.map +1 -0
- package/dist/memory/utils/sql-helpers.js +214 -0
- package/dist/memory/utils/validation.d.ts +62 -0
- package/dist/memory/utils/validation.d.ts.map +1 -0
- package/dist/memory/utils/validation.js +244 -0
- package/dist/memory/vector/index.d.ts +12 -0
- package/dist/memory/vector/index.d.ts.map +1 -0
- package/dist/memory/vector/index.js +14 -0
- package/dist/memory/vector/vector-memory-service.d.ts +88 -0
- package/dist/memory/vector/vector-memory-service.d.ts.map +1 -0
- package/dist/memory/vector/vector-memory-service.js +335 -0
- package/dist/observability/logger.d.ts +79 -0
- package/dist/observability/logger.d.ts.map +1 -0
- package/dist/observability/logger.js +165 -0
- package/dist/observability/metrics.d.ts +43 -0
- package/dist/observability/metrics.d.ts.map +1 -0
- package/dist/observability/metrics.js +197 -0
- package/dist/observability/query.d.ts +150 -0
- package/dist/observability/query.d.ts.map +1 -0
- package/dist/observability/query.js +339 -0
- package/dist/observability/types.d.ts +140 -0
- package/dist/observability/types.d.ts.map +1 -0
- package/dist/observability/types.js +6 -0
- package/dist/orchestration/agent.d.ts +98 -0
- package/dist/orchestration/agent.d.ts.map +1 -0
- package/dist/orchestration/agent.js +6 -0
- package/dist/orchestration/defaults.d.ts +21 -0
- package/dist/orchestration/defaults.d.ts.map +1 -0
- package/dist/orchestration/defaults.js +22 -0
- package/dist/orchestration/memory-integration.d.ts +58 -0
- package/dist/orchestration/memory-integration.d.ts.map +1 -0
- package/dist/orchestration/memory-integration.js +130 -0
- package/dist/orchestration/orchestrator.d.ts +67 -0
- package/dist/orchestration/orchestrator.d.ts.map +1 -0
- package/dist/orchestration/orchestrator.js +174 -0
- package/dist/orchestration/runtime.d.ts +82 -0
- package/dist/orchestration/runtime.d.ts.map +1 -0
- package/dist/orchestration/runtime.js +251 -0
- package/dist/orchestration/streaming-runtime.d.ts +36 -0
- package/dist/orchestration/streaming-runtime.d.ts.map +1 -0
- package/dist/orchestration/streaming-runtime.js +175 -0
- package/dist/orchestration/ticket-agent.d.ts +70 -0
- package/dist/orchestration/ticket-agent.d.ts.map +1 -0
- package/dist/orchestration/ticket-agent.js +146 -0
- package/dist/skills/activation/index.d.ts +7 -0
- package/dist/skills/activation/index.d.ts.map +1 -0
- package/dist/skills/activation/index.js +6 -0
- package/dist/skills/activation/skill-activator.d.ts +68 -0
- package/dist/skills/activation/skill-activator.d.ts.map +1 -0
- package/dist/skills/activation/skill-activator.js +224 -0
- package/dist/skills/catalog/catalog-search.d.ts +55 -0
- package/dist/skills/catalog/catalog-search.d.ts.map +1 -0
- package/dist/skills/catalog/catalog-search.js +111 -0
- package/dist/skills/catalog/catalog-types.d.ts +81 -0
- package/dist/skills/catalog/catalog-types.d.ts.map +1 -0
- package/dist/skills/catalog/catalog-types.js +66 -0
- package/dist/skills/catalog/index.d.ts +9 -0
- package/dist/skills/catalog/index.d.ts.map +1 -0
- package/dist/skills/catalog/index.js +7 -0
- package/dist/skills/catalog/vercel-catalog.d.ts +42 -0
- package/dist/skills/catalog/vercel-catalog.d.ts.map +1 -0
- package/dist/skills/catalog/vercel-catalog.js +189 -0
- package/dist/skills/compat/index.d.ts +9 -0
- package/dist/skills/compat/index.d.ts.map +1 -0
- package/dist/skills/compat/index.js +8 -0
- package/dist/skills/compat/skill-enhancer.d.ts +37 -0
- package/dist/skills/compat/skill-enhancer.d.ts.map +1 -0
- package/dist/skills/compat/skill-enhancer.js +76 -0
- package/dist/skills/compat/tool-mapper.d.ts +61 -0
- package/dist/skills/compat/tool-mapper.d.ts.map +1 -0
- package/dist/skills/compat/tool-mapper.js +168 -0
- package/dist/skills/compat/vercel-compat.d.ts +33 -0
- package/dist/skills/compat/vercel-compat.d.ts.map +1 -0
- package/dist/skills/compat/vercel-compat.js +132 -0
- package/dist/skills/index.d.ts +40 -0
- package/dist/skills/index.d.ts.map +1 -0
- package/dist/skills/index.js +47 -0
- package/dist/skills/integration/agent-skill-provider.d.ts +94 -0
- package/dist/skills/integration/agent-skill-provider.d.ts.map +1 -0
- package/dist/skills/integration/agent-skill-provider.js +161 -0
- package/dist/skills/integration/index.d.ts +7 -0
- package/dist/skills/integration/index.d.ts.map +1 -0
- package/dist/skills/integration/index.js +6 -0
- package/dist/skills/loader/github-loader.d.ts +61 -0
- package/dist/skills/loader/github-loader.d.ts.map +1 -0
- package/dist/skills/loader/github-loader.js +176 -0
- package/dist/skills/loader/index.d.ts +10 -0
- package/dist/skills/loader/index.d.ts.map +1 -0
- package/dist/skills/loader/index.js +9 -0
- package/dist/skills/loader/local-loader.d.ts +56 -0
- package/dist/skills/loader/local-loader.d.ts.map +1 -0
- package/dist/skills/loader/local-loader.js +186 -0
- package/dist/skills/loader/vercel-loader.d.ts +64 -0
- package/dist/skills/loader/vercel-loader.d.ts.map +1 -0
- package/dist/skills/loader/vercel-loader.js +313 -0
- package/dist/skills/loader/vercel-types.d.ts +64 -0
- package/dist/skills/loader/vercel-types.d.ts.map +1 -0
- package/dist/skills/loader/vercel-types.js +55 -0
- package/dist/skills/parser/index.d.ts +7 -0
- package/dist/skills/parser/index.d.ts.map +1 -0
- package/dist/skills/parser/index.js +6 -0
- package/dist/skills/parser/skill-md-parser.d.ts +64 -0
- package/dist/skills/parser/skill-md-parser.d.ts.map +1 -0
- package/dist/skills/parser/skill-md-parser.js +242 -0
- package/dist/skills/registry/index.d.ts +7 -0
- package/dist/skills/registry/index.d.ts.map +1 -0
- package/dist/skills/registry/index.js +6 -0
- package/dist/skills/registry/skill-registry.d.ts +133 -0
- package/dist/skills/registry/skill-registry.d.ts.map +1 -0
- package/dist/skills/registry/skill-registry.js +373 -0
- package/dist/skills/types.d.ts +216 -0
- package/dist/skills/types.d.ts.map +1 -0
- package/dist/skills/types.js +176 -0
- package/dist/templates/agent-spec.d.ts +138 -0
- package/dist/templates/agent-spec.d.ts.map +1 -0
- package/dist/templates/agent-spec.js +138 -0
- package/dist/templates/index.d.ts +56 -0
- package/dist/templates/index.d.ts.map +1 -0
- package/dist/templates/index.js +58 -0
- package/dist/templates/prompt-spec.d.ts +140 -0
- package/dist/templates/prompt-spec.d.ts.map +1 -0
- package/dist/templates/prompt-spec.js +210 -0
- package/dist/templates/skill-spec.d.ts +106 -0
- package/dist/templates/skill-spec.d.ts.map +1 -0
- package/dist/templates/skill-spec.js +119 -0
- package/dist/tools/base.d.ts +74 -0
- package/dist/tools/base.d.ts.map +1 -0
- package/dist/tools/base.js +6 -0
- package/dist/tools/cms/collection-tools.d.ts +36 -0
- package/dist/tools/cms/collection-tools.d.ts.map +1 -0
- package/dist/tools/cms/collection-tools.js +178 -0
- package/dist/tools/cms/factory.d.ts +89 -0
- package/dist/tools/cms/factory.d.ts.map +1 -0
- package/dist/tools/cms/factory.js +462 -0
- package/dist/tools/cms/global-tools.d.ts +21 -0
- package/dist/tools/cms/global-tools.d.ts.map +1 -0
- package/dist/tools/cms/global-tools.js +92 -0
- package/dist/tools/cms/index.d.ts +11 -0
- package/dist/tools/cms/index.d.ts.map +1 -0
- package/dist/tools/cms/index.js +11 -0
- package/dist/tools/cms/media-tools.d.ts +31 -0
- package/dist/tools/cms/media-tools.d.ts.map +1 -0
- package/dist/tools/cms/media-tools.js +140 -0
- package/dist/tools/cms/user-tools.d.ts +31 -0
- package/dist/tools/cms/user-tools.d.ts.map +1 -0
- package/dist/tools/cms/user-tools.js +135 -0
- package/dist/tools/deduplicator.d.ts +19 -0
- package/dist/tools/deduplicator.d.ts.map +1 -0
- package/dist/tools/deduplicator.js +53 -0
- package/dist/tools/document-summarizer.d.ts +11 -0
- package/dist/tools/document-summarizer.d.ts.map +1 -0
- package/dist/tools/document-summarizer.js +82 -0
- package/dist/tools/mcp-adapter.d.ts +66 -0
- package/dist/tools/mcp-adapter.d.ts.map +1 -0
- package/dist/tools/mcp-adapter.js +152 -0
- package/dist/tools/memory/index.d.ts +3 -0
- package/dist/tools/memory/index.d.ts.map +1 -0
- package/dist/tools/memory/index.js +1 -0
- package/dist/tools/memory/store-memory.d.ts +39 -0
- package/dist/tools/memory/store-memory.d.ts.map +1 -0
- package/dist/tools/memory/store-memory.js +94 -0
- package/dist/tools/registry.d.ts +14 -0
- package/dist/tools/registry.d.ts.map +1 -0
- package/dist/tools/registry.js +48 -0
- package/dist/tools/ticket-tools.d.ts +31 -0
- package/dist/tools/ticket-tools.d.ts.map +1 -0
- package/dist/tools/ticket-tools.js +74 -0
- package/dist/tools/web/duck-duck-go.d.ts +52 -0
- package/dist/tools/web/duck-duck-go.d.ts.map +1 -0
- package/dist/tools/web/duck-duck-go.js +202 -0
- package/dist/tools/web/exa.d.ts +34 -0
- package/dist/tools/web/exa.d.ts.map +1 -0
- package/dist/tools/web/exa.js +80 -0
- package/dist/tools/web/index.d.ts +6 -0
- package/dist/tools/web/index.d.ts.map +1 -0
- package/dist/tools/web/index.js +4 -0
- package/dist/tools/web/scraper.d.ts +9 -0
- package/dist/tools/web/scraper.d.ts.map +1 -0
- package/dist/tools/web/scraper.js +118 -0
- package/dist/tools/web/tavily.d.ts +32 -0
- package/dist/tools/web/tavily.d.ts.map +1 -0
- package/dist/tools/web/tavily.js +73 -0
- package/dist/tools/web/types.d.ts +31 -0
- package/dist/tools/web/types.d.ts.map +1 -0
- package/dist/tools/web/types.js +9 -0
- package/package.json +143 -0
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Positive-Negative Counter (PN-Counter)
|
|
3
|
+
*
|
|
4
|
+
* A CRDT counter that supports both increment and decrement operations.
|
|
5
|
+
* Uses two G-Counters (grow-only counters) internally: one for increments
|
|
6
|
+
* and one for decrements.
|
|
7
|
+
*
|
|
8
|
+
* Use cases:
|
|
9
|
+
* - Access counts
|
|
10
|
+
* - Usage metrics
|
|
11
|
+
* - Voting systems
|
|
12
|
+
* - Inventory tracking
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* const counter1 = new PNCounter('node-a')
|
|
17
|
+
* const counter2 = new PNCounter('node-b')
|
|
18
|
+
*
|
|
19
|
+
* counter1.increment(5)
|
|
20
|
+
* counter2.increment(3)
|
|
21
|
+
* counter2.decrement(2)
|
|
22
|
+
*
|
|
23
|
+
* const merged = counter1.merge(counter2)
|
|
24
|
+
* const total = merged.value() // 6 (5 + 3 - 2)
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export interface PNCounterData {
|
|
28
|
+
nodeId: string;
|
|
29
|
+
increments: Record<string, number>;
|
|
30
|
+
decrements: Record<string, number>;
|
|
31
|
+
}
|
|
32
|
+
export declare class PNCounter {
|
|
33
|
+
private nodeId;
|
|
34
|
+
private increments;
|
|
35
|
+
private decrements;
|
|
36
|
+
/**
|
|
37
|
+
* Creates a new PN-Counter.
|
|
38
|
+
* @param nodeId - Unique identifier for this node
|
|
39
|
+
*/
|
|
40
|
+
constructor(nodeId: string);
|
|
41
|
+
/**
|
|
42
|
+
* Increments the counter.
|
|
43
|
+
* @param delta - Amount to increment by (default: 1)
|
|
44
|
+
*/
|
|
45
|
+
increment(delta?: number): void;
|
|
46
|
+
/**
|
|
47
|
+
* Decrements the counter.
|
|
48
|
+
* @param delta - Amount to decrement by (default: 1)
|
|
49
|
+
*/
|
|
50
|
+
decrement(delta?: number): void;
|
|
51
|
+
/**
|
|
52
|
+
* Gets the current value of the counter.
|
|
53
|
+
* @returns The sum of all increments minus the sum of all decrements
|
|
54
|
+
*/
|
|
55
|
+
value(): number;
|
|
56
|
+
/**
|
|
57
|
+
* Gets the total increment count.
|
|
58
|
+
*/
|
|
59
|
+
totalIncrements(): number;
|
|
60
|
+
/**
|
|
61
|
+
* Gets the total decrement count.
|
|
62
|
+
*/
|
|
63
|
+
totalDecrements(): number;
|
|
64
|
+
/**
|
|
65
|
+
* Gets the increment count for a specific node.
|
|
66
|
+
* @param nodeId - The node ID to query
|
|
67
|
+
*/
|
|
68
|
+
getIncrement(nodeId: string): number;
|
|
69
|
+
/**
|
|
70
|
+
* Gets the decrement count for a specific node.
|
|
71
|
+
* @param nodeId - The node ID to query
|
|
72
|
+
*/
|
|
73
|
+
getDecrement(nodeId: string): number;
|
|
74
|
+
/**
|
|
75
|
+
* Merges another PN-Counter into this one.
|
|
76
|
+
* Takes the maximum of each node's counters.
|
|
77
|
+
* @param other - The counter to merge
|
|
78
|
+
* @returns A new merged PNCounter
|
|
79
|
+
*/
|
|
80
|
+
merge(other: PNCounter): PNCounter;
|
|
81
|
+
/**
|
|
82
|
+
* Resets this node's counters to zero.
|
|
83
|
+
* Note: This doesn't affect the overall value if other nodes have contributions.
|
|
84
|
+
*/
|
|
85
|
+
reset(): void;
|
|
86
|
+
/**
|
|
87
|
+
* Creates a copy of this counter.
|
|
88
|
+
* @returns A new PNCounter with the same state
|
|
89
|
+
*/
|
|
90
|
+
clone(): PNCounter;
|
|
91
|
+
/**
|
|
92
|
+
* Serializes the counter to a plain object.
|
|
93
|
+
* @returns A serializable representation
|
|
94
|
+
*/
|
|
95
|
+
toData(): PNCounterData;
|
|
96
|
+
/**
|
|
97
|
+
* Deserializes a counter from serialized data.
|
|
98
|
+
* @param data - The serialized counter data
|
|
99
|
+
* @returns A new PNCounter instance
|
|
100
|
+
*/
|
|
101
|
+
static fromData(data: PNCounterData): PNCounter;
|
|
102
|
+
/**
|
|
103
|
+
* Creates a new counter with a generated node ID.
|
|
104
|
+
* @returns A new PNCounter with a UUID node ID
|
|
105
|
+
*/
|
|
106
|
+
static create(): PNCounter;
|
|
107
|
+
/**
|
|
108
|
+
* Gets all contributing node IDs.
|
|
109
|
+
*/
|
|
110
|
+
get nodes(): string[];
|
|
111
|
+
/**
|
|
112
|
+
* Returns a string representation for debugging.
|
|
113
|
+
*/
|
|
114
|
+
toString(): string;
|
|
115
|
+
}
|
|
116
|
+
//# sourceMappingURL=pn-counter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pn-counter.d.ts","sourceRoot":"","sources":["../../../src/memory/crdt/pn-counter.ts"],"names":[],"mappings":"AACA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAEH,MAAM,WAAW,aAAa;IAC5B,MAAM,EAAE,MAAM,CAAA;IACd,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IAClC,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;CACnC;AAED,qBAAa,SAAS;IACpB,OAAO,CAAC,MAAM,CAAQ;IACtB,OAAO,CAAC,UAAU,CAAqB;IACvC,OAAO,CAAC,UAAU,CAAqB;IAEvC;;;OAGG;gBACS,MAAM,EAAE,MAAM;IAU1B;;;OAGG;IACH,SAAS,CAAC,KAAK,GAAE,MAAU,GAAG,IAAI;IAQlC;;;OAGG;IACH,SAAS,CAAC,KAAK,GAAE,MAAU,GAAG,IAAI;IAQlC;;;OAGG;IACH,KAAK,IAAI,MAAM;IAaf;;OAEG;IACH,eAAe,IAAI,MAAM;IAQzB;;OAEG;IACH,eAAe,IAAI,MAAM;IAQzB;;;OAGG;IACH,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM;IAIpC;;;OAGG;IACH,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM;IAIpC;;;;;OAKG;IACH,KAAK,CAAC,KAAK,EAAE,SAAS,GAAG,SAAS;IA0BlC;;;OAGG;IACH,KAAK,IAAI,IAAI;IAKb;;;OAGG;IACH,KAAK,IAAI,SAAS;IAOlB;;;OAGG;IACH,MAAM,IAAI,aAAa;IAQvB;;;;OAIG;IACH,MAAM,CAAC,QAAQ,CAAC,IAAI,EAAE,aAAa,GAAG,SAAS;IAO/C;;;OAGG;IACH,MAAM,CAAC,MAAM,IAAI,SAAS;IAI1B;;OAEG;IACH,IAAI,KAAK,IAAI,MAAM,EAAE,CAEpB;IAED;;OAEG;IACH,QAAQ,IAAI,MAAM;CAGnB"}
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
export class PNCounter {
|
|
3
|
+
nodeId;
|
|
4
|
+
increments;
|
|
5
|
+
decrements;
|
|
6
|
+
/**
|
|
7
|
+
* Creates a new PN-Counter.
|
|
8
|
+
* @param nodeId - Unique identifier for this node
|
|
9
|
+
*/
|
|
10
|
+
constructor(nodeId) {
|
|
11
|
+
this.nodeId = nodeId;
|
|
12
|
+
this.increments = new Map();
|
|
13
|
+
this.decrements = new Map();
|
|
14
|
+
// Initialize this node's counters
|
|
15
|
+
this.increments.set(nodeId, 0);
|
|
16
|
+
this.decrements.set(nodeId, 0);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Increments the counter.
|
|
20
|
+
* @param delta - Amount to increment by (default: 1)
|
|
21
|
+
*/
|
|
22
|
+
increment(delta = 1) {
|
|
23
|
+
if (delta < 0) {
|
|
24
|
+
throw new Error('Increment delta must be non-negative. Use decrement for negative values.');
|
|
25
|
+
}
|
|
26
|
+
const current = this.increments.get(this.nodeId) ?? 0;
|
|
27
|
+
this.increments.set(this.nodeId, current + delta);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Decrements the counter.
|
|
31
|
+
* @param delta - Amount to decrement by (default: 1)
|
|
32
|
+
*/
|
|
33
|
+
decrement(delta = 1) {
|
|
34
|
+
if (delta < 0) {
|
|
35
|
+
throw new Error('Decrement delta must be non-negative. Use increment for negative values.');
|
|
36
|
+
}
|
|
37
|
+
const current = this.decrements.get(this.nodeId) ?? 0;
|
|
38
|
+
this.decrements.set(this.nodeId, current + delta);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Gets the current value of the counter.
|
|
42
|
+
* @returns The sum of all increments minus the sum of all decrements
|
|
43
|
+
*/
|
|
44
|
+
value() {
|
|
45
|
+
let total = 0;
|
|
46
|
+
for (const inc of this.increments.values()) {
|
|
47
|
+
total += inc;
|
|
48
|
+
}
|
|
49
|
+
for (const dec of this.decrements.values()) {
|
|
50
|
+
total -= dec;
|
|
51
|
+
}
|
|
52
|
+
return total;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Gets the total increment count.
|
|
56
|
+
*/
|
|
57
|
+
totalIncrements() {
|
|
58
|
+
let total = 0;
|
|
59
|
+
for (const inc of this.increments.values()) {
|
|
60
|
+
total += inc;
|
|
61
|
+
}
|
|
62
|
+
return total;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Gets the total decrement count.
|
|
66
|
+
*/
|
|
67
|
+
totalDecrements() {
|
|
68
|
+
let total = 0;
|
|
69
|
+
for (const dec of this.decrements.values()) {
|
|
70
|
+
total += dec;
|
|
71
|
+
}
|
|
72
|
+
return total;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Gets the increment count for a specific node.
|
|
76
|
+
* @param nodeId - The node ID to query
|
|
77
|
+
*/
|
|
78
|
+
getIncrement(nodeId) {
|
|
79
|
+
return this.increments.get(nodeId) ?? 0;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Gets the decrement count for a specific node.
|
|
83
|
+
* @param nodeId - The node ID to query
|
|
84
|
+
*/
|
|
85
|
+
getDecrement(nodeId) {
|
|
86
|
+
return this.decrements.get(nodeId) ?? 0;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Merges another PN-Counter into this one.
|
|
90
|
+
* Takes the maximum of each node's counters.
|
|
91
|
+
* @param other - The counter to merge
|
|
92
|
+
* @returns A new merged PNCounter
|
|
93
|
+
*/
|
|
94
|
+
merge(other) {
|
|
95
|
+
const merged = new PNCounter(this.nodeId);
|
|
96
|
+
// Get all unique node IDs
|
|
97
|
+
const allNodes = new Set([
|
|
98
|
+
...this.increments.keys(),
|
|
99
|
+
...this.decrements.keys(),
|
|
100
|
+
...other.increments.keys(),
|
|
101
|
+
...other.decrements.keys(),
|
|
102
|
+
]);
|
|
103
|
+
for (const nodeId of allNodes) {
|
|
104
|
+
// Max of increments
|
|
105
|
+
const thisInc = this.increments.get(nodeId) ?? 0;
|
|
106
|
+
const otherInc = other.increments.get(nodeId) ?? 0;
|
|
107
|
+
merged.increments.set(nodeId, Math.max(thisInc, otherInc));
|
|
108
|
+
// Max of decrements
|
|
109
|
+
const thisDec = this.decrements.get(nodeId) ?? 0;
|
|
110
|
+
const otherDec = other.decrements.get(nodeId) ?? 0;
|
|
111
|
+
merged.decrements.set(nodeId, Math.max(thisDec, otherDec));
|
|
112
|
+
}
|
|
113
|
+
return merged;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Resets this node's counters to zero.
|
|
117
|
+
* Note: This doesn't affect the overall value if other nodes have contributions.
|
|
118
|
+
*/
|
|
119
|
+
reset() {
|
|
120
|
+
this.increments.set(this.nodeId, 0);
|
|
121
|
+
this.decrements.set(this.nodeId, 0);
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Creates a copy of this counter.
|
|
125
|
+
* @returns A new PNCounter with the same state
|
|
126
|
+
*/
|
|
127
|
+
clone() {
|
|
128
|
+
const cloned = new PNCounter(this.nodeId);
|
|
129
|
+
cloned.increments = new Map(this.increments);
|
|
130
|
+
cloned.decrements = new Map(this.decrements);
|
|
131
|
+
return cloned;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Serializes the counter to a plain object.
|
|
135
|
+
* @returns A serializable representation
|
|
136
|
+
*/
|
|
137
|
+
toData() {
|
|
138
|
+
return {
|
|
139
|
+
nodeId: this.nodeId,
|
|
140
|
+
increments: Object.fromEntries(this.increments),
|
|
141
|
+
decrements: Object.fromEntries(this.decrements),
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Deserializes a counter from serialized data.
|
|
146
|
+
* @param data - The serialized counter data
|
|
147
|
+
* @returns A new PNCounter instance
|
|
148
|
+
*/
|
|
149
|
+
static fromData(data) {
|
|
150
|
+
const counter = new PNCounter(data.nodeId);
|
|
151
|
+
counter.increments = new Map(Object.entries(data.increments));
|
|
152
|
+
counter.decrements = new Map(Object.entries(data.decrements));
|
|
153
|
+
return counter;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Creates a new counter with a generated node ID.
|
|
157
|
+
* @returns A new PNCounter with a UUID node ID
|
|
158
|
+
*/
|
|
159
|
+
static create() {
|
|
160
|
+
return new PNCounter(randomUUID());
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Gets all contributing node IDs.
|
|
164
|
+
*/
|
|
165
|
+
get nodes() {
|
|
166
|
+
return Array.from(new Set([...this.increments.keys(), ...this.decrements.keys()]));
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Returns a string representation for debugging.
|
|
170
|
+
*/
|
|
171
|
+
toString() {
|
|
172
|
+
return `PNCounter(value=${this.value()}, inc=${this.totalIncrements()}, dec=${this.totalDecrements()})`;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Vector Clock for causal ordering across distributed nodes.
|
|
3
|
+
*
|
|
4
|
+
* A vector clock is a mechanism for tracking causality in distributed systems.
|
|
5
|
+
* Each node maintains a counter, and the clock can determine if one event
|
|
6
|
+
* happened before, after, or concurrently with another event.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* const clock1 = new VectorClock('node-a')
|
|
11
|
+
* clock1.tick('node-a') // { 'node-a': 1 }
|
|
12
|
+
*
|
|
13
|
+
* const clock2 = new VectorClock('node-b')
|
|
14
|
+
* clock2.tick('node-b') // { 'node-b': 1 }
|
|
15
|
+
*
|
|
16
|
+
* // Concurrent events
|
|
17
|
+
* clock1.compare(clock2) // 'concurrent'
|
|
18
|
+
*
|
|
19
|
+
* // After merge
|
|
20
|
+
* clock1.merge(clock2)
|
|
21
|
+
* clock1.tick('node-a')
|
|
22
|
+
* clock1.compare(clock2) // 'after'
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export interface VectorClockData {
|
|
26
|
+
clock: Record<string, number>;
|
|
27
|
+
}
|
|
28
|
+
export type VectorClockComparison = 'before' | 'after' | 'concurrent' | 'equal';
|
|
29
|
+
export declare class VectorClock {
|
|
30
|
+
private clock;
|
|
31
|
+
private nodeId;
|
|
32
|
+
/**
|
|
33
|
+
* Creates a new VectorClock instance.
|
|
34
|
+
* @param nodeId - Unique identifier for this node
|
|
35
|
+
*/
|
|
36
|
+
constructor(nodeId: string);
|
|
37
|
+
/**
|
|
38
|
+
* Increments the clock for a specific node.
|
|
39
|
+
* Typically called when this node performs an operation.
|
|
40
|
+
* @param nodeId - The node ID to increment (defaults to this node)
|
|
41
|
+
*/
|
|
42
|
+
tick(nodeId?: string): void;
|
|
43
|
+
/**
|
|
44
|
+
* Gets the current timestamp for a specific node.
|
|
45
|
+
* @param nodeId - The node ID to query
|
|
46
|
+
* @returns The current timestamp for the node, or 0 if not present
|
|
47
|
+
*/
|
|
48
|
+
get(nodeId: string): number;
|
|
49
|
+
/**
|
|
50
|
+
* Gets the timestamp for this node.
|
|
51
|
+
* @returns The current timestamp for this node
|
|
52
|
+
*/
|
|
53
|
+
getLocal(): number;
|
|
54
|
+
/**
|
|
55
|
+
* Merges another vector clock into this one.
|
|
56
|
+
* Takes the maximum of each node's counter.
|
|
57
|
+
* @param other - The vector clock to merge
|
|
58
|
+
*/
|
|
59
|
+
merge(other: VectorClock): void;
|
|
60
|
+
/**
|
|
61
|
+
* Compares this vector clock with another.
|
|
62
|
+
* @param other - The vector clock to compare against
|
|
63
|
+
* @returns The comparison result:
|
|
64
|
+
* - 'before': this happened before other
|
|
65
|
+
* - 'after': this happened after other
|
|
66
|
+
* - 'concurrent': events are concurrent (no causal relationship)
|
|
67
|
+
* - 'equal': clocks are identical
|
|
68
|
+
*/
|
|
69
|
+
compare(other: VectorClock): VectorClockComparison;
|
|
70
|
+
/**
|
|
71
|
+
* Checks if this clock happened before another.
|
|
72
|
+
* @param other - The vector clock to compare against
|
|
73
|
+
*/
|
|
74
|
+
happenedBefore(other: VectorClock): boolean;
|
|
75
|
+
/**
|
|
76
|
+
* Checks if this clock happened after another.
|
|
77
|
+
* @param other - The vector clock to compare against
|
|
78
|
+
*/
|
|
79
|
+
happenedAfter(other: VectorClock): boolean;
|
|
80
|
+
/**
|
|
81
|
+
* Checks if this clock is concurrent with another.
|
|
82
|
+
* @param other - The vector clock to compare against
|
|
83
|
+
*/
|
|
84
|
+
isConcurrent(other: VectorClock): boolean;
|
|
85
|
+
/**
|
|
86
|
+
* Creates a copy of this vector clock.
|
|
87
|
+
* @returns A new VectorClock with the same state
|
|
88
|
+
*/
|
|
89
|
+
clone(): VectorClock;
|
|
90
|
+
/**
|
|
91
|
+
* Serializes the vector clock to a plain object.
|
|
92
|
+
* @returns A serializable representation of the clock
|
|
93
|
+
*/
|
|
94
|
+
toData(): VectorClockData;
|
|
95
|
+
/**
|
|
96
|
+
* Deserializes a vector clock from serialized data.
|
|
97
|
+
* @param data - The serialized clock data
|
|
98
|
+
* @param nodeId - The node ID for the new clock instance
|
|
99
|
+
* @returns A new VectorClock instance
|
|
100
|
+
*/
|
|
101
|
+
static fromData(data: VectorClockData, nodeId: string): VectorClock;
|
|
102
|
+
/**
|
|
103
|
+
* Gets the total number of nodes tracked by this clock.
|
|
104
|
+
*/
|
|
105
|
+
get size(): number;
|
|
106
|
+
/**
|
|
107
|
+
* Gets all node IDs tracked by this clock.
|
|
108
|
+
*/
|
|
109
|
+
get nodes(): string[];
|
|
110
|
+
/**
|
|
111
|
+
* Returns a string representation for debugging.
|
|
112
|
+
*/
|
|
113
|
+
toString(): string;
|
|
114
|
+
}
|
|
115
|
+
//# sourceMappingURL=vector-clock.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vector-clock.d.ts","sourceRoot":"","sources":["../../../src/memory/crdt/vector-clock.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAEH,MAAM,WAAW,eAAe;IAC9B,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;CAC9B;AAED,MAAM,MAAM,qBAAqB,GAAG,QAAQ,GAAG,OAAO,GAAG,YAAY,GAAG,OAAO,CAAA;AAE/E,qBAAa,WAAW;IACtB,OAAO,CAAC,KAAK,CAAqB;IAClC,OAAO,CAAC,MAAM,CAAQ;IAEtB;;;OAGG;gBACS,MAAM,EAAE,MAAM;IAM1B;;;;OAIG;IACH,IAAI,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI;IAM3B;;;;OAIG;IACH,GAAG,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM;IAI3B;;;OAGG;IACH,QAAQ,IAAI,MAAM;IAIlB;;;;OAIG;IACH,KAAK,CAAC,KAAK,EAAE,WAAW,GAAG,IAAI;IAO/B;;;;;;;;OAQG;IACH,OAAO,CAAC,KAAK,EAAE,WAAW,GAAG,qBAAqB;IA+BlD;;;OAGG;IACH,cAAc,CAAC,KAAK,EAAE,WAAW,GAAG,OAAO;IAI3C;;;OAGG;IACH,aAAa,CAAC,KAAK,EAAE,WAAW,GAAG,OAAO;IAI1C;;;OAGG;IACH,YAAY,CAAC,KAAK,EAAE,WAAW,GAAG,OAAO;IAIzC;;;OAGG;IACH,KAAK,IAAI,WAAW;IAMpB;;;OAGG;IACH,MAAM,IAAI,eAAe;IAMzB;;;;;OAKG;IACH,MAAM,CAAC,QAAQ,CAAC,IAAI,EAAE,eAAe,EAAE,MAAM,EAAE,MAAM,GAAG,WAAW;IAMnE;;OAEG;IACH,IAAI,IAAI,IAAI,MAAM,CAEjB;IAED;;OAEG;IACH,IAAI,KAAK,IAAI,MAAM,EAAE,CAEpB;IAED;;OAEG;IACH,QAAQ,IAAI,MAAM;CAMnB"}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Vector Clock for causal ordering across distributed nodes.
|
|
3
|
+
*
|
|
4
|
+
* A vector clock is a mechanism for tracking causality in distributed systems.
|
|
5
|
+
* Each node maintains a counter, and the clock can determine if one event
|
|
6
|
+
* happened before, after, or concurrently with another event.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* const clock1 = new VectorClock('node-a')
|
|
11
|
+
* clock1.tick('node-a') // { 'node-a': 1 }
|
|
12
|
+
*
|
|
13
|
+
* const clock2 = new VectorClock('node-b')
|
|
14
|
+
* clock2.tick('node-b') // { 'node-b': 1 }
|
|
15
|
+
*
|
|
16
|
+
* // Concurrent events
|
|
17
|
+
* clock1.compare(clock2) // 'concurrent'
|
|
18
|
+
*
|
|
19
|
+
* // After merge
|
|
20
|
+
* clock1.merge(clock2)
|
|
21
|
+
* clock1.tick('node-a')
|
|
22
|
+
* clock1.compare(clock2) // 'after'
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export class VectorClock {
|
|
26
|
+
clock;
|
|
27
|
+
nodeId;
|
|
28
|
+
/**
|
|
29
|
+
* Creates a new VectorClock instance.
|
|
30
|
+
* @param nodeId - Unique identifier for this node
|
|
31
|
+
*/
|
|
32
|
+
constructor(nodeId) {
|
|
33
|
+
this.nodeId = nodeId;
|
|
34
|
+
this.clock = new Map();
|
|
35
|
+
this.clock.set(nodeId, 0);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Increments the clock for a specific node.
|
|
39
|
+
* Typically called when this node performs an operation.
|
|
40
|
+
* @param nodeId - The node ID to increment (defaults to this node)
|
|
41
|
+
*/
|
|
42
|
+
tick(nodeId) {
|
|
43
|
+
const id = nodeId ?? this.nodeId;
|
|
44
|
+
const current = this.clock.get(id) ?? 0;
|
|
45
|
+
this.clock.set(id, current + 1);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Gets the current timestamp for a specific node.
|
|
49
|
+
* @param nodeId - The node ID to query
|
|
50
|
+
* @returns The current timestamp for the node, or 0 if not present
|
|
51
|
+
*/
|
|
52
|
+
get(nodeId) {
|
|
53
|
+
return this.clock.get(nodeId) ?? 0;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Gets the timestamp for this node.
|
|
57
|
+
* @returns The current timestamp for this node
|
|
58
|
+
*/
|
|
59
|
+
getLocal() {
|
|
60
|
+
return this.get(this.nodeId);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Merges another vector clock into this one.
|
|
64
|
+
* Takes the maximum of each node's counter.
|
|
65
|
+
* @param other - The vector clock to merge
|
|
66
|
+
*/
|
|
67
|
+
merge(other) {
|
|
68
|
+
for (const [nodeId, timestamp] of other.clock) {
|
|
69
|
+
const current = this.clock.get(nodeId) ?? 0;
|
|
70
|
+
this.clock.set(nodeId, Math.max(current, timestamp));
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Compares this vector clock with another.
|
|
75
|
+
* @param other - The vector clock to compare against
|
|
76
|
+
* @returns The comparison result:
|
|
77
|
+
* - 'before': this happened before other
|
|
78
|
+
* - 'after': this happened after other
|
|
79
|
+
* - 'concurrent': events are concurrent (no causal relationship)
|
|
80
|
+
* - 'equal': clocks are identical
|
|
81
|
+
*/
|
|
82
|
+
compare(other) {
|
|
83
|
+
let lessThan = false;
|
|
84
|
+
let greaterThan = false;
|
|
85
|
+
// Get all unique node IDs
|
|
86
|
+
const allNodes = new Set([...this.clock.keys(), ...other.clock.keys()]);
|
|
87
|
+
for (const nodeId of allNodes) {
|
|
88
|
+
const thisValue = this.clock.get(nodeId) ?? 0;
|
|
89
|
+
const otherValue = other.clock.get(nodeId) ?? 0;
|
|
90
|
+
if (thisValue < otherValue) {
|
|
91
|
+
lessThan = true;
|
|
92
|
+
}
|
|
93
|
+
if (thisValue > otherValue) {
|
|
94
|
+
greaterThan = true;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
if (lessThan && greaterThan) {
|
|
98
|
+
return 'concurrent';
|
|
99
|
+
}
|
|
100
|
+
if (lessThan) {
|
|
101
|
+
return 'before';
|
|
102
|
+
}
|
|
103
|
+
if (greaterThan) {
|
|
104
|
+
return 'after';
|
|
105
|
+
}
|
|
106
|
+
return 'equal';
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Checks if this clock happened before another.
|
|
110
|
+
* @param other - The vector clock to compare against
|
|
111
|
+
*/
|
|
112
|
+
happenedBefore(other) {
|
|
113
|
+
return this.compare(other) === 'before';
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Checks if this clock happened after another.
|
|
117
|
+
* @param other - The vector clock to compare against
|
|
118
|
+
*/
|
|
119
|
+
happenedAfter(other) {
|
|
120
|
+
return this.compare(other) === 'after';
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Checks if this clock is concurrent with another.
|
|
124
|
+
* @param other - The vector clock to compare against
|
|
125
|
+
*/
|
|
126
|
+
isConcurrent(other) {
|
|
127
|
+
return this.compare(other) === 'concurrent';
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Creates a copy of this vector clock.
|
|
131
|
+
* @returns A new VectorClock with the same state
|
|
132
|
+
*/
|
|
133
|
+
clone() {
|
|
134
|
+
const cloned = new VectorClock(this.nodeId);
|
|
135
|
+
cloned.clock = new Map(this.clock);
|
|
136
|
+
return cloned;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Serializes the vector clock to a plain object.
|
|
140
|
+
* @returns A serializable representation of the clock
|
|
141
|
+
*/
|
|
142
|
+
toData() {
|
|
143
|
+
return {
|
|
144
|
+
clock: Object.fromEntries(this.clock),
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Deserializes a vector clock from serialized data.
|
|
149
|
+
* @param data - The serialized clock data
|
|
150
|
+
* @param nodeId - The node ID for the new clock instance
|
|
151
|
+
* @returns A new VectorClock instance
|
|
152
|
+
*/
|
|
153
|
+
static fromData(data, nodeId) {
|
|
154
|
+
const clock = new VectorClock(nodeId);
|
|
155
|
+
clock.clock = new Map(Object.entries(data.clock));
|
|
156
|
+
return clock;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Gets the total number of nodes tracked by this clock.
|
|
160
|
+
*/
|
|
161
|
+
get size() {
|
|
162
|
+
return this.clock.size;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Gets all node IDs tracked by this clock.
|
|
166
|
+
*/
|
|
167
|
+
get nodes() {
|
|
168
|
+
return Array.from(this.clock.keys());
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Returns a string representation for debugging.
|
|
172
|
+
*/
|
|
173
|
+
toString() {
|
|
174
|
+
const entries = Array.from(this.clock.entries())
|
|
175
|
+
.map(([k, v]) => `${k}:${v}`)
|
|
176
|
+
.join(', ');
|
|
177
|
+
return `VectorClock(${entries})`;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Memory System Errors
|
|
3
|
+
*
|
|
4
|
+
* Custom error classes for the memory system with proper error codes and types.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Base error class for memory system errors
|
|
8
|
+
*/
|
|
9
|
+
export declare class MemoryError extends Error {
|
|
10
|
+
readonly code: string;
|
|
11
|
+
readonly statusCode: number;
|
|
12
|
+
constructor(message: string, code: string, statusCode?: number);
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Database connection errors
|
|
16
|
+
*/
|
|
17
|
+
export declare class DatabaseConnectionError extends MemoryError {
|
|
18
|
+
constructor(message?: string, originalError?: Error);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Database constraint violation errors
|
|
22
|
+
*/
|
|
23
|
+
export declare class DatabaseConstraintError extends MemoryError {
|
|
24
|
+
constructor(message?: string, originalError?: Error);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Database query/operation errors
|
|
28
|
+
*/
|
|
29
|
+
export declare class DatabaseOperationError extends MemoryError {
|
|
30
|
+
constructor(message?: string, originalError?: Error);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Validation errors
|
|
34
|
+
*/
|
|
35
|
+
export declare class ValidationError extends MemoryError {
|
|
36
|
+
constructor(message: string);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Not found errors
|
|
40
|
+
*/
|
|
41
|
+
export declare class NotFoundError extends MemoryError {
|
|
42
|
+
constructor(resource: string);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Configuration errors
|
|
46
|
+
*/
|
|
47
|
+
export declare class ConfigurationError extends MemoryError {
|
|
48
|
+
constructor(message: string);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Data migration errors
|
|
52
|
+
*/
|
|
53
|
+
export declare class MigrationError extends MemoryError {
|
|
54
|
+
constructor(message: string);
|
|
55
|
+
}
|
|
56
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/memory/errors/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH;;GAEG;AACH,qBAAa,WAAY,SAAQ,KAAK;IACpC,SAAgB,IAAI,EAAE,MAAM,CAAA;IAC5B,SAAgB,UAAU,EAAE,MAAM,CAAA;gBAEtB,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,UAAU,SAAM;CAU5D;AAED;;GAEG;AACH,qBAAa,uBAAwB,SAAQ,WAAW;gBAC1C,OAAO,SAA+B,EAAE,aAAa,CAAC,EAAE,KAAK;CAQ1E;AAED;;GAEG;AACH,qBAAa,uBAAwB,SAAQ,WAAW;gBAC1C,OAAO,SAAkC,EAAE,aAAa,CAAC,EAAE,KAAK;CAQ7E;AAED;;GAEG;AACH,qBAAa,sBAAuB,SAAQ,WAAW;gBACzC,OAAO,SAA8B,EAAE,aAAa,CAAC,EAAE,KAAK;CAQzE;AAED;;GAEG;AACH,qBAAa,eAAgB,SAAQ,WAAW;gBAClC,OAAO,EAAE,MAAM;CAI5B;AAED;;GAEG;AACH,qBAAa,aAAc,SAAQ,WAAW;gBAChC,QAAQ,EAAE,MAAM;CAI7B;AAED;;GAEG;AACH,qBAAa,kBAAmB,SAAQ,WAAW;gBACrC,OAAO,EAAE,MAAM;CAI5B;AAED;;GAEG;AACH,qBAAa,cAAe,SAAQ,WAAW;gBACjC,OAAO,EAAE,MAAM;CAI5B"}
|