Skip to content

feat(memory): First commit of file-based memory repository #3210

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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,86 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2023-2024 the original author or authors.
~
~ 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.
-->
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../../../../../../pom.xml</relativePath>
</parent>
<artifactId>spring-ai-autoconfigure-model-chat-memory-repository-file</artifactId>
<packaging>jar</packaging>
<name>Spring AI File Chat Memory Repository Auto Configuration</name>
<description>Spring File AI Chat Memory Repository Auto Configuration</description>
<url>https://github.com/spring-projects/spring-ai</url>

<scm>
<url>https://github.com/spring-projects/spring-ai</url>
<connection>git://github.com/spring-projects/spring-ai.git</connection>
<developerConnection>git@github.com:spring-projects/spring-ai.git</developerConnection>
</scm>

<dependencies>

<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-model-chat-memory-repository-file</artifactId>
<version>${project.parent.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-autoconfigure-model-chat-memory</artifactId>
<version>${project.parent.version}</version>
</dependency>

<!-- Boot dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure-processor</artifactId>
<optional>true</optional>
</dependency>

<!-- Test dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2025-2025 the original author or authors.
*
* 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 org.springframework.ai.model.chat.memory.repository.file.autoconfigure;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.ai.chat.memory.ChatMemoryRepository;
import org.springframework.ai.chat.memory.repository.file.FileChatMemoryRepository;
import org.springframework.ai.model.chat.memory.autoconfigure.ChatMemoryAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

import java.nio.file.Path;
import java.nio.file.Paths;

/**
* @author John Dahle
*/
@AutoConfiguration(before = ChatMemoryAutoConfiguration.class)
@EnableConfigurationProperties(FileChatMemoryRepositoryProperties.class)
@ConditionalOnProperty(prefix = FileChatMemoryRepositoryProperties.CONFIG_PREFIX, name = "enabled",
havingValue = "true", matchIfMissing = true)
public class FileChatMemoryRepositoryAutoConfiguration {

private final FileChatMemoryRepositoryProperties props;

public FileChatMemoryRepositoryAutoConfiguration(FileChatMemoryRepositoryProperties props) {
this.props = props;
}

@Bean
@ConditionalOnMissingBean(ChatMemoryRepository.class)
public FileChatMemoryRepository fileChatMemoryRepository(ObjectMapper objectMapper) {
Path baseDir = Paths.get(props.getBaseDir());
return new FileChatMemoryRepository(baseDir, objectMapper);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2025-2025 the original author or authors.
*
* 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 org.springframework.ai.model.chat.memory.repository.file.autoconfigure;

import org.springframework.boot.context.properties.ConfigurationProperties;

/**
* @author John Dahle
*/

@ConfigurationProperties(FileChatMemoryRepositoryProperties.CONFIG_PREFIX)
public class FileChatMemoryRepositoryProperties {

public static final String CONFIG_PREFIX = "spring.ai.chat.memory.repository.file";
private boolean enabled = true;
private String baseDir = System.getProperty("user.home") + "/.spring-ai/chat-memory";

public boolean isEnabled() {
return enabled;
}

public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

public String getBaseDir() {
return baseDir;
}

public void setBaseDir(String baseDir) {
this.baseDir = baseDir;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#
# Copyright 2024-2025 the original author or authors.
#
# 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.
#
org.springframework.ai.model.chat.memory.repository.file.autoconfigure.FileChatMemoryRepositoryAutoConfiguration
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2023-2024 the original author or authors.
~
~ 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.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../../../pom.xml</relativePath>
</parent>

<artifactId>spring-ai-model-chat-memory-repository-file</artifactId>
<name>Spring File-based Chat Memory Repository</name>
<description>Spring File-based Chat Memory Repository implementation</description>

<url>https://github.com/spring-projects/spring-ai</url>

<scm>
<url>https://github.com/spring-projects/spring-ai</url>
<connection>git://github.com/spring-projects/spring-ai.git</connection>
<developerConnection>git@github.com:spring-projects/spring-ai.git</developerConnection>
</scm>

<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-client-chat</artifactId>
<version>${project.version}</version>
</dependency>

<!-- TESTING -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-test</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Copyright 2025-2025 the original author or authors.
*
* 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 org.springframework.ai.chat.memory.repository.file;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.ai.chat.memory.ChatMemoryRepository;
import org.springframework.ai.chat.memory.repository.file.dto.MessageDto;
import org.springframework.ai.chat.memory.repository.file.dto.MessageDtoMapper;
import org.springframework.ai.chat.messages.Message;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
/**
* @author John Dahle
*/
public class FileChatMemoryRepository implements ChatMemoryRepository {

private final Path baseDir;

private final ObjectMapper objectMapper;

public FileChatMemoryRepository(Path baseDir, ObjectMapper objectMapper) {
this.baseDir = baseDir;
this.objectMapper = objectMapper;
try {
Files.createDirectories(baseDir);
}
catch (IOException e) {
throw new RuntimeException("Failed to create base directory: " + baseDir, e);
}
}

private Path fileFor(String conversationId) {
return baseDir.resolve(conversationId + ".json");
}

@Override
public List<String> findConversationIds() {
try (var stream = Files.list(baseDir)) {
return stream.filter(p -> p.toString().endsWith(".json"))
.map(p -> p.getFileName().toString().replaceFirst("\\.json$", ""))
.collect(Collectors.toList());
}
catch (IOException e) {
throw new RuntimeException("Failed to list conversation IDs", e);
}
}

@Override
public List<Message> findByConversationId(String conversationId) {
Path file = fileFor(conversationId);
if (!Files.exists(file)) {
return Collections.emptyList();
}
try {
// 1. Read DTOs from disk
List<MessageDto> dtos = objectMapper.readValue(file.toFile(), new TypeReference<List<MessageDto>>() {
});
// 2. Map them back to domain Messages
return MessageDtoMapper.toDomainList(dtos);
}
catch (IOException e) {
throw new RuntimeException("Failed to read messages for conversation: " + conversationId, e);
}
}

@Override
public void saveAll(String conversationId, List<Message> messages) {
try {
// 1. Convert domain Messages into DTOs
List<MessageDto> dtos = MessageDtoMapper.toDtoList(messages);
// 2. Tell Jackson they’re DTOs and write them
objectMapper.writerFor(new TypeReference<List<MessageDto>>() {
}).writeValue(fileFor(conversationId).toFile(), dtos);
}
catch (IOException e) {
throw new RuntimeException("Failed to write messages for conversation: " + conversationId, e);
}
}

@Override
public void deleteByConversationId(String conversationId) {
try {
Files.deleteIfExists(fileFor(conversationId));
}
catch (IOException e) {
throw new RuntimeException("Failed to delete conversation: " + conversationId, e);
}
}

}
Loading