Class AiChatDataService

java.lang.Object
io.jmix.aitoolsflowuidata.service.impl.AiChatDataService
All Implemented Interfaces:
AiChatService, org.springframework.beans.factory.InitializingBean

public class AiChatDataService extends Object implements AiChatService, org.springframework.beans.factory.InitializingBean
Default AiChatService. For an already-persisted user message it:
  1. loads the conversation history, windowed by jmix.aitools.ui.data.chat-memory-max-messages;
  2. creates an empty ASSISTANT placeholder message;
  3. invokes the chat client with that history;
  4. writes the reply into the placeholder, or removes it if the call fails.
The optional status callback is delivered to tools via AiToolStatusPublisher for ephemeral progress messages.
  • Field Details

  • Constructor Details

    • AiChatDataService

      public AiChatDataService()
  • Method Details

    • afterPropertiesSet

      public void afterPropertiesSet()
      Specified by:
      afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean
    • isAvailable

      public boolean isAvailable()
      Description copied from interface: AiChatService
      Returns whether AI chat is currently available, i.e. replies can be generated.
      Specified by:
      isAvailable in interface AiChatService
      Returns:
      true if chat is configured and ready to use
    • processMessage

      public String processMessage(AiChatMessage message)
      Description copied from interface: AiChatService
      Generates the assistant's reply to the given user message.
      Specified by:
      processMessage in interface AiChatService
      Parameters:
      message - the user message to answer
      Returns:
      the assistant's reply text, or an empty string if no reply was produced
    • processMessage

      public String processMessage(AiChatMessage message, @Nullable Consumer<AiToolStatusUpdate> statusCallback)
      Description copied from interface: AiChatService
      Generates the assistant's reply to the given user message, reporting progress while the reply is being prepared.
      Specified by:
      processMessage in interface AiChatService
      Parameters:
      message - the user message to answer
      statusCallback - receives progress updates during processing
      Returns:
      the assistant's reply text, or an empty string if no reply was produced
    • processMessageInternal

      protected String processMessageInternal(AiChatMessage message, @Nullable Consumer<AiToolStatusUpdate> statusCallback)
    • process

      protected @Nullable String process(UUID userMessageId, @Nullable Consumer<AiToolStatusUpdate> statusCallback)
    • buildPromptSpec

      protected org.springframework.ai.chat.client.ChatClient.ChatClientRequestSpec buildPromptSpec(List<org.springframework.ai.chat.messages.Message> history, UUID assistantMessageId, @Nullable Consumer<AiToolStatusUpdate> statusCallback)
    • loadUserMessage

      protected AiChatMessageEntity loadUserMessage(UUID userMessageId)
    • currentUsername

      protected String currentUsername()
    • loadHistory

      protected List<org.springframework.ai.chat.messages.Message> loadHistory(UUID conversationId)
    • loadRecentMessages

      protected List<AiChatMessageEntity> loadRecentMessages(UUID conversationId, int limit)
    • mapEntityToMessage

      protected org.springframework.ai.chat.messages.Message mapEntityToMessage(AiChatMessageEntity chatMessage)
    • createAssistantPlaceholder

      protected AiChatMessageEntity createAssistantPlaceholder(AiConversationEntity conversation)
    • saveAssistantResponse

      protected void saveAssistantResponse(AiChatMessageEntity placeholder, @Nullable String response)
    • removeAssistantPlaceholderSafely

      protected void removeAssistantPlaceholderSafely(AiChatMessageEntity placeholder)
    • resolveResponseLanguage

      protected String resolveResponseLanguage()
    • checkChatClient

      protected void checkChatClient()