A library for managing multi-context objects in distributed applications
npm install @nomyx/multi-context-objectsMultiContext 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.
- 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.
``bash`
npm install multicontext
1. Create a MultiContextObject:
`typescript
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}!;`
}
}
2. Use the object:
`typescript`
const myObject = new MyObject('obj1', ['client', 'server'], dataProvider);
const result = await myObject.getProxy().myMethod('World');
console.log(result); // Output: Hello, World!
The central class for creating objects that can operate across multiple contexts. It handles method registration, state management, and context switching.
Manages different contexts and their configurations. It allows you to define and switch between various execution environments.
Handles network operations, including remote method invocation and state synchronization across different nodes in the network.
Manages different AI providers and allows seamless switching between them for AI-powered operations.
Provides a robust event system for handling both local and cross-context events.
Use the @encryptedFor decorator to create methods that are encrypted for specific users:
`typescript
import { encryptedFor } from 'multicontext/decorators';
class SecureObject {
@encryptedFor('userPublicKey')
secureMethod(arg: string): string {
return Secure: ${arg};`
}
}
Use the @withAIProvider decorator to easily integrate AI capabilities:
`typescript
import { withAIProvider } from 'multicontext/decorators';
class AIObject extends MultiContextObject {
@withAIProvider('anthropic')
async generateText(prompt: string): Promise
const aiProvider = AIProviderManager.getInstance().getCurrentProvider();
return aiProvider.generateResponse(prompt);
}
}
`
Use built-in decorators for caching and rate limiting:
`typescript
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
// ... perform expensive operation
}
}
`
Use the Configuration class to set up global settings:
`typescript
import { Configuration } from 'multicontext';
const config = Configuration.getInstance();
config.setAiProvider('openai');
config.setMaxTokens(100);
config.setTemperature(0.7);
`
The framework provides robust error handling mechanisms:
`typescript
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');
}
``
We welcome contributions to the MultiContext framework! Please see our Contributing Guide for more details.
This project is licensed under the ISC License. See the LICENSE file for details.
For support, please open an issue in the GitHub repository or contact our support team at support@multicontext.com.