schemas.py

Overview

The schemas.py file defines data models used for handling code execution requests and results within the InfiniFlow system. It leverages the pydantic library to create strongly-typed, validated schemas that ensure the integrity and correctness of data exchanged between components responsible for executing code snippets in various programming languages.

This file primarily contains two classes:

These schemas facilitate structured communication between front-end clients, execution engines, and other backend services by defining clear contracts for the input and output data formats.


Classes

1. CodeExecutionResult

Description

CodeExecutionResult models the outcome of running a code snippet. It captures the execution status, outputs (standard output and error), exit code, resource usage metrics, and detailed error classifications if any occurred.

Attributes

Attribute

Type

Description

status

ResultStatus

Enum indicating the overall result status (e.g., success, failure).

stdout

str

Captured standard output from the execution.

stderr

str

Captured standard error output from the execution.

exit_code

int

Process exit code returned by the execution environment.

detail

Optional[str]

Optional detailed message or additional info about the result.

time_used_ms

Optional[float]

Time taken for execution in milliseconds.

memory_used_kb

Optional[float]

Peak memory usage during execution in kilobytes.

resource_limit_type

Optional[ResourceLimitType]

Enum indicating if a resource limit was hit (CPU, memory, etc).

unauthorized_access_type

Optional[UnauthorizedAccessType]

Enum specifying unauthorized access errors detected.

runtime_error_type

Optional[RuntimeErrorType]

Enum classifying runtime errors encountered (exceptions, etc).

Usage Example

from models.enums import ResultStatus

result = CodeExecutionResult(
    status=ResultStatus.SUCCESS,
    stdout="Hello, World!\n",
    stderr="",
    exit_code=0,
    time_used_ms=150.5,
    memory_used_kb=2048,
)
print(result.json())

2. CodeExecutionRequest

Description

CodeExecutionRequest defines the data structure for submitting code execution requests. It includes the code itself (base64 encoded), the programming language, and optional execution arguments.

Attributes

Attribute

Type

Description

code_b64

str

Base64 encoded string containing the source code to execute.

language

SupportLanguage

Enum specifying the programming language of the code (default: Python).

arguments

Optional[dict]

Optional dictionary of arguments to be passed during execution.

Validators

Usage Example

from models.enums import SupportLanguage

request = CodeExecutionRequest(
    code_b64="cHJpbnQoIkhlbGxvLCBJbmZpbklGbG93ISIp",  # base64 for: print("Hello, InfiniFlow!")
    language=SupportLanguage.PYTHON,
    arguments={"timeout": 5}
)
print(request.code_b64)

If an invalid base64 string is provided, the validator will raise an error:

try:
    invalid_request = CodeExecutionRequest(code_b64="!!!not_base64!!!")
except ValueError as e:
    print(e)  # Output: Invalid base64 encoding: ...

Implementation Details and Algorithms


Interaction with Other Components


Mermaid Class Diagram

classDiagram
    class CodeExecutionResult {
        +status: ResultStatus
        +stdout: str
        +stderr: str
        +exit_code: int
        +detail: Optional[str]
        +time_used_ms: Optional[float]
        +memory_used_kb: Optional[float]
        +resource_limit_type: Optional[ResourceLimitType]
        +unauthorized_access_type: Optional[UnauthorizedAccessType]
        +runtime_error_type: Optional[RuntimeErrorType]
    }

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

    CodeExecutionRequest ..> "models.enums.SupportLanguage"
    CodeExecutionResult ..> "models.enums.ResultStatus"
    CodeExecutionResult ..> "models.enums.ResourceLimitType"
    CodeExecutionResult ..> "models.enums.UnauthorizedAccessType"
    CodeExecutionResult ..> "models.enums.RuntimeErrorType"

Summary

The schemas.py file is a critical part of the InfiniFlow project’s code execution workflow. It provides validated, well-structured data models for submitting code to be executed and for reporting the results of that execution. By leveraging pydantic and enums, it ensures robustness, clarity, and consistency in handling code execution data, serving as a bridge between clients, execution services, and monitoring/security systems.