-
Notifications
You must be signed in to change notification settings - Fork 136
2415 chat-memory-redis - Introduce Redis-based chat memory handling components #4471
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
server/libs/modules/components/ai/agent/chat-memory/chat-memory-redis/build.gradle.kts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| dependencies { | ||
| implementation("redis.clients:jedis") | ||
| implementation("org.springframework.ai:spring-ai-model-chat-memory-repository-redis") | ||
| implementation(project(":server:libs:platform:platform-component:platform-component-service")) | ||
| } |
103 changes: 103 additions & 0 deletions
103
...va/com/bytechef/component/ai/agent/chat/memory/redis/RedisChatMemoryComponentHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| /* | ||
| * Copyright 2025 ByteChef | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.bytechef.component.ai.agent.chat.memory.redis; | ||
|
|
||
| import static com.bytechef.component.ai.agent.chat.memory.redis.constant.RedisChatMemoryConstants.DEFAULT_KEY_PREFIX; | ||
| import static com.bytechef.component.ai.agent.chat.memory.redis.constant.RedisChatMemoryConstants.HOST; | ||
| import static com.bytechef.component.ai.agent.chat.memory.redis.constant.RedisChatMemoryConstants.KEY_PREFIX; | ||
| import static com.bytechef.component.ai.agent.chat.memory.redis.constant.RedisChatMemoryConstants.PASSWORD; | ||
| import static com.bytechef.component.ai.agent.chat.memory.redis.constant.RedisChatMemoryConstants.PORT; | ||
| import static com.bytechef.component.ai.agent.chat.memory.redis.constant.RedisChatMemoryConstants.TIME_TO_LIVE; | ||
| import static com.bytechef.component.ai.agent.chat.memory.redis.constant.RedisChatMemoryConstants.USERNAME; | ||
| import static com.bytechef.component.definition.ComponentDsl.authorization; | ||
| import static com.bytechef.component.definition.ComponentDsl.component; | ||
| import static com.bytechef.component.definition.ComponentDsl.connection; | ||
| import static com.bytechef.component.definition.ComponentDsl.integer; | ||
| import static com.bytechef.component.definition.ComponentDsl.string; | ||
|
|
||
| import com.bytechef.component.ComponentHandler; | ||
| import com.bytechef.component.ai.agent.chat.memory.redis.action.RedisChatMemoryAddMessagesAction; | ||
| import com.bytechef.component.ai.agent.chat.memory.redis.action.RedisChatMemoryDeleteAction; | ||
| import com.bytechef.component.ai.agent.chat.memory.redis.action.RedisChatMemoryGetMessagesAction; | ||
| import com.bytechef.component.ai.agent.chat.memory.redis.action.RedisChatMemoryListConversationsAction; | ||
| import com.bytechef.component.ai.agent.chat.memory.redis.cluster.RedisChatMemory; | ||
| import com.bytechef.component.definition.Authorization.AuthorizationType; | ||
| import com.bytechef.component.definition.ComponentCategory; | ||
| import com.bytechef.component.definition.ComponentDefinition; | ||
| import com.bytechef.component.definition.ComponentDsl.ModifiableConnectionDefinition; | ||
| import com.bytechef.component.definition.Property.ControlType; | ||
| import com.google.auto.service.AutoService; | ||
|
|
||
| /** | ||
| * @author Ivica Cardic | ||
| */ | ||
| @AutoService(ComponentHandler.class) | ||
| public class RedisChatMemoryComponentHandler implements ComponentHandler { | ||
|
|
||
| private static final ModifiableConnectionDefinition CONNECTION_DEFINITION = connection() | ||
| .properties( | ||
| string(HOST) | ||
| .label("Host") | ||
| .description("The Redis server host.") | ||
| .defaultValue("localhost") | ||
| .required(true), | ||
| integer(PORT) | ||
| .label("Port") | ||
| .description("The Redis server port.") | ||
| .defaultValue(6379) | ||
| .required(true), | ||
| string(KEY_PREFIX) | ||
| .label("Key Prefix") | ||
| .description("The prefix for Redis keys used to store chat messages.") | ||
| .defaultValue(DEFAULT_KEY_PREFIX) | ||
| .required(false), | ||
| string(TIME_TO_LIVE) | ||
| .label("Time to Live") | ||
| .description( | ||
| "The time-to-live for chat messages (e.g., '24h', '7d', '30m'). Leave empty for no expiration.") | ||
| .required(false)) | ||
| .authorizations( | ||
| authorization(AuthorizationType.CUSTOM) | ||
| .properties( | ||
| string(USERNAME) | ||
| .label("Username") | ||
| .description("The Redis username (optional, for Redis 6.0+ ACL).") | ||
| .required(false), | ||
| string(PASSWORD) | ||
| .label("Password") | ||
| .description("The Redis password.") | ||
| .controlType(ControlType.PASSWORD) | ||
| .required(false))); | ||
|
|
||
| private static final ComponentDefinition COMPONENT_DEFINITION = component("redisChatMemory") | ||
| .title("Redis Chat Memory") | ||
| .description("Redis Chat Memory stores conversation history in Redis for fast, persistent storage.") | ||
| .icon("path:assets/redis-chat-memory.svg") | ||
| .categories(ComponentCategory.ARTIFICIAL_INTELLIGENCE) | ||
| .connection(CONNECTION_DEFINITION) | ||
| .actions( | ||
| RedisChatMemoryAddMessagesAction.ACTION_DEFINITION, | ||
| RedisChatMemoryGetMessagesAction.ACTION_DEFINITION, | ||
| RedisChatMemoryDeleteAction.ACTION_DEFINITION, | ||
| RedisChatMemoryListConversationsAction.ACTION_DEFINITION) | ||
| .clusterElements(RedisChatMemory.CLUSTER_ELEMENT_DEFINITION); | ||
|
|
||
| @Override | ||
| public ComponentDefinition getDefinition() { | ||
| return COMPONENT_DEFINITION; | ||
| } | ||
| } |
118 changes: 118 additions & 0 deletions
118
...ytechef/component/ai/agent/chat/memory/redis/action/RedisChatMemoryAddMessagesAction.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| /* | ||
| * Copyright 2025 ByteChef | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.bytechef.component.ai.agent.chat.memory.redis.action; | ||
|
|
||
| import static com.bytechef.component.ai.agent.chat.memory.redis.constant.RedisChatMemoryConstants.CONVERSATION_ID; | ||
| import static com.bytechef.component.ai.agent.chat.memory.redis.constant.RedisChatMemoryConstants.MESSAGES; | ||
| import static com.bytechef.component.ai.agent.chat.memory.redis.constant.RedisChatMemoryConstants.MESSAGE_CONTENT; | ||
| import static com.bytechef.component.ai.agent.chat.memory.redis.constant.RedisChatMemoryConstants.MESSAGE_ROLE; | ||
| import static com.bytechef.component.ai.agent.chat.memory.redis.util.RedisChatMemoryUtils.getChatMemoryRepository; | ||
| import static com.bytechef.component.definition.ComponentDsl.action; | ||
| import static com.bytechef.component.definition.ComponentDsl.array; | ||
| import static com.bytechef.component.definition.ComponentDsl.integer; | ||
| import static com.bytechef.component.definition.ComponentDsl.object; | ||
| import static com.bytechef.component.definition.ComponentDsl.option; | ||
| import static com.bytechef.component.definition.ComponentDsl.outputSchema; | ||
| import static com.bytechef.component.definition.ComponentDsl.string; | ||
|
|
||
| import com.bytechef.component.definition.ActionContext; | ||
| import com.bytechef.component.definition.ComponentDsl.ModifiableActionDefinition; | ||
| import com.bytechef.component.definition.Parameters; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import org.springframework.ai.chat.memory.ChatMemoryRepository; | ||
| import org.springframework.ai.chat.messages.AssistantMessage; | ||
| import org.springframework.ai.chat.messages.Message; | ||
| import org.springframework.ai.chat.messages.UserMessage; | ||
|
|
||
| /** | ||
| * @author Ivica Cardic | ||
| */ | ||
| public class RedisChatMemoryAddMessagesAction { | ||
|
|
||
| public static final ModifiableActionDefinition ACTION_DEFINITION = action("addMessages") | ||
| .title("Add Messages") | ||
| .description("Adds messages to the chat memory for a conversation.") | ||
| .properties( | ||
| string(CONVERSATION_ID) | ||
| .label("Conversation ID") | ||
| .description("The unique identifier for the conversation.") | ||
| .required(true), | ||
| array(MESSAGES) | ||
| .label("Messages") | ||
| .description("The messages to add to the conversation.") | ||
| .required(true) | ||
| .items( | ||
| object() | ||
| .properties( | ||
| string(MESSAGE_ROLE) | ||
| .label("Role") | ||
| .description("The role of the message sender.") | ||
| .required(true) | ||
| .options( | ||
| option("User", "user"), | ||
| option("Assistant", "assistant")), | ||
| string(MESSAGE_CONTENT) | ||
| .label("Content") | ||
| .description("The content of the message.") | ||
| .required(true)))) | ||
| .output( | ||
| outputSchema( | ||
| object() | ||
| .properties( | ||
| string(CONVERSATION_ID), | ||
| integer("messageCount")))) | ||
| .perform(RedisChatMemoryAddMessagesAction::perform); | ||
|
|
||
| private RedisChatMemoryAddMessagesAction() { | ||
| } | ||
|
|
||
| protected static Object perform( | ||
| Parameters inputParameters, Parameters connectionParameters, ActionContext context) { | ||
|
|
||
| String conversationId = inputParameters.getRequiredString(CONVERSATION_ID); | ||
| Object[] messagesArray = inputParameters.getRequiredArray(MESSAGES); | ||
|
|
||
| ChatMemoryRepository repository = getChatMemoryRepository(connectionParameters); | ||
| List<Message> existingMessages = new ArrayList<>(repository.findByConversationId(conversationId)); | ||
|
|
||
| for (Object messageObj : messagesArray) { | ||
| if (messageObj instanceof Map<?, ?> messageMap) { | ||
| String role = (String) messageMap.get(MESSAGE_ROLE); | ||
| String content = (String) messageMap.get(MESSAGE_CONTENT); | ||
| Message message = createMessage(role, content); | ||
|
|
||
| existingMessages.add(message); | ||
| } | ||
| } | ||
|
|
||
| repository.saveAll(conversationId, existingMessages); | ||
|
|
||
| return Map.of( | ||
| CONVERSATION_ID, conversationId, | ||
| "messageCount", existingMessages.size()); | ||
| } | ||
|
|
||
| private static Message createMessage(String role, String content) { | ||
| return switch (role) { | ||
| case "user" -> new UserMessage(content); | ||
| case "assistant" -> new AssistantMessage(content); | ||
| default -> throw new IllegalArgumentException("Unsupported role: " + role); | ||
| }; | ||
| } | ||
| } | ||
70 changes: 70 additions & 0 deletions
70
...com/bytechef/component/ai/agent/chat/memory/redis/action/RedisChatMemoryDeleteAction.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| /* | ||
| * Copyright 2025 ByteChef | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.bytechef.component.ai.agent.chat.memory.redis.action; | ||
|
|
||
| import static com.bytechef.component.ai.agent.chat.memory.redis.constant.RedisChatMemoryConstants.CONVERSATION_ID; | ||
| import static com.bytechef.component.ai.agent.chat.memory.redis.util.RedisChatMemoryUtils.getChatMemoryRepository; | ||
| import static com.bytechef.component.definition.ComponentDsl.action; | ||
| import static com.bytechef.component.definition.ComponentDsl.bool; | ||
| import static com.bytechef.component.definition.ComponentDsl.object; | ||
| import static com.bytechef.component.definition.ComponentDsl.outputSchema; | ||
| import static com.bytechef.component.definition.ComponentDsl.string; | ||
|
|
||
| import com.bytechef.component.definition.ActionContext; | ||
| import com.bytechef.component.definition.ComponentDsl.ModifiableActionDefinition; | ||
| import com.bytechef.component.definition.Parameters; | ||
| import java.util.Map; | ||
| import org.springframework.ai.chat.memory.ChatMemoryRepository; | ||
|
|
||
| /** | ||
| * @author Ivica Cardic | ||
| */ | ||
| public class RedisChatMemoryDeleteAction { | ||
|
|
||
| public static final ModifiableActionDefinition ACTION_DEFINITION = action("deleteConversation") | ||
| .title("Delete Conversation") | ||
| .description("Deletes all messages for a conversation.") | ||
| .properties( | ||
| string(CONVERSATION_ID) | ||
| .label("Conversation ID") | ||
| .description("The unique identifier for the conversation to delete.") | ||
| .required(true)) | ||
| .output( | ||
| outputSchema( | ||
| object() | ||
| .properties( | ||
| string(CONVERSATION_ID), | ||
| bool("deleted")))) | ||
| .perform(RedisChatMemoryDeleteAction::perform); | ||
|
|
||
| private RedisChatMemoryDeleteAction() { | ||
| } | ||
|
|
||
| protected static Object perform( | ||
| Parameters inputParameters, Parameters connectionParameters, ActionContext context) { | ||
|
|
||
| String conversationId = inputParameters.getRequiredString(CONVERSATION_ID); | ||
|
|
||
| ChatMemoryRepository repository = getChatMemoryRepository(connectionParameters); | ||
|
|
||
| repository.deleteByConversationId(conversationId); | ||
|
|
||
| return Map.of( | ||
| CONVERSATION_ID, conversationId, | ||
| "deleted", true); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.