Class DefaultAiChatMessageFactory

java.lang.Object
io.jmix.aichat.data.DefaultAiChatMessageFactory
All Implemented Interfaces:
AiChatMessageFactory<SimpleAiChatMessage>, Serializable, Function<AiChatMessageContext,SimpleAiChatMessage>

@Component("aichat_DefaultAiChatMessageFactory") public class DefaultAiChatMessageFactory extends Object implements AiChatMessageFactory<SimpleAiChatMessage>
The message factory AiChat falls back to when the application supplies none: it mints SimpleAiChatMessage instances and saves their attachments to the default file storage. This is the only place the add-on writes to FileStorage.

It fits the in-memory path, where the message type is SimpleAiChatMessage; an entity-backed source supplies its own factory instead, because the message contract is read-only and the component never creates entities itself. Replace it by declaring a @Primary bean of a subclass: getFileStorage() chooses where attachments go, saveAttachments(List) how they are stored, and apply(io.jmix.aichat.data.AiChatMessageContext) what message is built from the result.

See Also:
  • Field Details

  • Constructor Details

    • DefaultAiChatMessageFactory

      public DefaultAiChatMessageFactory()
  • Method Details

    • apply

      public SimpleAiChatMessage apply(AiChatMessageContext context)
      Description copied from interface: AiChatMessageFactory
      Creates a message for the given context. The factory owns persistence: for an entity source it saves each attachment's bytes to file storage and sets the resulting FileRefs on the created message.
      Specified by:
      apply in interface AiChatMessageFactory<SimpleAiChatMessage>
      Specified by:
      apply in interface Function<AiChatMessageContext,SimpleAiChatMessage>
      Parameters:
      context - what is known about the message to create
      Returns:
      the created message
    • saveAttachments

      protected List<FileRef> saveAttachments(List<com.vaadin.flow.component.ai.common.AIAttachment> attachments)
      Saves the given attachments and returns the references to them. An empty list is stored nowhere, so no file storage is resolved for a message without attachments.
      Parameters:
      attachments - the files attached to the message
      Returns:
      the references to the saved files, in the order the attachments were given
    • getFileStorage

      protected FileStorage getFileStorage()
      Returns the storage the attachments are saved to. Override to write to a named storage instead of the application's default one.
      Returns:
      the file storage