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
Expand Up @@ -63,15 +63,21 @@ public FilesystemFileStorageService(String baseDir) {

@Override
public void deleteFile(String directory, FileEntry fileEntry) {
Path directoryPath = resolveDirectoryPath(directory);
Path filePath = resolveFilePath(directoryPath, directory, fileEntry.getUrl());
if (fileEntry != null) {
Path directoryPath = resolveDirectoryPath(directory);
Path filePath = resolveFilePath(directoryPath, directory, fileEntry.getUrl());

File file = filePath.toFile();
File file = filePath.toFile();

if (!file.exists()) {
return;
}

boolean deleted = file.delete();
boolean deleted = file.delete();

if (!deleted) {
throw new FileStorageException("File %s cannot be deleted".formatted(directoryPath));
if (!deleted) {
throw new FileStorageException("File %s cannot be deleted".formatted(filePath));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,10 @@ private static String deriveDocumentName(List<Document> documents) {

Map<String, Object> metadata = document.getMetadata();

Object source = metadata.get("source");
Object filename = metadata.get("filename");

if (source != null) {
return source.toString();
if (filename != null) {
return filename.toString();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
import com.bytechef.component.definition.FileEntry;
import com.bytechef.component.definition.Parameters;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import org.springframework.ai.document.Document;
import org.springframework.ai.document.DocumentReader;
import org.springframework.core.io.FileSystemResource;

/**
Expand All @@ -38,6 +42,19 @@ protected static FileResult getFile(Parameters inputParameters, Context context)
return new FileResult(fileEntry, fileSystemResource);
}

protected static DocumentReader withFilename(DocumentReader reader, String filename) {
return () -> reader.read()
.stream()
.map(document -> {
Map<String, Object> metadata = new HashMap<>(document.getMetadata());

metadata.put("filename", filename);

return new Document(document.getId(), document.getText(), metadata);
})
.toList();
}

protected record FileResult(FileEntry fileEntry, FileSystemResource fileSystemResource) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.bytechef.component.definition.ClusterElementDefinition;
import com.bytechef.component.definition.ComponentDsl;
import com.bytechef.component.definition.Context;
import com.bytechef.component.definition.FileEntry;
import com.bytechef.component.definition.Parameters;
import com.bytechef.platform.component.definition.ai.vectorstore.DocumentReaderFunction;

Expand Down Expand Up @@ -49,6 +50,10 @@ protected static org.springframework.ai.document.DocumentReader apply(

FileResult result = getFile(inputParameters, context);

return new org.springframework.ai.reader.jsoup.JsoupDocumentReader(result.fileSystemResource());
FileEntry fileEntry = result.fileEntry();

return withFilename(
new org.springframework.ai.reader.jsoup.JsoupDocumentReader(result.fileSystemResource()),
fileEntry.getName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.bytechef.component.definition.ClusterElementDefinition;
import com.bytechef.component.definition.ComponentDsl;
import com.bytechef.component.definition.Context;
import com.bytechef.component.definition.FileEntry;
import com.bytechef.component.definition.Parameters;
import com.bytechef.platform.component.definition.ai.vectorstore.DocumentReaderFunction;
import java.util.List;
Expand All @@ -51,14 +52,18 @@ public class JsonDocumentReader extends AbstractDocumentReader {
.required(false))
.object(() -> JsonDocumentReader::apply);

protected static JsonReader apply(
protected static org.springframework.ai.document.DocumentReader apply(
Parameters inputParameters, Parameters connectionParameters, Context context) {

FileResult result = getFile(inputParameters, context);

List<String> keys = inputParameters.getList(JSON_KEYS_TO_USE, String.class);

return (keys == null) ? new JsonReader(result.fileSystemResource())
JsonReader jsonReader = (keys == null) ? new JsonReader(result.fileSystemResource())
: new JsonReader(result.fileSystemResource(), keys.toArray(new String[0]));

FileEntry fileEntry = result.fileEntry();

return withFilename(jsonReader, fileEntry.getName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.bytechef.component.definition.ClusterElementDefinition;
import com.bytechef.component.definition.ComponentDsl;
import com.bytechef.component.definition.Context;
import com.bytechef.component.definition.FileEntry;
import com.bytechef.component.definition.Parameters;
import com.bytechef.platform.component.definition.ai.vectorstore.DocumentReaderFunction;
import org.springframework.ai.reader.ExtractedTextFormatter;
Expand All @@ -41,7 +42,7 @@ public class PagePdfDocumentReader extends AbstractDocumentReader {
DOCUMENT_PROPERTY)
.object(() -> PagePdfDocumentReader::apply);

protected static org.springframework.ai.reader.pdf.PagePdfDocumentReader apply(
protected static org.springframework.ai.document.DocumentReader apply(
Parameters inputParameters, Parameters connectionParameters, Context context) {

FileResult result = getFile(inputParameters, context);
Expand All @@ -55,6 +56,10 @@ protected static org.springframework.ai.reader.pdf.PagePdfDocumentReader apply(
.withPagesPerDocument(1)
.build();

return new org.springframework.ai.reader.pdf.PagePdfDocumentReader(result.fileSystemResource(), config);
FileEntry fileEntry = result.fileEntry();

return withFilename(
new org.springframework.ai.reader.pdf.PagePdfDocumentReader(result.fileSystemResource(), config),
fileEntry.getName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.bytechef.component.definition.ClusterElementDefinition;
import com.bytechef.component.definition.ComponentDsl;
import com.bytechef.component.definition.Context;
import com.bytechef.component.definition.FileEntry;
import com.bytechef.component.definition.Parameters;
import com.bytechef.platform.component.definition.ai.vectorstore.DocumentReaderFunction;

Expand Down Expand Up @@ -51,6 +52,10 @@ protected static org.springframework.ai.document.DocumentReader apply(

FileResult result = getFile(inputParameters, context);

return new org.springframework.ai.reader.tika.TikaDocumentReader(result.fileSystemResource());
FileEntry fileEntry = result.fileEntry();

return withFilename(
new org.springframework.ai.reader.tika.TikaDocumentReader(result.fileSystemResource()),
fileEntry.getName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ public interface KnowledgeBaseFileStorage {
*/
void deleteDocument(FileEntry fileEntry);

/**
* Checks whether the chunk content file associated with the given file entry exists in storage.
*
* @param fileEntry the file entry representing the chunk to check
* @return true if the file exists, false otherwise
*/
boolean chunkContentExists(FileEntry fileEntry);

/**
* Reads the chunk content associated with the given file entry.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public KnowledgeBaseFileStorageImpl(FileStorageService fileStorageService) {
this.fileStorageService = fileStorageService;
}

@Override
public boolean chunkContentExists(FileEntry fileEntry) {
return fileStorageService.fileExists(KNOWLEDGE_BASE_CHUNKS_DIR, fileEntry);
}

@Override
public void deleteChunkContent(FileEntry fileEntry) {
fileStorageService.deleteFile(KNOWLEDGE_BASE_CHUNKS_DIR, fileEntry);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public List<KnowledgeBaseDocumentChunk> getKnowledgeBaseDocumentChunksByDocument
for (KnowledgeBaseDocumentChunk chunk : chunks) {
FileEntry contentFileEntry = chunk.getContent();

if (contentFileEntry != null) {
if (contentFileEntry != null && knowledgeBaseFileStorage.chunkContentExists(contentFileEntry)) {
String textContent = knowledgeBaseFileStorage.readChunkContent(contentFileEntry);

chunk.setTextContent(textContent);
Expand Down
Loading