canvas.py


Overview

This file defines the core classes Graph and Canvas which represent and manage dynamic workflows composed of interconnected components in the InfiniFlow system. These classes parse a JSON-based DSL (Domain Specific Language) describing a graph of components linked by dependencies, execute the components in sequence or parallel, maintain the workflow state (including history, globals, retrievals, and memory), and handle user interaction and data flow.

The classes integrate with other system parts such as the component registry (component_class), file service, Redis caching, and prompt chunk formatting, enabling complex, stateful workflows with external data and asynchronous processing.


Classes and Methods

Class: Graph

Represents a directed graph of components defined by a DSL JSON string.

Initialization

def __init__(self, dsl: str, tenant_id=None, task_id=None)

Loads and validates the DSL, initializes components as instances of registered component classes, and sets initial execution path.

Methods


Class: Canvas (inherits Graph)

Represents a specialized graph for interactive conversational workflows, managing globals, history, retrieval, and memory.

Initialization

def __init__(self, dsl: str, tenant_id=None, task_id=None)

Initializes default global variables related to the system context and forwards to Graph constructor.

Methods


Important Implementation Details and Algorithms


Interaction with Other System Parts


Usage Examples

# Create a Canvas instance from a DSL JSON string
canvas = Canvas(dsl_json_str, tenant_id="user123")

# Run the workflow with user input query
for event in canvas.run(query="What is InfiniFlow?"):
    if event["event"] == "message":
        print("Assistant says:", event["data"]["content"])
    elif event["event"] == "user_inputs":
        print("User inputs requested:", event["data"]["inputs"])

# Reset the canvas to initial state
canvas.reset()

Class Diagram

classDiagram
    class Graph {
        -dsl: dict
        -path: list
        -components: dict
        -error: str
        -_tenant_id: str
        -task_id: str
        +__init__(dsl: str, tenant_id=None, task_id=None)
        +load()
        +reset()
        +run(**kwargs)
        +get_component(cpn_id) -> dict
        +get_component_obj(cpn_id) -> ComponentBase
        +get_component_type(cpn_id) -> str
        +get_component_input_form(cpn_id) -> dict
        +get_tenant_id()
        +get_component_name(cid) -> str
        +__str__()
    }
    class Canvas {
        -globals: dict
        -history: list
        -retrieval: list
        -memory: list
        +__init__(dsl: str, tenant_id=None, task_id=None)
        +load()
        +reset(mem=False)
        +run(**kwargs) -> generator
        +is_reff(exp: str) -> bool
        +get_variable_value(exp: str) -> Any
        +get_history(window_size: int) -> list
        +add_user_input(question: str)
        +get_prologue() -> str
        +get_mode() -> str
        +set_global_param(**kwargs)
        +get_preset_param() -> dict
        +get_component_input_elements(cpnnm: str) -> dict
        +get_files(files: list) -> list[str]
        +tool_use_callback(agent_id: str, func_name: str, params: dict, result: Any, elapsed_time=None)
        +add_reference(chunks: list, doc_infos: list)
        +get_reference() -> dict
        +add_memory(user: str, assist: str, summ: str)
        +get_memory() -> list
        +get_component_thoughts(cpn_id) -> str
    }
    Canvas --|> Graph

Summary

canvas.py is a pivotal file implementing workflow orchestration in InfiniFlow. The Graph class provides the foundational graph model and component management, while Canvas builds on it to support interactive, stateful, asynchronous workflows with rich user input, branching logic, and external data integration. Its design supports extensibility via component classes and provides fine-grained event-driven execution suitable for conversational AI or agent-based systems.