Modus Serverless Framework

repository·main·Indexed 19 days ago

https://github.com/hypermodeinc/modus

An open-source serverless framework for building agentic systems and AI applications. Modus allows developers to write functions in Go or AssemblyScript that are transformed into scalable, sandboxed GraphQL endpoints using WebAssembly (Wasm). It includes a CLI (@hypermode/modus-cli) for local development and a runtime for executing modules with integrated access to AI models, vector support, and various databases including Dgraph, Neo4j, PostgreSQL, and MySQL.

Tokens
20.2K
Snippets
93
Records
118
Agent score
64%

What's inside Modus

  1. Overview of Modus CLI

    main
    The Modus CLI is the primary tool for the local developer experience when working with Modus. Modus itself is an open-source, serverless framework designed for building intelligent functions and APIs using WebAssembly (Wasm).
  2. Use the Modus SDK for AssemblyScript

    main
    The Modus SDK for AssemblyScript is designed for developers building Modus applications using AssemblyScript. It provides the necessary interfaces to access Modus platform features, utilize Modus-specific build scripts and tooling, and leverage specialized AssemblyScript APIs tailored for the Modus environment.
  3. How Modus works: From function to API

    main

    Modus is a serverless framework that turns functions into scalable, AI-ready GraphQL endpoints. It uses WebAssembly (Wasm) to provide a sandboxed execution environment.

    The Workflow

    1. Development: You write a function in a supported language (Go or AssemblyScript).
    2. Compilation: Modus extracts metadata, optimizes the code for the host environment, and caches the compiled module.
    3. Deployment/Activation: Modus reads the app's manifest to extract connections, models, and configuration, then generates a GraphQL API schema.
    4. Execution: When a GraphQL query is received, Modus loads the Wasm module into a sandboxed environment with dedicated memory, runs the code using host functions for AI/data access, and returns the result.
    // Example AssemblyScript function
    export function sayHello(name: string): string {
      return `Hello, ${name}!`;
    }
    
    // Example GraphQL query to call the function
    query SayHello {
      sayHello(name: "World")
    }
  4. Understand the role of the Modus Runtime

    main

    The Modus Runtime is the core component responsible for loading and executing Modus applications.

    Note for Users: You generally do not need to interact with the runtime source code directly. The Modus CLI handles the installation of the appropriate platform-specific binary in your development environment, and the runtime is used automatically when hosting Modus apps in production.

  5. Quickstart: Install and initialize Modus

    main

    To start building with Modus, install the CLI globally, initialize a new project, and run it locally in development mode.

    1. Install the Modus CLI: Use npm to install the @hypermode/modus-cli package.
    2. Initialize a project: Run modus new to scaffold a new Modus application.
    3. Run locally: Use modus dev to start your app with fast refresh enabled.
    # Install the CLI
    npm install -g @hypermode/modus-cli
    
    # Initialize a new app
    modus new
    
    # Run locally with fast refresh
    modus dev
  6. Use Modus shared libraries in external projects

    main
    The libraries located in the lib/ directory are designed to be used both within the Modus ecosystem and in external projects. They are primarily utilized by the Modus runtime and Hypermode hosting infrastructure, but they can be integrated into any application subject to the Modus LICENSE.
  7. Run the Modus app with Kubernetes secrets

    main

    To run a Modus application using a Kubernetes secret for secret management, use the modus_runtime command with the -useKubernetesSecret flag and specify the secret name using -kubernetesSecretName. The secret name should follow the namespace/secret-name format.

    modus_runtime -appPath ./build -useKubernetesSecret -kubernetesSecretName default/example
  8. Set up and run the Secrets example

    main

    This guide demonstrates how to set up a local environment using kind to test the Modus secrets implementation.

    Prerequisites

    A running Kubernetes cluster is required. This example uses kind for local testing.

    Steps

    1. Initialize the cluster: Run the provided setup script to create a local kind cluster.
    2. Build the application: Compile the Modus app using the build script.
    3. Execute the runtime: Run the application using modus_runtime with Kubernetes secret flags.

    Verification

    To verify the implementation is working correctly:

    1. Call the GetSecretValue() function with the input argument "foo".
    2. Confirm that the function returns "bar".
    # Setup local kind cluster for testing
    bash setup.sh
    
    # Build the Modus app
    bash build.sh
    
    # Run the Modus app
    modus_runtime -appPath ./build -useKubernetesSecret -kubernetesSecretName default/example
  9. Install Modus shared libraries via Go modules

    main

    Modus shared libraries are organized into independent Go modules. Each module is versioned separately using a tag scheme that corresponds to its directory path. To install a specific library, use its unique module path. For example, to install the manifest library, use the path github.com/hypermodeinc/modus/lib/manifest.

    go get -u github.com/hypermodeinc/modus/lib/manifest
  10. Understand the output of the Modus Keygen Tool

    main

    When executed, the modus-keygen tool produces both standard output (stdout) and local files.

    Standard Output (stdout)

    The tool prints a JSON-encoded object and a JWT token:

    1. RSA Public Key (JSON): A JSON object containing an RSA public key. This is intended to be passed to the MODUS_PEMS environment variable.
    2. JWT Token: A bearer token signed with the generated RSA key. It has a 1-year expiration date and no additional claims. This is used for authorization.

    Generated Files

    The tool creates two files in your current working directory:

    • private-key.pem: A 2048-bit RSA private key. IMPORTANT: Keep this secret and do not lose it. You need this file to issue new JWT tokens that can be validated by the public key.
    • public-key.pem: The corresponding public key (this matches the JSON output provided to stdout).

    Note on persistence: If private-key.pem already exists in the working directory, the tool will re-use it instead of generating a new key pair. This allows you to run the tool multiple times to generate additional JWTs from the same existing key pair.