message.py


Overview

The message.py file defines a message generation component for the InfiniFlow system, intended to produce dynamic textual content based on predefined templates and input parameters. It supports both streaming and batch generation of messages, including the use of Jinja2 templating for advanced formatting. The component can interpolate variables, handle partial or asynchronous content streaming, and ensures execution timeouts for stability.

This component is likely used within a larger agent or workflow system where dynamic message generation is required, such as chatbots, notifications, or interactive flows.


Classes and Functions

Class: MessageParam

Defines the parameters for the Message component.

Description

Attributes

Attribute

Type

Description

content

list

List of message templates (strings) to choose from randomly.

stream

bool

Whether message content should be streamed incrementally (True) or generated at once (False).

outputs

dict

Defines output schema with "content" as a string.

Methods

Usage Example

params = MessageParam()
params.content = ["Hello, {user}!"]
params.stream = False
params.check()  # Returns True if valid

Class: Message

The main component class responsible for generating messages.

Description

Class Attributes

Attribute

Type

Description

component_name

str

Static name identifier "Message" for the component.


Methods


get_kwargs(script: str, kwargs: dict = {}, delimeter: str = None) -> tuple[str, dict[str, str | list | Any]]

Parses input template text (script) to extract variable placeholders and prepares keyword arguments for rendering.

script = "Hello, {%user.name%}! Your score is {%score%}."
kwargs = {"score": 42}
processed_script, processed_kwargs = message_instance.get_kwargs(script, kwargs)
# processed_script: "Hello, {user_name}! Your score is {score}."
# processed_kwargs: {"user_name": ..., "score": 42}

_stream(rand_cnt: str) -> Generator[str, None, None]

Generates content incrementally by streaming parts of the message template.


_is_jinjia2(content: str) -> bool

Checks if the given string contains Jinja2 template syntax.


_invoke(**kwargs)

Main execution method called to generate the message content.

message = Message()
message._param.content = ["Hello, {{ user }}!"]
message._param.stream = False
message._invoke(user="Alice")
print(message.get_output("content"))  # "Hello, Alice!"

thoughts() -> str

Returns an empty string; placeholder for potential future use or interface compliance.


Important Implementation Details


Interaction With Other System Components


Visual Diagram: Class Structure

classDiagram
    class MessageParam {
        +content: list
        +stream: bool
        +outputs: dict
        +check() bool
    }
    class Message {
        +component_name: str
        +get_kwargs(script: str, kwargs: dict, delimeter: str) tuple
        -_stream(rand_cnt: str) generator
        -_is_jinjia2(content: str) bool
        -_invoke(**kwargs) void
        +thoughts() str
    }
    MessageParam <|-- Message

Summary

The message.py file implements a flexible and powerful message generation component within the InfiniFlow agent system. It supports templated messages with variable interpolation, streaming output, and Jinja2 templating integration. It is designed to be robust with validation, timeout controls, and partial content streaming, making it suitable for interactive or real-time applications requiring dynamic textual output.

This component is tightly coupled with the agent's variable context (_canvas), the component base framework, and utility functions, enabling it to serve as a dynamic message producer in workflows or conversational agents.