Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:869413421 Ai moive studio CanvasAssistantService

From Leeroopedia
Knowledge Sources
Domains Agent_Systems, NLP
Last Updated 2026-10-23 12:00 GMT

Overview

Service that orchestrates the canvas AI assistant conversation lifecycle: session management, agent invocation, event stream consumption, and resume from interrupts.

Description

CanvasAssistantService.chat manages a complete assistant turn:

1. Session management: Gets or creates a session via session_store, appends the user message to conversation history 2. Context building: Loads canvas graph snapshot, workflow draft, and builds agent context 3. Agent invocation: Calls the agent factory to build the graph, then invokes it with the user message and context 4. Stream consumption: Consumes the agent's event stream (_consume_agent_stream), tracking tool calls, mutations, and responses 5. Finalization: Persists session state and returns a CanvasAssistantTurnResult

The resume method handles interrupt-based human-in-the-loop: it resumes the agent with a user decision (approve/reject) via LangGraph's Command(resume=...).

Usage

Import when handling canvas assistant chat requests. Called by the canvas assistant SSE API endpoint.

Code Reference

Source Location

  • Repository: ai-moive-studio
  • File: backend/src/assistant/service.py:L321-354

Signature

class CanvasAssistantService:
    async def chat(self, request: CanvasAssistantChatRequest, user_id: str) -> CanvasAssistantTurnResult:
        """
        Args:
            request: Chat request with message, document_id, api_key_id, chat_model_id, session_id
            user_id: User UUID

        Returns:
            CanvasAssistantTurnResult with agent response, tool calls, session state
        """

    async def resume(self, request: CanvasAssistantResumeRequest, user_id: str) -> CanvasAssistantTurnResult:
        """Resume from an interrupt with user decision."""

Import

from src.assistant.service import CanvasAssistantService

I/O Contract

Inputs

Name Type Required Description
request CanvasAssistantChatRequest Yes Message, document_id, api_key_id, chat_model_id, session_id
user_id str Yes User UUID

Outputs

Name Type Description
return value CanvasAssistantTurnResult Agent response, tool calls, session state

Usage Examples

Chatting with the Assistant

from src.assistant.service import CanvasAssistantService

service = CanvasAssistantService(
    session_store=session_store,
    agent_factory=factory,
)
result = await service.chat(
    request=CanvasAssistantChatRequest(
        message="Add a text node with a sunset prompt",
        document_id="doc-uuid",
        api_key_id="key-uuid",
        chat_model_id="model-uuid",
        session_id="session-uuid",
    ),
    user_id="user-uuid",
)
print(result.response)

Related Pages

Implements Principle

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment