Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"))
}
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;
}
}
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);

Comment thread
ivicac marked this conversation as resolved.
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);
};
}
}
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);
}
}
Loading
Loading