npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@nomyx/multi-context-objects

v1.0.8

Published

A library for managing multi-context objects in distributed applications

Downloads

18

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

  1. 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}!`;
  }
}
  1. 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].