Interface MessageMixin


public interface MessageMixin
Jackson mixin interface for configuring JSON serialization of BaseMessage and its subclasses.

MessageMixin provides Jackson annotations to enable proper polymorphic serialization and deserialization of the entire agui message hierarchy. This mixin uses the message's "role" property as the type discriminator to maintain type safety during JSON processing.

The mixin configures Jackson to:

  • Use the "role" property as the type discriminator
  • Map each message role to its corresponding Java class
  • Preserve complete type information during serialization
  • Reconstruct the correct message subclass during deserialization

Supported message types and their role mappings:

  • "assistant" → AssistantMessage - AI assistant responses with optional tool calls
  • "developer" → DeveloperMessage - Developer instructions and system context
  • "user" → UserMessage - User input and queries
  • "system" → SystemMessage - System prompts and behavioral instructions
  • "tool" → ToolMessage - Tool execution results and function call responses

This role-based type discrimination aligns with common conversational AI patterns where message types are naturally distinguished by their sender role in the conversation. The approach ensures that message collections can be serialized and deserialized while maintaining the correct type information for each message.

The mixin enables seamless JSON serialization of message objects regardless of their specific subclass, making it possible to serialize conversation histories containing mixed message types and deserialize them back to their original types correctly.

This mixin is applied to BaseMessage and affects all its subclasses through Jackson's inheritance mechanism, providing consistent JSON handling across the entire message hierarchy without requiring modifications to the core message classes.

Example JSON output:


 {
   "role": "assistant",
   "content": "Hello! How can I help you today?",
   "id": "msg-123",
   "name": "AI Assistant",
   "toolCalls": []
 }
 

The role-based discrimination is particularly useful for conversation persistence, API communication, and maintaining conversation context across different components of the agui framework.

Author:
Pascal Wilbrink