@nomyx/multi-context-objects
v1.0.8
Published
A library for managing multi-context objects in distributed applications
Downloads
18
Maintainers
Readme
MultiContext Framework
Overview
MultiContext is a sophisticated, event-driven framework designed for building distributed applications that can seamlessly operate across multiple contexts (e.g., client, server, edge). It provides a robust set of tools for managing state synchronization, secure method invocation, and AI-powered operations across different environments.
Key Features
- Multi-Context Operations: Easily define and execute methods that can run in different contexts (client, server, edge).
- State Synchronization: Automatic state synchronization across contexts using a custom StateSync mechanism.
- Secure Method Invocation: Implement and invoke methods with built-in security checks and encryption.
- AI Integration: Seamlessly integrate multiple AI providers (Anthropic, Vertex AI, Azure OpenAI, Meta) into your applications.
- Event Management: Robust event system for handling cross-context communication.
- Domain Management: Manage and verify different domains within your application.
- Caching and Rate Limiting: Built-in support for caching responses and rate limiting requests.
- Error Handling and Retry Logic: Sophisticated error handling with circuit breaker pattern and retry mechanisms.
Installation
npm install multicontext
Quick Start
- Create a MultiContextObject:
import { MultiContextObject, ContextType, DataProvider } from 'multicontext';
class MyObject extends MultiContextObject {
constructor(id: string, contexts: ContextType[], provider: DataProvider) {
super(id, 'MyObjectType', contexts, provider);
this.registerMethod('myMethod', 'client', this.myMethod.bind(this));
}
myMethod(arg: string): string {
return `Hello, ${arg}!`;
}
}
- Use the object:
const myObject = new MyObject('obj1', ['client', 'server'], dataProvider);
const result = await myObject.getProxy().myMethod('World');
console.log(result); // Output: Hello, World!
Core Concepts
MultiContextObject
The central class for creating objects that can operate across multiple contexts. It handles method registration, state management, and context switching.
ContextManager
Manages different contexts and their configurations. It allows you to define and switch between various execution environments.
NetworkManager
Handles network operations, including remote method invocation and state synchronization across different nodes in the network.
AIProviderManager
Manages different AI providers and allows seamless switching between them for AI-powered operations.
EventManager
Provides a robust event system for handling both local and cross-context events.
Advanced Usage
Secure Methods
Use the @encryptedFor
decorator to create methods that are encrypted for specific users:
import { encryptedFor } from 'multicontext/decorators';
class SecureObject {
@encryptedFor('userPublicKey')
secureMethod(arg: string): string {
return `Secure: ${arg}`;
}
}
AI-Powered Methods
Use the @withAIProvider
decorator to easily integrate AI capabilities:
import { withAIProvider } from 'multicontext/decorators';
class AIObject extends MultiContextObject {
@withAIProvider('anthropic')
async generateText(prompt: string): Promise<string> {
const aiProvider = AIProviderManager.getInstance().getCurrentProvider();
return aiProvider.generateResponse(prompt);
}
}
Caching and Rate Limiting
Use built-in decorators for caching and rate limiting:
import { cacheResult, rateLimit } from 'multicontext/decorators';
class OptimizedObject extends MultiContextObject {
@cacheResult(60000) // Cache for 1 minute
@rateLimit(5, 60000) // 5 requests per minute
async expensiveOperation(input: string): Promise<string> {
// ... perform expensive operation
}
}
Configuration
Use the Configuration
class to set up global settings:
import { Configuration } from 'multicontext';
const config = Configuration.getInstance();
config.setAiProvider('openai');
config.setMaxTokens(100);
config.setTemperature(0.7);
Error Handling
The framework provides robust error handling mechanisms:
import { ErrorHandler, RetryManager } from 'multicontext/utils';
const errorHandler = new ErrorHandler();
const retryManager = new RetryManager();
try {
await retryManager.retry(() => someOperation(), 3, 1000);
} catch (error) {
errorHandler.handleError(error, 'someOperation');
}
Contributing
We welcome contributions to the MultiContext framework! Please see our Contributing Guide for more details.
License
This project is licensed under the ISC License. See the LICENSE file for details.
Support
For support, please open an issue in the GitHub repository or contact our support team at [email protected].