canvas_service.py


Overview

This file, canvas_service.py, is a core service layer module of the InfiniFlow platform responsible for managing canvas templates, user canvases (agents), and orchestrating conversational AI completions based on those canvases. It acts as an intermediary between the database models representing canvases and the runtime logic that executes conversations using those canvases.

Key responsibilities include:

This module tightly integrates database services, the Canvas execution engine, and OpenAI-compatible response formatting to facilitate multi-tenant, session-based AI agents.


Classes and Functions

Classes

1. CanvasTemplateService(CommonService)

2. DataFlowTemplateService(CommonService)

3. UserCanvasService(CommonService)

get_list(cls, tenant_id, page_number, items_per_page, orderby, desc, id, title, canvas_category=CanvasCategory.Agent)
get_by_tenant_id(cls, pid)
get_by_tenant_ids(cls, joined_tenant_ids, user_id, page_number, items_per_page, orderby, desc, keywords, canvas_category=CanvasCategory.Agent)
accessible(cls, canvas_id, tenant_id)

Functions

completion(tenant_id, agent_id, session_id=None, **kwargs)

Usage Example:
for event in completion(tenant_id=1, agent_id=42, query="Hello AI!", user_id="user123"):
    print(event)  # Streamed response chunks

completionOpenAI(tenant_id, agent_id, question, session_id=None, stream=True, **kwargs)

Usage Example:
# Streaming usage
for event in completionOpenAI(tenant_id=1, agent_id=42, question="Explain AI?", stream=True):
    print(event)

# Non-streaming usage
response = next(completionOpenAI(tenant_id=1, agent_id=42, question="Explain AI?", stream=False))
print(response)

Important Implementation Details


Interaction with Other Modules


Visual Diagram

classDiagram
    class CanvasTemplateService {
        +model: CanvasTemplate
    }
    class DataFlowTemplateService {
        +model: CanvasTemplate
    }
    class UserCanvasService {
        +model: UserCanvas
        +get_list(tenant_id, page_number, items_per_page, orderby, desc, id, title, canvas_category)
        +get_by_tenant_id(pid)
        +get_by_tenant_ids(joined_tenant_ids, user_id, page_number, items_per_page, orderby, desc, keywords, canvas_category)
        +accessible(canvas_id, tenant_id)
    }
    class Canvas {
        +__init__(dsl, tenant_id, agent_id)
        +run(query, files, user_id, inputs)
        +reset()
        +get_reference()
    }
    class API4ConversationService {
        +get_by_id(session_id)
        +save(**conv)
        +append_message(conv_id, conv)
    }
    class completion {
        +tenant_id
        +agent_id
        +session_id
        +kwargs
        +generator
    }
    class completionOpenAI {
        +tenant_id
        +agent_id
        +question
        +session_id
        +stream
        +kwargs
        +generator
    }

    CanvasTemplateService --|> CommonService
    DataFlowTemplateService --|> CommonService
    UserCanvasService --|> CommonService

    completion --> Canvas
    completion --> API4ConversationService
    completionOpenAI --> completion
    completionOpenAI --> tiktoken
    completionOpenAI --> get_data_openai

Summary

canvas_service.py plays a critical role in the InfiniFlow architecture by bridging the gap between user canvas data stored in the database and the AI runtime engine (Canvas). It provides robust services for querying canvases, enforcing permissions, managing conversational sessions, and generating AI responses with OpenAI API compatibility. This module enables multi-tenant agent management and scalable conversational AI workflows with streaming and batch response support.