Skip to content

Commit

Permalink
fix: LlamaParseReader as Standard Reader in SimpleDirectoryReader
Browse files Browse the repository at this point in the history
  • Loading branch information
KindOfAScam committed May 4, 2024
1 parent c30575c commit cb2b673
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
14 changes: 13 additions & 1 deletion packages/core/src/readers/SimpleDirectoryReader.edge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,19 @@ export class SimpleDirectoryReader implements BaseReader {
continue;
}

const fileDocs = await reader.loadData(filePath, fs);
let fileDocs: Document<Metadata>[] = [];

try {
const loadedData = await reader.loadData(filePath, fs);
if (Array.isArray(loadedData)) {
fileDocs = loadedData.flat(); // Ensure flat structure
} else {
fileDocs = [loadedData];
}
} catch (error) {
console.error(`Error loading data for file ${filePath}: ${error}`);
}

fileDocs.forEach(addMetaData(filePath));

// Observer can still cancel addition of the resulting docs from this file
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/readers/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { Document } from "../Node.js";
* A reader takes imports data into Document objects.
*/
export interface BaseReader {
loadData(...args: unknown[]): Promise<Document[]>;
loadData(...args: unknown[]): Promise<Document[] | Document[][]>;
}

/**
Expand All @@ -16,9 +16,9 @@ export interface FileReader extends BaseReader {
}

/**
* A reader takes multiple file paths and imports data into Document objects.
* A reader takes multiple file paths and imports data into an array of Document objects.
*/
export interface MultiReader {
export interface MultiReader extends BaseReader {
loadData(filePath: string, fs?: CompleteFileSystem): Promise<Document[]>;
loadData(filePaths: string[], fs?: CompleteFileSystem): Promise<Document[][]>;
}
Expand Down

0 comments on commit cb2b673

Please sign in to comment.