code_exec.py


Overview

The code_exec.py file implements a sandboxed code execution tool component for the InfiniFlow system. It enables safe execution of user-supplied code snippets written in Python or JavaScript, returning structured results in JSON format. This functionality is encapsulated in a reusable component that validates, encodes, sends, and processes code execution requests through a remote sandbox service.

Key features include:

This file primarily contains data models to represent execution requests, parameter definitions for the tool, and the main tool class implementing the execution logic.


Classes and Functions

Enum: Language

class Language(StrEnum):
    PYTHON = "python"
    NODEJS = "nodejs"

Class: CodeExecutionRequest

class CodeExecutionRequest(BaseModel):
    code_b64: str
    language: str = Language.PYTHON.value
    arguments: Optional[dict] = {}

Validators:

Usage Example

req = CodeExecutionRequest(
    code_b64=base64.b64encode(b"def main(): return {'result': 42}").decode(),
    language="python",
    arguments={"arg1": "value1"}
)

Class: CodeExecParam

class CodeExecParam(ToolParamBase):

Important Details

Usage Example

param = CodeExecParam()
param.lang = "python"
param.script = """
def main() -> dict:
    return {"result": "Hello, World!"}
"""
param.arguments = {}
param.outputs = {"result": {"value": "", "type": "string"}}
param.check()

Class: CodeExec

class CodeExec(ToolBase, ABC):

Properties

Methods

Usage Example

code_exec = CodeExec()
result = code_exec._execute_code(
    language="python",
    code="def main(): return {'result': 123}",
    arguments={}
)
print(result)

Important Implementation Details


Interaction with Other System Components


Visual Diagram

classDiagram
    class Language {
        <<enum>>
        +PYTHON = "python"
        +NODEJS = "nodejs"
    }

    class CodeExecutionRequest {
        +code_b64: str
        +language: str
        +arguments: Optional[dict]
        +validate_base64(v: str) str
        +normalize_language(v) str
    }

    class CodeExecParam {
        +meta: ToolMeta
        +lang: str
        +script: str
        +arguments: dict
        +outputs: dict
        +check()
        +get_input_form() dict
    }

    class CodeExec {
        +component_name: str = "CodeExec"
        +_invoke(**kwargs)
        +_execute_code(language: str, code: str, arguments: dict)
        +_encode_code(code: str) str
        +thoughts() str
    }

    CodeExecParam <|-- CodeExec
    CodeExecutionRequest ..> CodeExec : uses for request payload
    Language <.. CodeExecutionRequest : language field

Summary

code_exec.py is a critical utility within the InfiniFlow system that allows for dynamic, sandboxed execution of Python and JavaScript code snippets. It ensures safety, validation, and structured integration with the rest of the agent/tool framework by:

This design enables users and automated agents to run arbitrary code fragments safely in a controlled environment, supporting complex data processing workflows within the InfiniFlow platform.