Skip to content

Message

🛈 DocStrings

A message in a conversation.

Source code in src/anyenv/text_sharing/opencode.py
@dataclass
class Message:
    """A message in a conversation."""

    role: MessageRole
    """Role of the message sender."""

    parts: list[MessagePart] = field(default_factory=list)
    """Parts that make up the message content."""

    def __post_init__(self) -> None:
        """Ensure at least one part exists."""
        if not self.parts:
            msg = "Message must have at least one part"
            raise ValueError(msg)

    @classmethod
    def text(cls, role: MessageRole, text: str) -> Message:
        """Create a message with a single text part.

        Convenience method for creating simple text messages.

        Args:
            role: Role of the message sender
            text: Text content

        Returns:
            Message with a single text part

        Example:
            ```python
            msg = Message.text("user", "Hello!")
            # Equivalent to:
            msg = Message(role="user", parts=[MessagePart(type="text", text="Hello!")])
            ```
        """
        return cls(role=role, parts=[MessagePart(type="text", text=text)])

parts class-attribute instance-attribute

parts: list[MessagePart] = field(default_factory=list)

Parts that make up the message content.

role instance-attribute

role: MessageRole

Role of the message sender.

__post_init__

__post_init__() -> None

Ensure at least one part exists.

Source code in src/anyenv/text_sharing/opencode.py
def __post_init__(self) -> None:
    """Ensure at least one part exists."""
    if not self.parts:
        msg = "Message must have at least one part"
        raise ValueError(msg)

text classmethod

text(role: MessageRole, text: str) -> Message

Create a message with a single text part.

Convenience method for creating simple text messages.

Parameters:

Name Type Description Default
role MessageRole

Role of the message sender

required
text str

Text content

required

Returns:

Type Description
Message

Message with a single text part

Example
msg = Message.text("user", "Hello!")
# Equivalent to:
msg = Message(role="user", parts=[MessagePart(type="text", text="Hello!")])
Source code in src/anyenv/text_sharing/opencode.py
@classmethod
def text(cls, role: MessageRole, text: str) -> Message:
    """Create a message with a single text part.

    Convenience method for creating simple text messages.

    Args:
        role: Role of the message sender
        text: Text content

    Returns:
        Message with a single text part

    Example:
        ```python
        msg = Message.text("user", "Hello!")
        # Equivalent to:
        msg = Message(role="user", parts=[MessagePart(type="text", text="Hello!")])
        ```
    """
    return cls(role=role, parts=[MessagePart(type="text", text=text)])

:fa-brands-github: Show source on GitHub