1
import { DocumentStore } from './database/entities/DocumentStore'
3
export enum DocumentStoreStatus {
11
export interface IDocumentStore {
15
loaders: string // JSON string
16
whereUsed: string // JSON string
19
status: DocumentStoreStatus
22
export interface IDocumentStoreFileChunk {
31
export interface IDocumentStoreFileChunkPagedResponse {
32
chunks: IDocumentStoreFileChunk[]
34
file?: IDocumentStoreLoader
40
export interface IDocumentStoreLoader {
44
loaderConfig: any // JSON string
47
splitterConfig: any // JSON string
50
status: DocumentStoreStatus
52
files?: IDocumentStoreLoaderFile[]
57
export interface IDocumentStoreLoaderForPreview extends IDocumentStoreLoader {
60
previewChunkCount: number
63
export interface IDocumentStoreLoaderFile {
68
status: DocumentStoreStatus
72
export interface IDocumentStoreWhereUsed {
77
export class DocumentStoreDTO {
81
files: IDocumentStoreLoaderFile[]
82
whereUsed: IDocumentStoreWhereUsed[]
85
status: DocumentStoreStatus
91
loaders: IDocumentStoreLoader[]
95
static fromEntity(entity: DocumentStore): DocumentStoreDTO {
96
let documentStoreDTO = new DocumentStoreDTO()
98
Object.assign(documentStoreDTO, entity)
99
documentStoreDTO.id = entity.id
100
documentStoreDTO.name = entity.name
101
documentStoreDTO.description = entity.description
102
documentStoreDTO.status = entity.status
103
documentStoreDTO.totalChars = 0
104
documentStoreDTO.totalChunks = 0
106
if (entity.whereUsed) {
107
documentStoreDTO.whereUsed = JSON.parse(entity.whereUsed)
109
documentStoreDTO.whereUsed = []
112
if (entity.loaders) {
113
documentStoreDTO.loaders = JSON.parse(entity.loaders)
114
documentStoreDTO.loaders.map((loader) => {
115
documentStoreDTO.totalChars += loader.totalChars
116
documentStoreDTO.totalChunks += loader.totalChunks
117
switch (loader.loaderId) {
119
loader.source = loader.loaderConfig.pdfFile.replace('FILE-STORAGE::', '')
122
loader.source = loader.loaderConfig.url + ' (' + loader.loaderConfig.method + ')'
124
case 'cheerioWebScraper':
125
loader.source = loader.loaderConfig.url
128
loader.source = loader.loaderConfig.jsonFile.replace('FILE-STORAGE::', '')
131
loader.source = loader.loaderConfig.docxFile.replace('FILE-STORAGE::', '')
134
loader.source = loader.loaderConfig.txtFile.replace('FILE-STORAGE::', '')
136
case 'unstructuredFileLoader':
137
loader.source = loader.loaderConfig.filePath
140
loader.source = 'None'
143
if (loader.status !== 'SYNC') {
144
documentStoreDTO.status = DocumentStoreStatus.STALE
149
return documentStoreDTO
152
static fromEntities(entities: DocumentStore[]): DocumentStoreDTO[] {
153
return entities.map((entity) => this.fromEntity(entity))
156
static toEntity(body: any): DocumentStore {
157
const docStore = new DocumentStore()
158
Object.assign(docStore, body)
159
docStore.loaders = '[]'
160
docStore.whereUsed = '[]'
161
// when a new document store is created, it is empty and in sync
162
docStore.status = DocumentStoreStatus.EMPTY_SYNC