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:
- loads the conversation history, windowed by
jmix.aitools.ui.data.chat-memory-max-messages; - creates an empty
ASSISTANTplaceholder message; - invokes the chat client with that history;
- writes the reply into the placeholder, or removes it if the call fails.
AiToolStatusPublisher for
ephemeral progress messages.-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected AiToolRegistryprotected static final Stringprotected @Nullable org.springframework.ai.chat.client.ChatClientprotected ChatClientFactoryprotected CurrentUserSubstitutionprotected UnconstrainedDataManagerprotected AiToolsFlowuiDataPropertiesprotected ResponseLanguageProviderprotected AiChatSystemPromptProvider -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidprotected org.springframework.ai.chat.client.ChatClient.ChatClientRequestSpecbuildPromptSpec(List<org.springframework.ai.chat.messages.Message> history, UUID assistantMessageId, @Nullable Consumer<AiToolStatusUpdate> statusCallback) protected voidprotected AiChatMessageEntitycreateAssistantPlaceholder(AiConversationEntity conversation) protected StringbooleanReturns whether AI chat is currently available, i.e.protected List<org.springframework.ai.chat.messages.Message> loadHistory(UUID conversationId) protected List<AiChatMessageEntity> loadRecentMessages(UUID conversationId, int limit) protected AiChatMessageEntityloadUserMessage(UUID userMessageId) protected org.springframework.ai.chat.messages.MessagemapEntityToMessage(AiChatMessageEntity chatMessage) protected @Nullable Stringprocess(UUID userMessageId, @Nullable Consumer<AiToolStatusUpdate> statusCallback) processMessage(AiChatMessage message) Generates the assistant's reply to the given user message.processMessage(AiChatMessage message, @Nullable Consumer<AiToolStatusUpdate> statusCallback) Generates the assistant's reply to the given user message, reporting progress while the reply is being prepared.protected StringprocessMessageInternal(AiChatMessage message, @Nullable Consumer<AiToolStatusUpdate> statusCallback) protected voidremoveAssistantPlaceholderSafely(AiChatMessageEntity placeholder) protected Stringprotected voidsaveAssistantResponse(AiChatMessageEntity placeholder, @Nullable String response)
-
Field Details
-
ASSISTANT_MESSAGE_ID_KEY
- See Also:
-
chatClientFactory
-
aiToolRegistry
-
systemPromptProvider
-
responseLanguageProvider
-
dataManager
-
dataProperties
-
currentUserSubstitution
-
chatClient
protected @Nullable org.springframework.ai.chat.client.ChatClient chatClient
-
-
Constructor Details
-
AiChatDataService
public AiChatDataService()
-
-
Method Details
-
afterPropertiesSet
public void afterPropertiesSet()- Specified by:
afterPropertiesSetin interfaceorg.springframework.beans.factory.InitializingBean
-
isAvailable
public boolean isAvailable()Description copied from interface:AiChatServiceReturns whether AI chat is currently available, i.e. replies can be generated.- Specified by:
isAvailablein interfaceAiChatService- Returns:
trueif chat is configured and ready to use
-
processMessage
Description copied from interface:AiChatServiceGenerates the assistant's reply to the given user message.- Specified by:
processMessagein interfaceAiChatService- 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:AiChatServiceGenerates the assistant's reply to the given user message, reporting progress while the reply is being prepared.- Specified by:
processMessagein interfaceAiChatService- Parameters:
message- the user message to answerstatusCallback- 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
-
currentUsername
-
loadHistory
-
loadRecentMessages
-
mapEntityToMessage
protected org.springframework.ai.chat.messages.Message mapEntityToMessage(AiChatMessageEntity chatMessage) -
createAssistantPlaceholder
-
saveAssistantResponse
-
removeAssistantPlaceholderSafely
-
resolveResponseLanguage
-
checkChatClient
protected void checkChatClient()
-