code.mdx


Overview

The Code component is a versatile integration module within the Agent framework that allows users to embed and execute custom Python or JavaScript code snippets dynamically. This enables sophisticated data processing workflows by extending the Agent's capabilities through user-defined logic. The component leverages sandboxed execution environments to securely run arbitrary code, ensuring system safety and stability.

Key highlights:


Detailed Explanations

Component Purpose

The Code component is primarily intended for scenarios requiring complex or custom logic that cannot be achieved through standard Agent configurations alone. By embedding Python or JavaScript code, users can implement advanced processing steps, API calls, data transformations, or other specialized computations within their workflows.


Configuration Sections

1. Input Variables

2. Code Field

Python Example
def main(arg1: str, arg2: str) -> dict:
    return {
        "result": arg1 + arg2,
    }
JavaScript Example
const axios = require('axios');
async function main(args) {
  try {
    const response = await axios.get('https://github.com/infiniflow/ragflow');
    console.log('Body:', response.data);
  } catch (error) {
    console.error('Error:', error.message);
  }
}

3. Return Values

4. Output


Prerequisites and Environment Setup

The Code component depends on a secure and isolated execution environment, which requires proper installation and configuration of:

  1. gVisor: An isolation layer to sandbox container runtimes, protecting the host system.

  2. RAGFlow Sandbox: A pluggable backend that executes the code safely within containers.

  3. Additional Dependencies (Optional): If your code requires extra Python or JavaScript packages, they must be installed in the Sandbox images.

  4. RAGFlow Configuration: Ensure sandbox-specific settings are enabled in the .env file.

  5. Service Restart: Any configuration changes require restarting the RAGFlow services.


Troubleshooting Common Issues

Error Message

Root Cause

Solution Summary

HTTPConnectionPool(host='sandbox-executor-manager', port=9385): Read timed out.

gVisor not installed or base images not pulled.

Install gVisor, restart Docker, verify runtime with docker run --rm --runtime=runsc hello-world. Pull base images for NodeJS and Python Sandbox runners.

HTTPConnectionPool(host='none', port=9385): Max retries exceeded.

Missing /etc/hosts entry for sandbox-executor-manager.

Add entry mapping sandbox-executor-manager to 127.0.0.1 in /etc/hosts.

Container pool is busy

All runners are busy executing tasks.

Retry later or increase runner pool size in configuration.


Frequently Asked Questions

How to Import My Own Python or JavaScript Packages Into Sandbox?

For Python:

  1. Navigate to the Sandbox directory.

  2. Append your package (e.g., openpyxl) to sandbox_base_image/python/requirements.txt.

  3. Rebuild the image using make (or make build for build-only).

  4. Enter the container and verify installation via Python import.

For JavaScript:

  1. Navigate to sandbox_base_image/nodejs.

  2. Use npm install <package> to add packages (e.g., lodash).

  3. Rebuild the image using make.

  4. Enter the container and verify via npm list <package> or by checking node_modules.


Implementation Details and Algorithms


Interaction with Other System Components


Visual Diagram

flowchart TD
    A[Agent Workflow] --> B[Code Component]
    B --> C[RAGFlow Sandbox Executor]
    C --> D[gVisor Isolation Layer]
    D --> E[Container Runtime (Docker)]
    E --> F[Python / JavaScript Runtime]
    F --> G[User Code Execution]
    G --> F
    F --> C
    C --> B
    B --> H[Output Variables]

Diagram Explanation:


Usage Example

Suppose you want to concatenate two strings in Python:

  1. Add input variables: arg1, arg2.

  2. Enter the following code in the code editor:

def main(arg1: str, arg2: str) -> dict:
    return {"result": arg1 + arg2}
  1. Define the output variable result.

  2. Run the Agent workflow. The output variable result will contain the concatenated string.

This example demonstrates how simple it is to embed custom logic securely and flexibly into your Agent workflows.


Summary

The code.mdx file documents the Code component, a powerful extension point within the Agent framework for running custom Python or JavaScript code securely inside sandboxed containers. It guides users through setup, configuration, troubleshooting, and extending runtime environments with additional packages, enabling complex and dynamic data processing capabilities.