mcard-js 2.1.12 → 2.1.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -0
- package/dist/hash/HashValidator.d.ts +14 -0
- package/dist/hash/HashValidator.d.ts.map +1 -0
- package/dist/hash/HashValidator.js +57 -0
- package/dist/hash/HashValidator.js.map +1 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +38 -0
- package/dist/index.js.map +1 -0
- package/dist/model/CardCollection.d.ts +92 -0
- package/dist/model/CardCollection.d.ts.map +1 -0
- package/dist/model/CardCollection.js +217 -0
- package/dist/model/CardCollection.js.map +1 -0
- package/dist/model/ContentTypeInterpreter.d.ts +23 -0
- package/dist/model/ContentTypeInterpreter.d.ts.map +1 -0
- package/dist/model/ContentTypeInterpreter.js +140 -0
- package/dist/model/ContentTypeInterpreter.js.map +1 -0
- package/dist/model/EventProducer.d.ts +10 -0
- package/dist/model/EventProducer.d.ts.map +1 -0
- package/dist/model/EventProducer.js +46 -0
- package/dist/model/EventProducer.js.map +1 -0
- package/dist/model/GTime.d.ts +44 -0
- package/dist/model/GTime.d.ts.map +1 -0
- package/dist/model/GTime.js +91 -0
- package/dist/model/GTime.js.map +1 -0
- package/dist/model/Handle.d.ts +37 -0
- package/dist/model/Handle.d.ts.map +1 -0
- package/dist/model/Handle.js +90 -0
- package/dist/model/Handle.js.map +1 -0
- package/dist/model/MCard.d.ts +43 -0
- package/dist/model/MCard.d.ts.map +1 -0
- package/dist/model/MCard.js +76 -0
- package/dist/model/MCard.js.map +1 -0
- package/dist/model/constants.d.ts +45 -0
- package/dist/model/constants.d.ts.map +1 -0
- package/dist/model/constants.js +27 -0
- package/dist/model/constants.js.map +1 -0
- package/dist/model/detectors/BaseDetector.d.ts +26 -0
- package/dist/model/detectors/BaseDetector.d.ts.map +1 -0
- package/dist/model/detectors/BaseDetector.js +5 -0
- package/dist/model/detectors/BaseDetector.js.map +1 -0
- package/dist/model/detectors/BinaryDetector.d.ts +12 -0
- package/dist/model/detectors/BinaryDetector.d.ts.map +1 -0
- package/dist/model/detectors/BinaryDetector.js +82 -0
- package/dist/model/detectors/BinaryDetector.js.map +1 -0
- package/dist/model/detectors/ContentTypeInterpreter.d.ts +11 -0
- package/dist/model/detectors/ContentTypeInterpreter.d.ts.map +1 -0
- package/dist/model/detectors/ContentTypeInterpreter.js +51 -0
- package/dist/model/detectors/ContentTypeInterpreter.js.map +1 -0
- package/dist/model/detectors/DataFormatDetectors.d.ts +29 -0
- package/dist/model/detectors/DataFormatDetectors.d.ts.map +1 -0
- package/dist/model/detectors/DataFormatDetectors.js +223 -0
- package/dist/model/detectors/DataFormatDetectors.js.map +1 -0
- package/dist/model/detectors/LanguageDetector.d.ts +11 -0
- package/dist/model/detectors/LanguageDetector.d.ts.map +1 -0
- package/dist/model/detectors/LanguageDetector.js +148 -0
- package/dist/model/detectors/LanguageDetector.js.map +1 -0
- package/dist/model/detectors/MarkupDetectors.d.ts +22 -0
- package/dist/model/detectors/MarkupDetectors.d.ts.map +1 -0
- package/dist/model/detectors/MarkupDetectors.js +132 -0
- package/dist/model/detectors/MarkupDetectors.js.map +1 -0
- package/dist/model/detectors/OBJDetector.d.ts +8 -0
- package/dist/model/detectors/OBJDetector.d.ts.map +1 -0
- package/dist/model/detectors/OBJDetector.js +48 -0
- package/dist/model/detectors/OBJDetector.js.map +1 -0
- package/dist/model/detectors/registry.d.ts +13 -0
- package/dist/model/detectors/registry.d.ts.map +1 -0
- package/dist/model/detectors/registry.js +67 -0
- package/dist/model/detectors/registry.js.map +1 -0
- package/dist/monads/Either.d.ts +51 -0
- package/dist/monads/Either.d.ts.map +1 -0
- package/dist/monads/Either.js +82 -0
- package/dist/monads/Either.js.map +1 -0
- package/dist/monads/IO.d.ts +38 -0
- package/dist/monads/IO.d.ts.map +1 -0
- package/dist/monads/IO.js +66 -0
- package/dist/monads/IO.js.map +1 -0
- package/dist/monads/Maybe.d.ts +45 -0
- package/dist/monads/Maybe.d.ts.map +1 -0
- package/dist/monads/Maybe.js +73 -0
- package/dist/monads/Maybe.js.map +1 -0
- package/dist/monads/Reader.d.ts +29 -0
- package/dist/monads/Reader.d.ts.map +1 -0
- package/dist/monads/Reader.js +44 -0
- package/dist/monads/Reader.js.map +1 -0
- package/dist/monads/State.d.ts +33 -0
- package/dist/monads/State.d.ts.map +1 -0
- package/dist/monads/State.js +50 -0
- package/dist/monads/State.js.map +1 -0
- package/dist/monads/Writer.d.ts +29 -0
- package/dist/monads/Writer.d.ts.map +1 -0
- package/dist/monads/Writer.js +46 -0
- package/dist/monads/Writer.js.map +1 -0
- package/dist/ptr/FaroSidecar.d.ts +56 -0
- package/dist/ptr/FaroSidecar.d.ts.map +1 -0
- package/dist/ptr/FaroSidecar.js +102 -0
- package/dist/ptr/FaroSidecar.js.map +1 -0
- package/dist/ptr/LensProtocol.d.ts +84 -0
- package/dist/ptr/LensProtocol.d.ts.map +1 -0
- package/dist/ptr/LensProtocol.js +98 -0
- package/dist/ptr/LensProtocol.js.map +1 -0
- package/dist/ptr/SandboxWorker.d.ts +54 -0
- package/dist/ptr/SandboxWorker.d.ts.map +1 -0
- package/dist/ptr/SandboxWorker.js +172 -0
- package/dist/ptr/SandboxWorker.js.map +1 -0
- package/dist/ptr/common_types.d.ts +39 -0
- package/dist/ptr/common_types.d.ts.map +1 -0
- package/dist/ptr/common_types.js +12 -0
- package/dist/ptr/common_types.js.map +1 -0
- package/dist/ptr/lambda/AlphaConversion.d.ts +42 -0
- package/dist/ptr/lambda/AlphaConversion.d.ts.map +1 -0
- package/dist/ptr/lambda/AlphaConversion.js +244 -0
- package/dist/ptr/lambda/AlphaConversion.js.map +1 -0
- package/dist/ptr/lambda/BetaReduction.d.ts +73 -0
- package/dist/ptr/lambda/BetaReduction.d.ts.map +1 -0
- package/dist/ptr/lambda/BetaReduction.js +322 -0
- package/dist/ptr/lambda/BetaReduction.js.map +1 -0
- package/dist/ptr/lambda/EtaConversion.d.ts +65 -0
- package/dist/ptr/lambda/EtaConversion.d.ts.map +1 -0
- package/dist/ptr/lambda/EtaConversion.js +228 -0
- package/dist/ptr/lambda/EtaConversion.js.map +1 -0
- package/dist/ptr/lambda/FreeVariables.d.ts +44 -0
- package/dist/ptr/lambda/FreeVariables.d.ts.map +1 -0
- package/dist/ptr/lambda/FreeVariables.js +207 -0
- package/dist/ptr/lambda/FreeVariables.js.map +1 -0
- package/dist/ptr/lambda/LambdaRuntime.d.ts +80 -0
- package/dist/ptr/lambda/LambdaRuntime.d.ts.map +1 -0
- package/dist/ptr/lambda/LambdaRuntime.js +433 -0
- package/dist/ptr/lambda/LambdaRuntime.js.map +1 -0
- package/dist/ptr/lambda/LambdaTerm.d.ts +95 -0
- package/dist/ptr/lambda/LambdaTerm.d.ts.map +1 -0
- package/dist/ptr/lambda/LambdaTerm.js +159 -0
- package/dist/ptr/lambda/LambdaTerm.js.map +1 -0
- package/dist/ptr/lambda/index.d.ts +24 -0
- package/dist/ptr/lambda/index.d.ts.map +1 -0
- package/dist/ptr/lambda/index.js +34 -0
- package/dist/ptr/lambda/index.js.map +1 -0
- package/dist/ptr/llm/Config.d.ts +57 -0
- package/dist/ptr/llm/Config.d.ts.map +1 -0
- package/dist/ptr/llm/Config.js +183 -0
- package/dist/ptr/llm/Config.js.map +1 -0
- package/dist/ptr/llm/LLMRuntime.d.ts +35 -0
- package/dist/ptr/llm/LLMRuntime.d.ts.map +1 -0
- package/dist/ptr/llm/LLMRuntime.js +176 -0
- package/dist/ptr/llm/LLMRuntime.js.map +1 -0
- package/dist/ptr/llm/providers/LLMProvider.d.ts +43 -0
- package/dist/ptr/llm/providers/LLMProvider.d.ts.map +1 -0
- package/dist/ptr/llm/providers/LLMProvider.js +31 -0
- package/dist/ptr/llm/providers/LLMProvider.js.map +1 -0
- package/dist/ptr/llm/providers/MLCLLMProvider.d.ts +22 -0
- package/dist/ptr/llm/providers/MLCLLMProvider.d.ts.map +1 -0
- package/dist/ptr/llm/providers/MLCLLMProvider.js +155 -0
- package/dist/ptr/llm/providers/MLCLLMProvider.js.map +1 -0
- package/dist/ptr/llm/providers/OllamaEmbeddingProvider.d.ts +20 -0
- package/dist/ptr/llm/providers/OllamaEmbeddingProvider.d.ts.map +1 -0
- package/dist/ptr/llm/providers/OllamaEmbeddingProvider.js +42 -0
- package/dist/ptr/llm/providers/OllamaEmbeddingProvider.js.map +1 -0
- package/dist/ptr/llm/providers/OllamaProvider.d.ts +21 -0
- package/dist/ptr/llm/providers/OllamaProvider.d.ts.map +1 -0
- package/dist/ptr/llm/providers/OllamaProvider.js +158 -0
- package/dist/ptr/llm/providers/OllamaProvider.js.map +1 -0
- package/dist/ptr/llm/providers/WebLLMProvider.d.ts +22 -0
- package/dist/ptr/llm/providers/WebLLMProvider.d.ts.map +1 -0
- package/dist/ptr/llm/providers/WebLLMProvider.js +151 -0
- package/dist/ptr/llm/providers/WebLLMProvider.js.map +1 -0
- package/dist/ptr/node/Action.d.ts +426 -0
- package/dist/ptr/node/Action.d.ts.map +1 -0
- package/dist/ptr/node/Action.js +711 -0
- package/dist/ptr/node/Action.js.map +1 -0
- package/dist/ptr/node/CLMLoader.d.ts +18 -0
- package/dist/ptr/node/CLMLoader.d.ts.map +1 -0
- package/dist/ptr/node/CLMLoader.js +18 -0
- package/dist/ptr/node/CLMLoader.js.map +1 -0
- package/dist/ptr/node/CLMRunner.d.ts +34 -0
- package/dist/ptr/node/CLMRunner.d.ts.map +1 -0
- package/dist/ptr/node/CLMRunner.js +36 -0
- package/dist/ptr/node/CLMRunner.js.map +1 -0
- package/dist/ptr/node/FileSystemUtils.d.ts +40 -0
- package/dist/ptr/node/FileSystemUtils.d.ts.map +1 -0
- package/dist/ptr/node/FileSystemUtils.js +170 -0
- package/dist/ptr/node/FileSystemUtils.js.map +1 -0
- package/dist/ptr/node/NetworkConfig.d.ts +191 -0
- package/dist/ptr/node/NetworkConfig.d.ts.map +1 -0
- package/dist/ptr/node/NetworkConfig.js +8 -0
- package/dist/ptr/node/NetworkConfig.js.map +1 -0
- package/dist/ptr/node/NetworkRuntime.d.ts +38 -0
- package/dist/ptr/node/NetworkRuntime.d.ts.map +1 -0
- package/dist/ptr/node/NetworkRuntime.js +871 -0
- package/dist/ptr/node/NetworkRuntime.js.map +1 -0
- package/dist/ptr/node/P2PChatSession.d.ts +53 -0
- package/dist/ptr/node/P2PChatSession.d.ts.map +1 -0
- package/dist/ptr/node/P2PChatSession.js +154 -0
- package/dist/ptr/node/P2PChatSession.js.map +1 -0
- package/dist/ptr/node/RuntimeInterface.d.ts +4 -0
- package/dist/ptr/node/RuntimeInterface.d.ts.map +1 -0
- package/dist/ptr/node/RuntimeInterface.js +2 -0
- package/dist/ptr/node/RuntimeInterface.js.map +1 -0
- package/dist/ptr/node/Runtimes.d.ts +34 -0
- package/dist/ptr/node/Runtimes.d.ts.map +1 -0
- package/dist/ptr/node/Runtimes.js +38 -0
- package/dist/ptr/node/Runtimes.js.map +1 -0
- package/dist/ptr/node/SignalingServer.d.ts +37 -0
- package/dist/ptr/node/SignalingServer.d.ts.map +1 -0
- package/dist/ptr/node/SignalingServer.js +218 -0
- package/dist/ptr/node/SignalingServer.js.map +1 -0
- package/dist/ptr/node/cli.d.ts +3 -0
- package/dist/ptr/node/cli.d.ts.map +1 -0
- package/dist/ptr/node/cli.js +266 -0
- package/dist/ptr/node/cli.js.map +1 -0
- package/dist/ptr/node/clm/builtins/handle.d.ts +14 -0
- package/dist/ptr/node/clm/builtins/handle.d.ts.map +1 -0
- package/dist/ptr/node/clm/builtins/handle.js +177 -0
- package/dist/ptr/node/clm/builtins/handle.js.map +1 -0
- package/dist/ptr/node/clm/builtins/index.d.ts +20 -0
- package/dist/ptr/node/clm/builtins/index.d.ts.map +1 -0
- package/dist/ptr/node/clm/builtins/index.js +60 -0
- package/dist/ptr/node/clm/builtins/index.js.map +1 -0
- package/dist/ptr/node/clm/index.d.ts +25 -0
- package/dist/ptr/node/clm/index.d.ts.map +1 -0
- package/dist/ptr/node/clm/index.js +28 -0
- package/dist/ptr/node/clm/index.js.map +1 -0
- package/dist/ptr/node/clm/loader.d.ts +25 -0
- package/dist/ptr/node/clm/loader.d.ts.map +1 -0
- package/dist/ptr/node/clm/loader.js +146 -0
- package/dist/ptr/node/clm/loader.js.map +1 -0
- package/dist/ptr/node/clm/multiruntime.d.ts +9 -0
- package/dist/ptr/node/clm/multiruntime.d.ts.map +1 -0
- package/dist/ptr/node/clm/multiruntime.js +121 -0
- package/dist/ptr/node/clm/multiruntime.js.map +1 -0
- package/dist/ptr/node/clm/runner.d.ts +70 -0
- package/dist/ptr/node/clm/runner.d.ts.map +1 -0
- package/dist/ptr/node/clm/runner.js +324 -0
- package/dist/ptr/node/clm/runner.js.map +1 -0
- package/dist/ptr/node/clm/types.d.ts +166 -0
- package/dist/ptr/node/clm/types.d.ts.map +1 -0
- package/dist/ptr/node/clm/types.js +5 -0
- package/dist/ptr/node/clm/types.js.map +1 -0
- package/dist/ptr/node/clm/utils.d.ts +29 -0
- package/dist/ptr/node/clm/utils.d.ts.map +1 -0
- package/dist/ptr/node/clm/utils.js +88 -0
- package/dist/ptr/node/clm/utils.js.map +1 -0
- package/dist/ptr/node/network/HttpClient.d.ts +14 -0
- package/dist/ptr/node/network/HttpClient.d.ts.map +1 -0
- package/dist/ptr/node/network/HttpClient.js +139 -0
- package/dist/ptr/node/network/HttpClient.js.map +1 -0
- package/dist/ptr/node/network/MCardSerialization.d.ts +19 -0
- package/dist/ptr/node/network/MCardSerialization.d.ts.map +1 -0
- package/dist/ptr/node/network/MCardSerialization.js +41 -0
- package/dist/ptr/node/network/MCardSerialization.js.map +1 -0
- package/dist/ptr/node/network/NetworkInfrastructure.d.ts +37 -0
- package/dist/ptr/node/network/NetworkInfrastructure.d.ts.map +1 -0
- package/dist/ptr/node/network/NetworkInfrastructure.js +121 -0
- package/dist/ptr/node/network/NetworkInfrastructure.js.map +1 -0
- package/dist/ptr/node/network/NetworkSecurity.d.ts +24 -0
- package/dist/ptr/node/network/NetworkSecurity.d.ts.map +1 -0
- package/dist/ptr/node/network/NetworkSecurity.js +104 -0
- package/dist/ptr/node/network/NetworkSecurity.js.map +1 -0
- package/dist/ptr/node/runtimes/base.d.ts +31 -0
- package/dist/ptr/node/runtimes/base.d.ts.map +1 -0
- package/dist/ptr/node/runtimes/base.js +33 -0
- package/dist/ptr/node/runtimes/base.js.map +1 -0
- package/dist/ptr/node/runtimes/binary.d.ts +10 -0
- package/dist/ptr/node/runtimes/binary.d.ts.map +1 -0
- package/dist/ptr/node/runtimes/binary.js +16 -0
- package/dist/ptr/node/runtimes/binary.js.map +1 -0
- package/dist/ptr/node/runtimes/factory.d.ts +22 -0
- package/dist/ptr/node/runtimes/factory.d.ts.map +1 -0
- package/dist/ptr/node/runtimes/factory.js +152 -0
- package/dist/ptr/node/runtimes/factory.js.map +1 -0
- package/dist/ptr/node/runtimes/index.d.ts +28 -0
- package/dist/ptr/node/runtimes/index.d.ts.map +1 -0
- package/dist/ptr/node/runtimes/index.js +30 -0
- package/dist/ptr/node/runtimes/index.js.map +1 -0
- package/dist/ptr/node/runtimes/javascript.d.ts +22 -0
- package/dist/ptr/node/runtimes/javascript.d.ts.map +1 -0
- package/dist/ptr/node/runtimes/javascript.js +125 -0
- package/dist/ptr/node/runtimes/javascript.js.map +1 -0
- package/dist/ptr/node/runtimes/lean.d.ts +10 -0
- package/dist/ptr/node/runtimes/lean.d.ts.map +1 -0
- package/dist/ptr/node/runtimes/lean.js +60 -0
- package/dist/ptr/node/runtimes/lean.js.map +1 -0
- package/dist/ptr/node/runtimes/loader.d.ts +21 -0
- package/dist/ptr/node/runtimes/loader.d.ts.map +1 -0
- package/dist/ptr/node/runtimes/loader.js +126 -0
- package/dist/ptr/node/runtimes/loader.js.map +1 -0
- package/dist/ptr/node/runtimes/python.d.ts +14 -0
- package/dist/ptr/node/runtimes/python.d.ts.map +1 -0
- package/dist/ptr/node/runtimes/python.js +100 -0
- package/dist/ptr/node/runtimes/python.js.map +1 -0
- package/dist/ptr/node/runtimes/wasm.d.ts +10 -0
- package/dist/ptr/node/runtimes/wasm.d.ts.map +1 -0
- package/dist/ptr/node/runtimes/wasm.js +55 -0
- package/dist/ptr/node/runtimes/wasm.js.map +1 -0
- package/dist/rag/GraphRAGEngine.d.ts +26 -0
- package/dist/rag/GraphRAGEngine.d.ts.map +1 -0
- package/dist/rag/GraphRAGEngine.js +166 -0
- package/dist/rag/GraphRAGEngine.js.map +1 -0
- package/dist/rag/HandleVectorStore.d.ts +201 -0
- package/dist/rag/HandleVectorStore.d.ts.map +1 -0
- package/dist/rag/HandleVectorStore.js +527 -0
- package/dist/rag/HandleVectorStore.js.map +1 -0
- package/dist/rag/PersistentIndexer.d.ts +148 -0
- package/dist/rag/PersistentIndexer.d.ts.map +1 -0
- package/dist/rag/PersistentIndexer.js +302 -0
- package/dist/rag/PersistentIndexer.js.map +1 -0
- package/dist/rag/embeddings/VisionEmbeddingProvider.d.ts +87 -0
- package/dist/rag/embeddings/VisionEmbeddingProvider.d.ts.map +1 -0
- package/dist/rag/embeddings/VisionEmbeddingProvider.js +164 -0
- package/dist/rag/embeddings/VisionEmbeddingProvider.js.map +1 -0
- package/dist/rag/graph/community.d.ts +56 -0
- package/dist/rag/graph/community.d.ts.map +1 -0
- package/dist/rag/graph/community.js +247 -0
- package/dist/rag/graph/community.js.map +1 -0
- package/dist/rag/graph/extractor.d.ts +99 -0
- package/dist/rag/graph/extractor.d.ts.map +1 -0
- package/dist/rag/graph/extractor.js +210 -0
- package/dist/rag/graph/extractor.js.map +1 -0
- package/dist/rag/graph/store.d.ts +176 -0
- package/dist/rag/graph/store.d.ts.map +1 -0
- package/dist/rag/graph/store.js +504 -0
- package/dist/rag/graph/store.js.map +1 -0
- package/dist/rag/index.d.ts +20 -0
- package/dist/rag/index.d.ts.map +1 -0
- package/dist/rag/index.js +26 -0
- package/dist/rag/index.js.map +1 -0
- package/dist/rag/semanticVersioning.d.ts +187 -0
- package/dist/rag/semanticVersioning.d.ts.map +1 -0
- package/dist/rag/semanticVersioning.js +253 -0
- package/dist/rag/semanticVersioning.js.map +1 -0
- package/dist/storage/IndexedDBEngine.d.ts +33 -0
- package/dist/storage/IndexedDBEngine.d.ts.map +1 -0
- package/dist/storage/IndexedDBEngine.js +198 -0
- package/dist/storage/IndexedDBEngine.js.map +1 -0
- package/dist/storage/SqliteNodeEngine.d.ts +123 -0
- package/dist/storage/SqliteNodeEngine.d.ts.map +1 -0
- package/dist/storage/SqliteNodeEngine.js +385 -0
- package/dist/storage/SqliteNodeEngine.js.map +1 -0
- package/dist/storage/SqliteWasmEngine.d.ts +60 -0
- package/dist/storage/SqliteWasmEngine.d.ts.map +1 -0
- package/dist/storage/SqliteWasmEngine.js +193 -0
- package/dist/storage/SqliteWasmEngine.js.map +1 -0
- package/dist/storage/StorageAdapter.d.ts +42 -0
- package/dist/storage/StorageAdapter.d.ts.map +1 -0
- package/dist/storage/StorageAdapter.js +2 -0
- package/dist/storage/StorageAdapter.js.map +1 -0
- package/dist/storage/VectorStore.d.ts +202 -0
- package/dist/storage/VectorStore.d.ts.map +1 -0
- package/dist/storage/VectorStore.js +557 -0
- package/dist/storage/VectorStore.js.map +1 -0
- package/dist/storage/schema.d.ts +162 -0
- package/dist/storage/schema.d.ts.map +1 -0
- package/dist/storage/schema.js +373 -0
- package/dist/storage/schema.js.map +1 -0
- package/dist/util/FileIO.d.ts +44 -0
- package/dist/util/FileIO.d.ts.map +1 -0
- package/dist/util/FileIO.js +203 -0
- package/dist/util/FileIO.js.map +1 -0
- package/dist/util/Loader.d.ts +34 -0
- package/dist/util/Loader.d.ts.map +1 -0
- package/dist/util/Loader.js +168 -0
- package/dist/util/Loader.js.map +1 -0
- package/package.json +2 -1
|
@@ -0,0 +1,871 @@
|
|
|
1
|
+
import * as http from 'http';
|
|
2
|
+
import { P2PChatSession } from './P2PChatSession.js';
|
|
3
|
+
import { createSignalingServer } from './SignalingServer.js';
|
|
4
|
+
import { NetworkSecurity } from './network/NetworkSecurity.js';
|
|
5
|
+
import { MCardSerialization } from './network/MCardSerialization.js';
|
|
6
|
+
import { RateLimiter, NetworkCache } from './network/NetworkInfrastructure.js';
|
|
7
|
+
import { HttpClient } from './network/HttpClient.js';
|
|
8
|
+
/**
|
|
9
|
+
* Network Runtime for handling declarative network operations.
|
|
10
|
+
*/
|
|
11
|
+
export class NetworkRuntime {
|
|
12
|
+
collection;
|
|
13
|
+
security;
|
|
14
|
+
cache;
|
|
15
|
+
rateLimiter;
|
|
16
|
+
httpClient;
|
|
17
|
+
sessions;
|
|
18
|
+
constructor(collection) {
|
|
19
|
+
this.collection = collection;
|
|
20
|
+
this.security = new NetworkSecurity();
|
|
21
|
+
this.cache = new NetworkCache(collection);
|
|
22
|
+
this.rateLimiter = new RateLimiter();
|
|
23
|
+
this.httpClient = new HttpClient(this.rateLimiter, this.cache);
|
|
24
|
+
this.sessions = new Map();
|
|
25
|
+
}
|
|
26
|
+
async execute(_code, context, config, _chapterDir) {
|
|
27
|
+
const builtin = config.builtin;
|
|
28
|
+
if (!builtin) {
|
|
29
|
+
throw new Error('NetworkRuntime requires "builtin" to be defined in config.');
|
|
30
|
+
}
|
|
31
|
+
switch (builtin) {
|
|
32
|
+
case 'http_request':
|
|
33
|
+
return this.handleHttpRequest(config.config || {}, context);
|
|
34
|
+
case 'http_get':
|
|
35
|
+
return this.handleHttpGet(config.config || {}, context);
|
|
36
|
+
case 'http_post':
|
|
37
|
+
return this.handleHttpPost(config.config || {}, context);
|
|
38
|
+
case 'load_url':
|
|
39
|
+
return this.handleLoadUrl(config.config || {}, context);
|
|
40
|
+
case 'mcard_send':
|
|
41
|
+
return this.handleMCardSend(config.config || {}, context);
|
|
42
|
+
case 'listen_http':
|
|
43
|
+
return this.handleListenHttp(config.config || {}, context);
|
|
44
|
+
case 'mcard_sync':
|
|
45
|
+
return this.handleMCardSync(config.config || {}, context);
|
|
46
|
+
case 'listen_sync':
|
|
47
|
+
return this.handleListenSync(config.config || {}, context);
|
|
48
|
+
case 'webrtc_connect':
|
|
49
|
+
return this.handleWebRTCConnect(config.config || {}, context);
|
|
50
|
+
case 'webrtc_listen':
|
|
51
|
+
return this.handleWebRTCListen(config.config || {}, context);
|
|
52
|
+
case 'session_record':
|
|
53
|
+
return this.handleSessionRecord(config.config || {}, context);
|
|
54
|
+
case 'mcard_read':
|
|
55
|
+
return this.handleMCardRead(config.config || {}, context);
|
|
56
|
+
case 'run_command':
|
|
57
|
+
return this.handleRunCommand(config.config, context);
|
|
58
|
+
case 'clm_orchestrator':
|
|
59
|
+
return this.handleOrchestrator(config.config || {}, context);
|
|
60
|
+
case 'signaling_server':
|
|
61
|
+
return this.handleSignalingServer(config.config || {}, context);
|
|
62
|
+
default:
|
|
63
|
+
throw new Error(`Unknown network builtin: ${builtin}`);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
async handleHttpGet(config, context) {
|
|
67
|
+
return this.handleHttpRequest({ ...config, method: 'GET' }, context);
|
|
68
|
+
}
|
|
69
|
+
async handleHttpPost(config, context) {
|
|
70
|
+
const params = { ...config, method: 'POST' };
|
|
71
|
+
if (config.json) {
|
|
72
|
+
params.headers = { ...params.headers, 'Content-Type': 'application/json' };
|
|
73
|
+
params.body = JSON.stringify(config.json);
|
|
74
|
+
}
|
|
75
|
+
return this.handleHttpRequest(params, context);
|
|
76
|
+
}
|
|
77
|
+
async handleHttpRequest(config, context) {
|
|
78
|
+
// Interpolate URL
|
|
79
|
+
const url = this.interpolate(config.url, context);
|
|
80
|
+
// Security Validation
|
|
81
|
+
this.security.validateUrl(url);
|
|
82
|
+
const method = config.method || 'GET';
|
|
83
|
+
const headers = this.interpolateHeaders(config.headers || {}, context);
|
|
84
|
+
let body = config.body;
|
|
85
|
+
if (typeof body === 'string') {
|
|
86
|
+
body = this.interpolate(body, context);
|
|
87
|
+
}
|
|
88
|
+
else if (typeof body === 'object' && body !== null) {
|
|
89
|
+
body = JSON.stringify(body);
|
|
90
|
+
}
|
|
91
|
+
// Add Query Params
|
|
92
|
+
const fetchUrl = new URL(url);
|
|
93
|
+
if (config.query_params) {
|
|
94
|
+
for (const [key, value] of Object.entries(config.query_params)) {
|
|
95
|
+
fetchUrl.searchParams.append(key, this.interpolate(String(value), context));
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return this.httpClient.request(fetchUrl.toString(), method, headers, body, {
|
|
99
|
+
retry: config.retry,
|
|
100
|
+
cache: config.cache,
|
|
101
|
+
timeout: typeof config.timeout === 'number' ? config.timeout : config.timeout?.total,
|
|
102
|
+
responseType: config.response_type
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
async handleLoadUrl(config, context) {
|
|
106
|
+
const url = this.interpolate(config.url, context);
|
|
107
|
+
this.security.validateUrl(url);
|
|
108
|
+
try {
|
|
109
|
+
const res = await fetch(url);
|
|
110
|
+
const text = await res.text();
|
|
111
|
+
return {
|
|
112
|
+
url,
|
|
113
|
+
content: text,
|
|
114
|
+
status: res.status,
|
|
115
|
+
headers: Object.fromEntries(res.headers.entries())
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
catch (e) {
|
|
119
|
+
return { success: false, error: String(e) };
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
async handleMCardSend(config, context) {
|
|
123
|
+
if (!this.collection) {
|
|
124
|
+
throw new Error('MCard Send requires a CardCollection.');
|
|
125
|
+
}
|
|
126
|
+
const hash = this.interpolate(config.hash, context);
|
|
127
|
+
const url = this.interpolate(config.url, context);
|
|
128
|
+
const card = await this.collection.get(hash);
|
|
129
|
+
if (!card) {
|
|
130
|
+
return { success: false, error: `MCard not found: ${hash}` };
|
|
131
|
+
}
|
|
132
|
+
const payload = MCardSerialization.serialize(card);
|
|
133
|
+
return this.handleHttpPost({
|
|
134
|
+
url,
|
|
135
|
+
json: payload,
|
|
136
|
+
headers: config.headers
|
|
137
|
+
}, context);
|
|
138
|
+
}
|
|
139
|
+
async handleListenHttp(config, context) {
|
|
140
|
+
const port = Number(this.interpolate(String(config.port || 3000), context));
|
|
141
|
+
const path = this.interpolate(config.path || '/mcard', context);
|
|
142
|
+
return new Promise((resolve, reject) => {
|
|
143
|
+
const server = http.createServer(async (req, res) => {
|
|
144
|
+
if (req.method === 'POST' && req.url === path) {
|
|
145
|
+
const bodyChunks = [];
|
|
146
|
+
req.on('data', chunk => bodyChunks.push(chunk));
|
|
147
|
+
req.on('end', async () => {
|
|
148
|
+
try {
|
|
149
|
+
const body = Buffer.concat(bodyChunks).toString();
|
|
150
|
+
const json = JSON.parse(body);
|
|
151
|
+
const card = await MCardSerialization.deserialize(json);
|
|
152
|
+
if (json.hash) {
|
|
153
|
+
MCardSerialization.verifyHash(card, json.hash);
|
|
154
|
+
}
|
|
155
|
+
if (this.collection) {
|
|
156
|
+
await this.collection.add(card);
|
|
157
|
+
}
|
|
158
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
159
|
+
res.end(JSON.stringify({ success: true, hash: card.hash }));
|
|
160
|
+
}
|
|
161
|
+
catch (e) {
|
|
162
|
+
res.writeHead(400, { 'Content-Type': 'application/json' });
|
|
163
|
+
res.end(JSON.stringify({ success: false, error: String(e) }));
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
res.writeHead(404);
|
|
169
|
+
res.end();
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
server.listen(port, () => {
|
|
173
|
+
console.log(`[Network] Listening on port ${port} at ${path}`);
|
|
174
|
+
resolve({
|
|
175
|
+
success: true,
|
|
176
|
+
message: `Server started on port ${port}`
|
|
177
|
+
});
|
|
178
|
+
});
|
|
179
|
+
server.on('error', (err) => {
|
|
180
|
+
reject(err);
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
async handleMCardSync(config, context) {
|
|
185
|
+
if (!this.collection) {
|
|
186
|
+
throw new Error('MCard Sync requires a CardCollection.');
|
|
187
|
+
}
|
|
188
|
+
const mode = this.interpolate(config.mode || 'pull', context);
|
|
189
|
+
const urlParams = this.interpolate(config.url, context);
|
|
190
|
+
const url = urlParams.endsWith('/') ? urlParams.slice(0, -1) : urlParams;
|
|
191
|
+
// 1. Get Local Manifest
|
|
192
|
+
const localCards = await this.collection.getAllMCardsRaw();
|
|
193
|
+
const localHashes = new Set(localCards.map(c => c.hash));
|
|
194
|
+
// 2. Get Remote Manifest
|
|
195
|
+
const manifestRes = await this.handleHttpRequest({
|
|
196
|
+
url: `${url}/manifest`,
|
|
197
|
+
method: 'GET'
|
|
198
|
+
}, context);
|
|
199
|
+
if (!manifestRes.success) {
|
|
200
|
+
throw new Error(`Failed to fetch remote manifest: ${manifestRes.error?.message}`);
|
|
201
|
+
}
|
|
202
|
+
const remoteHashes = new Set(manifestRes.body);
|
|
203
|
+
const stats = {
|
|
204
|
+
mode,
|
|
205
|
+
local_total: localHashes.size,
|
|
206
|
+
remote_total: remoteHashes.size,
|
|
207
|
+
synced: 0,
|
|
208
|
+
pushed: 0,
|
|
209
|
+
pulled: 0
|
|
210
|
+
};
|
|
211
|
+
// Push Helper
|
|
212
|
+
const pushCards = async () => {
|
|
213
|
+
const toSend = [];
|
|
214
|
+
for (const card of localCards) {
|
|
215
|
+
if (!remoteHashes.has(card.hash)) {
|
|
216
|
+
toSend.push(card);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
if (toSend.length > 0) {
|
|
220
|
+
const payload = {
|
|
221
|
+
cards: toSend.map(card => MCardSerialization.serialize(card))
|
|
222
|
+
};
|
|
223
|
+
const pushRes = await this.handleHttpPost({
|
|
224
|
+
url: `${url}/batch`,
|
|
225
|
+
json: payload,
|
|
226
|
+
headers: config.headers
|
|
227
|
+
}, context);
|
|
228
|
+
if (!pushRes.success) {
|
|
229
|
+
throw new Error(`Failed to push batch: ${pushRes.error?.message}`);
|
|
230
|
+
}
|
|
231
|
+
return toSend.length;
|
|
232
|
+
}
|
|
233
|
+
return 0;
|
|
234
|
+
};
|
|
235
|
+
// Pull Helper
|
|
236
|
+
const pullCards = async () => {
|
|
237
|
+
const neededHashes = [];
|
|
238
|
+
for (const h of remoteHashes) {
|
|
239
|
+
if (!localHashes.has(h)) {
|
|
240
|
+
neededHashes.push(h);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
if (neededHashes.length > 0) {
|
|
244
|
+
const fetchRes = await this.handleHttpPost({
|
|
245
|
+
url: `${url}/get`,
|
|
246
|
+
json: { hashes: neededHashes },
|
|
247
|
+
headers: config.headers
|
|
248
|
+
}, context);
|
|
249
|
+
if (!fetchRes.success) {
|
|
250
|
+
throw new Error(`Failed to pull batch: ${fetchRes.error?.message}`);
|
|
251
|
+
}
|
|
252
|
+
const receivedCards = fetchRes.body.cards;
|
|
253
|
+
for (const json of receivedCards) {
|
|
254
|
+
const card = await MCardSerialization.deserialize(json);
|
|
255
|
+
await this.collection.add(card);
|
|
256
|
+
}
|
|
257
|
+
return receivedCards.length;
|
|
258
|
+
}
|
|
259
|
+
return 0;
|
|
260
|
+
};
|
|
261
|
+
if (mode === 'push') {
|
|
262
|
+
stats.pushed = await pushCards();
|
|
263
|
+
stats.synced = stats.pushed;
|
|
264
|
+
}
|
|
265
|
+
else if (mode === 'pull') {
|
|
266
|
+
stats.pulled = await pullCards();
|
|
267
|
+
stats.synced = stats.pulled;
|
|
268
|
+
}
|
|
269
|
+
else if (mode === 'both' || mode === 'bidirectional') {
|
|
270
|
+
const pushed = await pushCards();
|
|
271
|
+
const pulled = await pullCards();
|
|
272
|
+
stats.synced = pushed + pulled;
|
|
273
|
+
stats.pushed = pushed;
|
|
274
|
+
stats.pulled = pulled;
|
|
275
|
+
}
|
|
276
|
+
return { success: true, stats };
|
|
277
|
+
}
|
|
278
|
+
// ============ WebRTC Implementation ============
|
|
279
|
+
getPeerConnectionClass() {
|
|
280
|
+
if (typeof RTCPeerConnection !== 'undefined') {
|
|
281
|
+
return RTCPeerConnection;
|
|
282
|
+
}
|
|
283
|
+
else if (typeof global !== 'undefined' && global.RTCPeerConnection) {
|
|
284
|
+
return global.RTCPeerConnection;
|
|
285
|
+
}
|
|
286
|
+
return null;
|
|
287
|
+
}
|
|
288
|
+
async handleWebRTCConnect(config, context) {
|
|
289
|
+
const PeerConnection = this.getPeerConnectionClass();
|
|
290
|
+
if (!PeerConnection) {
|
|
291
|
+
return {
|
|
292
|
+
success: false,
|
|
293
|
+
error: 'WebRTC not supported in this environment (RTCPeerConnection not found).'
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
const signalingUrl = this.interpolate(config.signaling_url, context);
|
|
297
|
+
const targetPeerId = this.interpolate(config.target_peer_id, context);
|
|
298
|
+
const myPeerId = config.peer_id ? this.interpolate(config.peer_id, context) : `peer_${Date.now()}`;
|
|
299
|
+
const channelLabel = config.channel_label || 'mcard-sync';
|
|
300
|
+
// MOCK MODE for Testing
|
|
301
|
+
if (signalingUrl === 'mock://p2p') {
|
|
302
|
+
return new Promise(resolve => {
|
|
303
|
+
setTimeout(() => {
|
|
304
|
+
resolve({
|
|
305
|
+
success: true,
|
|
306
|
+
peer_id: myPeerId,
|
|
307
|
+
channel: channelLabel,
|
|
308
|
+
status: 'connected',
|
|
309
|
+
mock: true
|
|
310
|
+
});
|
|
311
|
+
}, 100);
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
console.log(`[WebRTC] Connecting to ${targetPeerId} via ${signalingUrl} as ${myPeerId}`);
|
|
315
|
+
const pc = new PeerConnection({
|
|
316
|
+
iceServers: config.ice_servers || [{ urls: 'stun:stun.l.google.com:19302' }]
|
|
317
|
+
});
|
|
318
|
+
const dc = pc.createDataChannel(channelLabel);
|
|
319
|
+
const connectionPromise = new Promise((resolve, reject) => {
|
|
320
|
+
const timeoutMs = config.timeout || 30000;
|
|
321
|
+
const timeoutId = setTimeout(() => {
|
|
322
|
+
pc.close();
|
|
323
|
+
reject(new Error('WebRTC connection timed out'));
|
|
324
|
+
}, timeoutMs);
|
|
325
|
+
dc.onopen = () => {
|
|
326
|
+
clearTimeout(timeoutId);
|
|
327
|
+
console.log(`[WebRTC] Data channel '${channelLabel}' open`);
|
|
328
|
+
if (config.message) {
|
|
329
|
+
const msg = typeof config.message === 'string'
|
|
330
|
+
? this.interpolate(config.message, context)
|
|
331
|
+
: JSON.stringify(config.message);
|
|
332
|
+
dc.send(msg);
|
|
333
|
+
}
|
|
334
|
+
resolve({
|
|
335
|
+
success: true,
|
|
336
|
+
peer_id: myPeerId,
|
|
337
|
+
channel: channelLabel,
|
|
338
|
+
status: 'connected'
|
|
339
|
+
});
|
|
340
|
+
};
|
|
341
|
+
dc.onerror = (err) => {
|
|
342
|
+
clearTimeout(timeoutId);
|
|
343
|
+
console.error('[WebRTC] Data channel error:', err);
|
|
344
|
+
reject(err);
|
|
345
|
+
};
|
|
346
|
+
this._setupP2PProtocol(dc);
|
|
347
|
+
});
|
|
348
|
+
const offer = await pc.createOffer();
|
|
349
|
+
await pc.setLocalDescription(offer);
|
|
350
|
+
console.log('[WebRTC] Local Offer created. SDP ready to send.');
|
|
351
|
+
if (config.await_response !== false) {
|
|
352
|
+
return connectionPromise;
|
|
353
|
+
}
|
|
354
|
+
return {
|
|
355
|
+
success: true,
|
|
356
|
+
status: 'initiating',
|
|
357
|
+
peer_id: myPeerId
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
_setupP2PProtocol(dc) {
|
|
361
|
+
dc.onmessage = async (event) => {
|
|
362
|
+
try {
|
|
363
|
+
const msg = JSON.parse(event.data);
|
|
364
|
+
if (msg.type === 'sync_manifest') {
|
|
365
|
+
if (!this.collection)
|
|
366
|
+
return;
|
|
367
|
+
const remoteHashes = new Set(msg.hashes);
|
|
368
|
+
const localCards = await this.collection.getAllMCardsRaw();
|
|
369
|
+
const localHashes = new Set(localCards.map(c => c.hash));
|
|
370
|
+
const needed = [...remoteHashes].filter((h) => !localHashes.has(h));
|
|
371
|
+
const toPush = localCards.filter(c => !remoteHashes.has(c.hash));
|
|
372
|
+
if (needed.length > 0) {
|
|
373
|
+
dc.send(JSON.stringify({ type: 'sync_request', hashes: needed }));
|
|
374
|
+
}
|
|
375
|
+
if (toPush.length > 0) {
|
|
376
|
+
const payload = {
|
|
377
|
+
type: 'batch_push',
|
|
378
|
+
cards: toPush.map(c => MCardSerialization.serialize(c))
|
|
379
|
+
};
|
|
380
|
+
dc.send(JSON.stringify(payload));
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
else if (msg.type === 'sync_request') {
|
|
384
|
+
if (!this.collection)
|
|
385
|
+
return;
|
|
386
|
+
const requested = msg.hashes || [];
|
|
387
|
+
const foundCards = [];
|
|
388
|
+
for (const h of requested) {
|
|
389
|
+
const c = await this.collection.get(h);
|
|
390
|
+
if (c)
|
|
391
|
+
foundCards.push(MCardSerialization.serialize(c));
|
|
392
|
+
}
|
|
393
|
+
if (foundCards.length > 0) {
|
|
394
|
+
dc.send(JSON.stringify({ type: 'batch_push', cards: foundCards }));
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
else if (msg.type === 'batch_push') {
|
|
398
|
+
if (!this.collection)
|
|
399
|
+
return;
|
|
400
|
+
const cards = msg.cards || [];
|
|
401
|
+
let added = 0;
|
|
402
|
+
for (const cJson of cards) {
|
|
403
|
+
const card = await MCardSerialization.deserialize(cJson);
|
|
404
|
+
await this.collection.add(card);
|
|
405
|
+
added++;
|
|
406
|
+
}
|
|
407
|
+
console.log(`[WebRTC] Synced ${added} cards from peer.`);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
catch (e) {
|
|
411
|
+
console.error('[WebRTC] Protocol error:', e);
|
|
412
|
+
}
|
|
413
|
+
};
|
|
414
|
+
}
|
|
415
|
+
async handleWebRTCListen(config, context) {
|
|
416
|
+
const PeerConnection = this.getPeerConnectionClass();
|
|
417
|
+
if (!PeerConnection) {
|
|
418
|
+
return {
|
|
419
|
+
success: false,
|
|
420
|
+
error: 'WebRTC not supported in this environment (RTCPeerConnection not found).'
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
const signalingUrl = this.interpolate(config.signaling_url, context);
|
|
424
|
+
const myPeerId = config.peer_id ? this.interpolate(config.peer_id, context) : `listener_${Date.now()}`;
|
|
425
|
+
if (signalingUrl === 'mock://p2p') {
|
|
426
|
+
return new Promise(resolve => {
|
|
427
|
+
setTimeout(() => {
|
|
428
|
+
resolve({
|
|
429
|
+
success: true,
|
|
430
|
+
peer_id: myPeerId,
|
|
431
|
+
status: 'listening',
|
|
432
|
+
mock: true
|
|
433
|
+
});
|
|
434
|
+
}, 100);
|
|
435
|
+
});
|
|
436
|
+
}
|
|
437
|
+
console.log(`[WebRTC] Listening on ${signalingUrl} as ${myPeerId}`);
|
|
438
|
+
return {
|
|
439
|
+
success: true,
|
|
440
|
+
status: 'listening',
|
|
441
|
+
peer_id: myPeerId,
|
|
442
|
+
note: 'Signaling loop implementation pending specific server protocol.'
|
|
443
|
+
};
|
|
444
|
+
}
|
|
445
|
+
async handleListenSync(config, context) {
|
|
446
|
+
if (!this.collection) {
|
|
447
|
+
throw new Error('Listen Sync requires a CardCollection.');
|
|
448
|
+
}
|
|
449
|
+
const port = Number(this.interpolate(String(config.port || 3000), context));
|
|
450
|
+
const basePath = this.interpolate(config.base_path || '/sync', context);
|
|
451
|
+
return new Promise((resolve, reject) => {
|
|
452
|
+
const server = http.createServer(async (req, res) => {
|
|
453
|
+
const url = req.url || '';
|
|
454
|
+
const readBody = async () => {
|
|
455
|
+
return new Promise((res, rej) => {
|
|
456
|
+
const chunks = [];
|
|
457
|
+
req.on('data', c => chunks.push(c));
|
|
458
|
+
req.on('end', () => {
|
|
459
|
+
try {
|
|
460
|
+
const str = Buffer.concat(chunks).toString();
|
|
461
|
+
res(JSON.parse(str || '{}'));
|
|
462
|
+
}
|
|
463
|
+
catch (e) {
|
|
464
|
+
rej(e);
|
|
465
|
+
}
|
|
466
|
+
});
|
|
467
|
+
req.on('error', rej);
|
|
468
|
+
});
|
|
469
|
+
};
|
|
470
|
+
try {
|
|
471
|
+
if (req.method === 'GET' && url === `${basePath}/manifest`) {
|
|
472
|
+
const all = await this.collection.getAllMCardsRaw();
|
|
473
|
+
const hashes = all.map(c => c.hash);
|
|
474
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
475
|
+
res.end(JSON.stringify(hashes));
|
|
476
|
+
return;
|
|
477
|
+
}
|
|
478
|
+
if (req.method === 'POST' && url === `${basePath}/batch`) {
|
|
479
|
+
const json = await readBody();
|
|
480
|
+
const cards = json.cards || [];
|
|
481
|
+
let added = 0;
|
|
482
|
+
for (const cJson of cards) {
|
|
483
|
+
const card = await MCardSerialization.deserialize(cJson);
|
|
484
|
+
await this.collection.add(card);
|
|
485
|
+
added++;
|
|
486
|
+
}
|
|
487
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
488
|
+
res.end(JSON.stringify({ success: true, added }));
|
|
489
|
+
return;
|
|
490
|
+
}
|
|
491
|
+
if (req.method === 'POST' && url === `${basePath}/get`) {
|
|
492
|
+
const json = await readBody();
|
|
493
|
+
const requestedHashes = json.hashes || [];
|
|
494
|
+
const foundCards = [];
|
|
495
|
+
for (const h of requestedHashes) {
|
|
496
|
+
const card = await this.collection.get(h);
|
|
497
|
+
if (card) {
|
|
498
|
+
foundCards.push(MCardSerialization.serialize(card));
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
502
|
+
res.end(JSON.stringify({ success: true, cards: foundCards }));
|
|
503
|
+
return;
|
|
504
|
+
}
|
|
505
|
+
res.writeHead(404);
|
|
506
|
+
res.end();
|
|
507
|
+
}
|
|
508
|
+
catch (e) {
|
|
509
|
+
res.writeHead(500, { 'Content-Type': 'application/json' });
|
|
510
|
+
res.end(JSON.stringify({ success: false, error: String(e) }));
|
|
511
|
+
}
|
|
512
|
+
});
|
|
513
|
+
server.listen(port, () => {
|
|
514
|
+
console.log(`[Network] Sync listening on port ${port} at ${basePath}`);
|
|
515
|
+
resolve({
|
|
516
|
+
success: true,
|
|
517
|
+
message: `Sync Server started on port ${port}`,
|
|
518
|
+
port,
|
|
519
|
+
basePath
|
|
520
|
+
});
|
|
521
|
+
});
|
|
522
|
+
server.on('error', (err) => {
|
|
523
|
+
reject(err);
|
|
524
|
+
});
|
|
525
|
+
});
|
|
526
|
+
}
|
|
527
|
+
interpolate(text, context) {
|
|
528
|
+
if (!text || typeof text !== 'string')
|
|
529
|
+
return text;
|
|
530
|
+
return text.replace(/\$\{([^}]+)\}/g, (_, path) => {
|
|
531
|
+
const keys = path.split('.');
|
|
532
|
+
let val = context;
|
|
533
|
+
for (const key of keys) {
|
|
534
|
+
if (val && typeof val === 'object' && key in val) {
|
|
535
|
+
val = val[key];
|
|
536
|
+
}
|
|
537
|
+
else {
|
|
538
|
+
return '';
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
return String(val);
|
|
542
|
+
});
|
|
543
|
+
}
|
|
544
|
+
interpolateHeaders(headers, context) {
|
|
545
|
+
const result = {};
|
|
546
|
+
for (const [key, val] of Object.entries(headers)) {
|
|
547
|
+
result[key] = this.interpolate(val, context);
|
|
548
|
+
}
|
|
549
|
+
return result;
|
|
550
|
+
}
|
|
551
|
+
async handleSessionRecord(config, context) {
|
|
552
|
+
if (!this.collection) {
|
|
553
|
+
throw new Error('Session Record requires a CardCollection.');
|
|
554
|
+
}
|
|
555
|
+
const sessionId = this.interpolate(config.sessionId, context);
|
|
556
|
+
let operation = config.operation || 'add';
|
|
557
|
+
if (typeof operation === 'string' && operation.includes('${')) {
|
|
558
|
+
operation = this.interpolate(operation, context);
|
|
559
|
+
}
|
|
560
|
+
if (operation === 'init') {
|
|
561
|
+
if (this.sessions.has(sessionId)) {
|
|
562
|
+
return { success: true, message: 'Session already exists', sessionId };
|
|
563
|
+
}
|
|
564
|
+
let bufferSize = config.maxBufferSize || 5;
|
|
565
|
+
if (typeof config.maxBufferSize === 'string') {
|
|
566
|
+
bufferSize = parseInt(this.interpolate(config.maxBufferSize, context), 10);
|
|
567
|
+
}
|
|
568
|
+
let initialHead = config.initialHeadHash || null;
|
|
569
|
+
if (typeof config.initialHeadHash === 'string') {
|
|
570
|
+
initialHead = this.interpolate(config.initialHeadHash, context);
|
|
571
|
+
if (initialHead === 'null' || initialHead === 'undefined' || initialHead === '')
|
|
572
|
+
initialHead = null;
|
|
573
|
+
}
|
|
574
|
+
const session = new P2PChatSession(this.collection, sessionId, bufferSize, initialHead);
|
|
575
|
+
this.sessions.set(sessionId, session);
|
|
576
|
+
return { success: true, message: 'Session initialized', sessionId, bufferSize, initialHead };
|
|
577
|
+
}
|
|
578
|
+
if (operation === 'batch') {
|
|
579
|
+
const results = [];
|
|
580
|
+
let subOps = config.operations;
|
|
581
|
+
if (!Array.isArray(subOps)) {
|
|
582
|
+
const ctx = context;
|
|
583
|
+
subOps = ctx?.params?.operations || ctx?.operations || [];
|
|
584
|
+
}
|
|
585
|
+
for (const op of subOps) {
|
|
586
|
+
const subConfig = { ...config, ...op };
|
|
587
|
+
results.push(await this.handleSessionRecord(subConfig, context));
|
|
588
|
+
}
|
|
589
|
+
return {
|
|
590
|
+
success: true,
|
|
591
|
+
operation: 'batch',
|
|
592
|
+
results
|
|
593
|
+
};
|
|
594
|
+
}
|
|
595
|
+
if (operation === 'summarize') {
|
|
596
|
+
let session = this.sessions.get(sessionId);
|
|
597
|
+
if (!session) {
|
|
598
|
+
session = new P2PChatSession(this.collection, sessionId, 5, null);
|
|
599
|
+
this.sessions.set(sessionId, session);
|
|
600
|
+
}
|
|
601
|
+
const keepOriginals = config.keepOriginals === true;
|
|
602
|
+
const summaryHash = await session.summarize(keepOriginals);
|
|
603
|
+
return {
|
|
604
|
+
success: true,
|
|
605
|
+
operation: 'summarize',
|
|
606
|
+
summary_hash: summaryHash,
|
|
607
|
+
sessionId
|
|
608
|
+
};
|
|
609
|
+
}
|
|
610
|
+
const session = this.sessions.get(sessionId);
|
|
611
|
+
if (!session) {
|
|
612
|
+
const newSession = new P2PChatSession(this.collection, sessionId, 5, null);
|
|
613
|
+
this.sessions.set(sessionId, newSession);
|
|
614
|
+
}
|
|
615
|
+
const validSession = this.sessions.get(sessionId);
|
|
616
|
+
if (operation === 'add') {
|
|
617
|
+
const sender = this.interpolate(config.sender || 'unknown', context);
|
|
618
|
+
const content = this.interpolate(config.content || '', context);
|
|
619
|
+
const hash = await validSession.addMessage(sender, content);
|
|
620
|
+
const head = validSession.getHeadHash();
|
|
621
|
+
return {
|
|
622
|
+
success: true,
|
|
623
|
+
checkpoint_hash: hash,
|
|
624
|
+
head_hash: head,
|
|
625
|
+
sessionId
|
|
626
|
+
};
|
|
627
|
+
}
|
|
628
|
+
else if (operation === 'flush') {
|
|
629
|
+
const hash = await validSession.checkpoint();
|
|
630
|
+
return {
|
|
631
|
+
success: true,
|
|
632
|
+
checkpoint_hash: hash,
|
|
633
|
+
sessionId
|
|
634
|
+
};
|
|
635
|
+
}
|
|
636
|
+
return { success: false, error: `Unknown operation ${operation}` };
|
|
637
|
+
}
|
|
638
|
+
async handleMCardRead(config, context) {
|
|
639
|
+
if (!this.collection) {
|
|
640
|
+
throw new Error('MCard Read requires a CardCollection.');
|
|
641
|
+
}
|
|
642
|
+
const hash = this.interpolate(config.hash, context);
|
|
643
|
+
if (!hash)
|
|
644
|
+
throw new Error('Hash is required for mcard_read');
|
|
645
|
+
const card = await this.collection.get(hash);
|
|
646
|
+
if (!card)
|
|
647
|
+
return { success: false, error: 'MCard not found', hash };
|
|
648
|
+
let content = card.getContentAsText();
|
|
649
|
+
if (config.parse_json !== false) {
|
|
650
|
+
try {
|
|
651
|
+
content = JSON.parse(content);
|
|
652
|
+
}
|
|
653
|
+
catch (e) {
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
return {
|
|
657
|
+
success: true,
|
|
658
|
+
hash,
|
|
659
|
+
content,
|
|
660
|
+
g_time: card.g_time
|
|
661
|
+
};
|
|
662
|
+
}
|
|
663
|
+
async handleOrchestrator(config, context) {
|
|
664
|
+
const steps = config.steps || [];
|
|
665
|
+
const state = {};
|
|
666
|
+
let allSuccess = true;
|
|
667
|
+
console.log(`[NetworkRuntime] Starting Orchestration with ${steps.length} steps.`);
|
|
668
|
+
for (const step of steps) {
|
|
669
|
+
const stepName = step.name || step.action;
|
|
670
|
+
console.log(`[Orchestrator] Step: ${stepName}`);
|
|
671
|
+
try {
|
|
672
|
+
if (step.action === 'start_process') {
|
|
673
|
+
const cmd = this.interpolate(step.command, context);
|
|
674
|
+
const { spawn } = await import('child_process');
|
|
675
|
+
const parts = cmd.split(' ');
|
|
676
|
+
const env = { ...process.env, ...(step.env || {}) };
|
|
677
|
+
const proc = spawn(parts[0], parts.slice(1), {
|
|
678
|
+
detached: true,
|
|
679
|
+
stdio: 'inherit',
|
|
680
|
+
cwd: process.cwd(),
|
|
681
|
+
env: env
|
|
682
|
+
});
|
|
683
|
+
proc.unref();
|
|
684
|
+
if (step.id_key) {
|
|
685
|
+
state[step.id_key] = proc.pid;
|
|
686
|
+
console.log(`[Orchestrator] Process started (PID: ${proc.pid}) stored in '${step.id_key}'`);
|
|
687
|
+
}
|
|
688
|
+
else {
|
|
689
|
+
console.log(`[Orchestrator] Process started (PID: ${proc.pid})`);
|
|
690
|
+
}
|
|
691
|
+
if (step.wait_after) {
|
|
692
|
+
await new Promise(r => setTimeout(r, step.wait_after));
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
else if (step.action === 'run_clm') {
|
|
696
|
+
if (!context.runCLM)
|
|
697
|
+
throw new Error("runCLM capability not available in context");
|
|
698
|
+
const file = step.file;
|
|
699
|
+
const input = step.input || {};
|
|
700
|
+
console.log(`[Orchestrator] Running CLM: ${file}`);
|
|
701
|
+
const res = await context.runCLM(file, input);
|
|
702
|
+
if (!res.success) {
|
|
703
|
+
console.error(`[Orchestrator] CLM Failed: ${file}`, res.error);
|
|
704
|
+
if (!step.continue_on_error) {
|
|
705
|
+
allSuccess = false;
|
|
706
|
+
break;
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
else {
|
|
710
|
+
console.log(`[Orchestrator] CLM Passed: ${file}`);
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
else if (step.action === 'run_clm_background') {
|
|
714
|
+
const file = step.file;
|
|
715
|
+
const filter = file.replace(/\.(yaml|yml|clm)$/i, '');
|
|
716
|
+
const cmd = `npx tsx examples/run-all-clms.ts ${filter}`;
|
|
717
|
+
const { spawn } = await import('child_process');
|
|
718
|
+
const parts = cmd.split(' ');
|
|
719
|
+
const env = { ...process.env, ...(step.env || {}) };
|
|
720
|
+
const proc = spawn(parts[0], parts.slice(1), {
|
|
721
|
+
detached: true,
|
|
722
|
+
stdio: 'inherit',
|
|
723
|
+
cwd: process.cwd(),
|
|
724
|
+
env: env
|
|
725
|
+
});
|
|
726
|
+
proc.unref();
|
|
727
|
+
if (step.id_key) {
|
|
728
|
+
state[step.id_key] = proc.pid;
|
|
729
|
+
console.log(`[Orchestrator] Background CLM started (PID: ${proc.pid}) stored in '${step.id_key}'`);
|
|
730
|
+
}
|
|
731
|
+
if (step.wait_after) {
|
|
732
|
+
await new Promise(r => setTimeout(r, step.wait_after));
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
else if (step.action === 'stop_process') {
|
|
736
|
+
const key = step.pid_key;
|
|
737
|
+
const pid = state[key];
|
|
738
|
+
if (pid) {
|
|
739
|
+
try {
|
|
740
|
+
context.process.kill(pid);
|
|
741
|
+
console.log(`[Orchestrator] Stopped process ${pid} (${key})`);
|
|
742
|
+
}
|
|
743
|
+
catch (e) {
|
|
744
|
+
console.warn(`[Orchestrator] Failed to stop process ${pid}: ${e}`);
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
else {
|
|
748
|
+
console.warn(`[Orchestrator] No PID found for key '${key}'`);
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
else if (step.action === 'sleep') {
|
|
752
|
+
const ms = step.ms || 1000;
|
|
753
|
+
await new Promise(r => setTimeout(r, ms));
|
|
754
|
+
}
|
|
755
|
+
else if (step.action === 'start_signaling_server') {
|
|
756
|
+
const port = step.port || 3000;
|
|
757
|
+
console.log(`[Orchestrator] Starting builtin signaling server on port ${port}...`);
|
|
758
|
+
const result = await this.handleSignalingServer({ port, background: true }, context);
|
|
759
|
+
if (result.success) {
|
|
760
|
+
console.log(`[Orchestrator] Signaling server started on port ${result.port}`);
|
|
761
|
+
if (step.id_key) {
|
|
762
|
+
state[step.id_key] = {
|
|
763
|
+
type: 'signaling_server',
|
|
764
|
+
port: result.port,
|
|
765
|
+
server: this._signalingServer
|
|
766
|
+
};
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
else {
|
|
770
|
+
console.error(`[Orchestrator] Failed to start signaling server: ${result.error}`);
|
|
771
|
+
if (!step.continue_on_error) {
|
|
772
|
+
allSuccess = false;
|
|
773
|
+
break;
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
if (step.wait_after) {
|
|
777
|
+
await new Promise(r => setTimeout(r, step.wait_after));
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
else if (step.action === 'stop_signaling_server') {
|
|
781
|
+
const key = step.id_key;
|
|
782
|
+
const serverInfo = state[key];
|
|
783
|
+
if (serverInfo && serverInfo.server) {
|
|
784
|
+
try {
|
|
785
|
+
serverInfo.server.close();
|
|
786
|
+
console.log(`[Orchestrator] Signaling server stopped (${key})`);
|
|
787
|
+
}
|
|
788
|
+
catch (e) {
|
|
789
|
+
console.warn(`[Orchestrator] Failed to stop signaling server: ${e}`);
|
|
790
|
+
}
|
|
791
|
+
}
|
|
792
|
+
else if (this._signalingServer) {
|
|
793
|
+
try {
|
|
794
|
+
this._signalingServer.close();
|
|
795
|
+
console.log(`[Orchestrator] Signaling server stopped`);
|
|
796
|
+
}
|
|
797
|
+
catch (e) {
|
|
798
|
+
console.warn(`[Orchestrator] Failed to stop signaling server: ${e}`);
|
|
799
|
+
}
|
|
800
|
+
}
|
|
801
|
+
else {
|
|
802
|
+
console.warn(`[Orchestrator] No signaling server found to stop`);
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
}
|
|
806
|
+
catch (e) {
|
|
807
|
+
console.error(`[Orchestrator] Step '${stepName}' caused error:`, e);
|
|
808
|
+
allSuccess = false;
|
|
809
|
+
if (!step.continue_on_error)
|
|
810
|
+
break;
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
return {
|
|
814
|
+
success: allSuccess,
|
|
815
|
+
state
|
|
816
|
+
};
|
|
817
|
+
}
|
|
818
|
+
async handleRunCommand(config, context) {
|
|
819
|
+
const command = this.interpolate(config.command, context);
|
|
820
|
+
console.log(`[NetworkRuntime] Executing command: ${command}`);
|
|
821
|
+
const { exec, spawn } = await import('child_process');
|
|
822
|
+
if (config.background) {
|
|
823
|
+
const parts = command.split(' ');
|
|
824
|
+
const cmd = parts[0];
|
|
825
|
+
const args = parts.slice(1);
|
|
826
|
+
const subprocess = spawn(cmd, args, {
|
|
827
|
+
detached: true,
|
|
828
|
+
stdio: 'ignore'
|
|
829
|
+
});
|
|
830
|
+
subprocess.unref();
|
|
831
|
+
console.log(`[NetworkRuntime] Started background process with PID: ${subprocess.pid}`);
|
|
832
|
+
return {
|
|
833
|
+
success: true,
|
|
834
|
+
pid: subprocess.pid,
|
|
835
|
+
message: "Background process started"
|
|
836
|
+
};
|
|
837
|
+
}
|
|
838
|
+
return new Promise((resolve, reject) => {
|
|
839
|
+
exec(command, (error, stdout, stderr) => {
|
|
840
|
+
if (error) {
|
|
841
|
+
console.error(`[NetworkRuntime] Command failed: ${error.message}`);
|
|
842
|
+
return resolve({
|
|
843
|
+
success: false,
|
|
844
|
+
error: error.message,
|
|
845
|
+
stderr
|
|
846
|
+
});
|
|
847
|
+
}
|
|
848
|
+
console.log(`[NetworkRuntime] Command output:\n${stdout}`);
|
|
849
|
+
resolve({
|
|
850
|
+
success: true,
|
|
851
|
+
stdout,
|
|
852
|
+
stderr
|
|
853
|
+
});
|
|
854
|
+
});
|
|
855
|
+
});
|
|
856
|
+
}
|
|
857
|
+
async handleSignalingServer(config, _context) {
|
|
858
|
+
const port = config.port || 3000;
|
|
859
|
+
console.log(`[NetworkRuntime] Starting signaling server on port ${port}...`);
|
|
860
|
+
const result = await createSignalingServer({
|
|
861
|
+
port,
|
|
862
|
+
autoFindPort: true,
|
|
863
|
+
maxPortTries: 10
|
|
864
|
+
});
|
|
865
|
+
if (result.success && result.server) {
|
|
866
|
+
this._signalingServer = result.server;
|
|
867
|
+
}
|
|
868
|
+
return result;
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
//# sourceMappingURL=NetworkRuntime.js.map
|