Message Interface
All message types implement theMessage interface, which provides core functionality for conversation management.
Location: src/Magic/Chat/Messages/Message.php:9
Methods
toArray(): array- Convert the message to an array representationfromArray(array $data): static- Create a message instance from an arraytext(): ?string- Get the text representation of the messagerole(): Role- Get the role associated with this message (User, Assistant, or System)
TextMessage
Simple text-based messages for standard conversation exchanges. Location:src/Magic/Chat/Messages/TextMessage.php:7
Constructor
The role of the message sender (User, Assistant, or System)
The text content of the message
Static Methods
user()
Create a user message.The message content
assistant()
Create an assistant message.The message content
fromChunk()
Create a message from a streaming chunk. Used for streaming responses.The text chunk to create the message from
Instance Methods
append()
Append additional text to the message content. Returns the message instance for chaining.The text to append
JsonMessage
Messages containing structured JSON data. Useful for extracting structured information from AI responses. Location:src/Magic/Chat/Messages/JsonMessage.php:9
Constructor
The role of the message sender
The structured data as an associative array
The partial JSON string (used during streaming)
Methods
data()
Get the structured data from the message.append()
Append a JSON chunk during streaming. Automatically parses partial JSON.The JSON chunk to append
fromChunk()
Create a JsonMessage from a streaming chunk.The JSON chunk
ToolCallMessage
Represents a request from the AI to call a specific tool or function. Location:src/Magic/Chat/Messages/ToolCallMessage.php:9
Constructor
The role of the message sender (typically Assistant)
The tool call details (name, arguments, id)
Partial JSON string for streaming tool calls
Optional JSON schema for validating tool arguments
Static Methods
call()
Create a tool call message from a ToolCall instance.The tool call to create a message from
fromChunk()
Create from a streaming chunk of tool call data.The tool call JSON chunk
Methods
data()
Get the tool call arguments.append()
Append tool call data during streaming. Parses partial JSON.The chunk to append
appendFull()
Append with full tool call structure parsing.The chunk to append
ToolResultMessage
Contains the result of a tool execution, sent back to the AI. Location:src/Magic/Chat/Messages/ToolResultMessage.php:8
Constructor
The role of the message sender (typically User)
The original tool call this result corresponds to
The result of the tool execution (string, array, or other data)
Whether this result should end the conversation
Static Methods
output()
Create a tool result message with successful output.The original tool call
The tool execution result
end()
Create a result that ends the conversation.The original tool call
The final output
error()
Create an error result message.The original tool call
The error message
canceled()
Create a canceled tool result.The tool call that was canceled
Methods
data()
Get the output as an array (if possible).json()
Get the output as a JSON array.withOutput()
Create a new instance with different output.The new output value
ToolCall
Represents a single tool invocation request. Location:src/Magic/Chat/Messages/ToolCall.php:8
Constructor
The name of the tool to call
The arguments to pass to the tool
Unique identifier for this tool call
Static Methods
tryFrom()
Safely create a ToolCall from an array, returning null if invalid.The array data to create from
fromArray()
Create a ToolCall from an array.The array containing tool call data
DataMessage Interface
Interface for messages that contain structured data. Location:src/Magic/Chat/Messages/DataMessage.php:5
Implemented by JsonMessage, ToolCallMessage, and ToolResultMessage.
Methods
data(): ?array- Get the structured data from the message
PartialMessage Interface
Interface for messages that support streaming updates. Location:src/Magic/Chat/Messages/PartialMessage.php:5
Implemented by TextMessage, JsonMessage, and ToolCallMessage.
Methods
append(string $chunk): static- Append a chunk to the messagefromChunk(string $chunk): static- Create a message from an initial chunk
Role Enum
Defines the participant roles in a conversation. Location:src/Magic/Chat/Prompt/Role.php:5
Values
Role::User- Messages from the user/humanRole::Assistant- Messages from the AI assistantRole::System- System-level instructions and context