|
| 1 | +/* |
| 2 | + * Copyright 2025-2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.ai.chat.memory.repository.file; |
| 17 | + |
| 18 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 19 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | +import org.springframework.ai.chat.memory.ChatMemoryRepository; |
| 21 | +import org.springframework.ai.chat.memory.repository.file.dto.MessageDto; |
| 22 | +import org.springframework.ai.chat.memory.repository.file.dto.MessageDtoMapper; |
| 23 | +import org.springframework.ai.chat.messages.Message; |
| 24 | + |
| 25 | +import java.io.IOException; |
| 26 | +import java.nio.file.Files; |
| 27 | +import java.nio.file.Path; |
| 28 | +import java.util.Collections; |
| 29 | +import java.util.List; |
| 30 | +import java.util.stream.Collectors; |
| 31 | +/** |
| 32 | + * @author John Dahle |
| 33 | + */ |
| 34 | +public class FileChatMemoryRepository implements ChatMemoryRepository { |
| 35 | + |
| 36 | + private final Path baseDir; |
| 37 | + |
| 38 | + private final ObjectMapper objectMapper; |
| 39 | + |
| 40 | + public FileChatMemoryRepository(Path baseDir, ObjectMapper objectMapper) { |
| 41 | + this.baseDir = baseDir; |
| 42 | + this.objectMapper = objectMapper; |
| 43 | + try { |
| 44 | + Files.createDirectories(baseDir); |
| 45 | + } |
| 46 | + catch (IOException e) { |
| 47 | + throw new RuntimeException("Failed to create base directory: " + baseDir, e); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + private Path fileFor(String conversationId) { |
| 52 | + return baseDir.resolve(conversationId + ".json"); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public List<String> findConversationIds() { |
| 57 | + try (var stream = Files.list(baseDir)) { |
| 58 | + return stream.filter(p -> p.toString().endsWith(".json")) |
| 59 | + .map(p -> p.getFileName().toString().replaceFirst("\\.json$", "")) |
| 60 | + .collect(Collectors.toList()); |
| 61 | + } |
| 62 | + catch (IOException e) { |
| 63 | + throw new RuntimeException("Failed to list conversation IDs", e); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public List<Message> findByConversationId(String conversationId) { |
| 69 | + Path file = fileFor(conversationId); |
| 70 | + if (!Files.exists(file)) { |
| 71 | + return Collections.emptyList(); |
| 72 | + } |
| 73 | + try { |
| 74 | + // 1. Read DTOs from disk |
| 75 | + List<MessageDto> dtos = objectMapper.readValue(file.toFile(), new TypeReference<List<MessageDto>>() { |
| 76 | + }); |
| 77 | + // 2. Map them back to domain Messages |
| 78 | + return MessageDtoMapper.toDomainList(dtos); |
| 79 | + } |
| 80 | + catch (IOException e) { |
| 81 | + throw new RuntimeException("Failed to read messages for conversation: " + conversationId, e); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public void saveAll(String conversationId, List<Message> messages) { |
| 87 | + try { |
| 88 | + // 1. Convert domain Messages into DTOs |
| 89 | + List<MessageDto> dtos = MessageDtoMapper.toDtoList(messages); |
| 90 | + // 2. Tell Jackson they’re DTOs and write them |
| 91 | + objectMapper.writerFor(new TypeReference<List<MessageDto>>() { |
| 92 | + }).writeValue(fileFor(conversationId).toFile(), dtos); |
| 93 | + } |
| 94 | + catch (IOException e) { |
| 95 | + throw new RuntimeException("Failed to write messages for conversation: " + conversationId, e); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public void deleteByConversationId(String conversationId) { |
| 101 | + try { |
| 102 | + Files.deleteIfExists(fileFor(conversationId)); |
| 103 | + } |
| 104 | + catch (IOException e) { |
| 105 | + throw new RuntimeException("Failed to delete conversation: " + conversationId, e); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | +} |
0 commit comments