|
@@ -6,7 +6,7 @@ import { getOrCreateModel } from '~/server/util/mongoose-utils';
|
|
|
|
|
|
|
|
export interface VectorStoreFileRelation {
|
|
export interface VectorStoreFileRelation {
|
|
|
vectorStoreRelationId: mongoose.Types.ObjectId;
|
|
vectorStoreRelationId: mongoose.Types.ObjectId;
|
|
|
- pageId: mongoose.Types.ObjectId;
|
|
|
|
|
|
|
+ page: mongoose.Types.ObjectId;
|
|
|
fileIds: string[];
|
|
fileIds: string[];
|
|
|
isAttachedToVectorStore: boolean;
|
|
isAttachedToVectorStore: boolean;
|
|
|
}
|
|
}
|
|
@@ -19,9 +19,9 @@ interface VectorStoreFileRelationModel extends Model<VectorStoreFileRelation> {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
export const prepareVectorStoreFileRelations = (
|
|
export const prepareVectorStoreFileRelations = (
|
|
|
- vectorStoreRelationId: Types.ObjectId, pageId: Types.ObjectId, fileId: string, relationsMap: Map<string, VectorStoreFileRelation>,
|
|
|
|
|
|
|
+ vectorStoreRelationId: Types.ObjectId, page: Types.ObjectId, fileId: string, relationsMap: Map<string, VectorStoreFileRelation>,
|
|
|
): Map<string, VectorStoreFileRelation> => {
|
|
): Map<string, VectorStoreFileRelation> => {
|
|
|
- const pageIdStr = pageId.toHexString();
|
|
|
|
|
|
|
+ const pageIdStr = page.toHexString();
|
|
|
const existingData = relationsMap.get(pageIdStr);
|
|
const existingData = relationsMap.get(pageIdStr);
|
|
|
|
|
|
|
|
// If the data exists, add the fileId to the fileIds array
|
|
// If the data exists, add the fileId to the fileIds array
|
|
@@ -32,7 +32,7 @@ export const prepareVectorStoreFileRelations = (
|
|
|
else {
|
|
else {
|
|
|
relationsMap.set(pageIdStr, {
|
|
relationsMap.set(pageIdStr, {
|
|
|
vectorStoreRelationId,
|
|
vectorStoreRelationId,
|
|
|
- pageId,
|
|
|
|
|
|
|
+ page,
|
|
|
fileIds: [fileId],
|
|
fileIds: [fileId],
|
|
|
isAttachedToVectorStore: false,
|
|
isAttachedToVectorStore: false,
|
|
|
});
|
|
});
|
|
@@ -47,7 +47,7 @@ const schema = new Schema<VectorStoreFileRelationDocument, VectorStoreFileRelati
|
|
|
ref: 'VectorStore',
|
|
ref: 'VectorStore',
|
|
|
required: true,
|
|
required: true,
|
|
|
},
|
|
},
|
|
|
- pageId: {
|
|
|
|
|
|
|
+ page: {
|
|
|
type: Schema.Types.ObjectId,
|
|
type: Schema.Types.ObjectId,
|
|
|
ref: 'Page',
|
|
ref: 'Page',
|
|
|
required: true,
|
|
required: true,
|
|
@@ -64,14 +64,14 @@ const schema = new Schema<VectorStoreFileRelationDocument, VectorStoreFileRelati
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
// define unique compound index
|
|
// define unique compound index
|
|
|
-schema.index({ vectorStoreRelationId: 1, pageId: 1 }, { unique: true });
|
|
|
|
|
|
|
+schema.index({ vectorStoreRelationId: 1, page: 1 }, { unique: true });
|
|
|
|
|
|
|
|
schema.statics.upsertVectorStoreFileRelations = async function(vectorStoreFileRelations: VectorStoreFileRelation[]): Promise<void> {
|
|
schema.statics.upsertVectorStoreFileRelations = async function(vectorStoreFileRelations: VectorStoreFileRelation[]): Promise<void> {
|
|
|
await this.bulkWrite(
|
|
await this.bulkWrite(
|
|
|
vectorStoreFileRelations.map((data) => {
|
|
vectorStoreFileRelations.map((data) => {
|
|
|
return {
|
|
return {
|
|
|
updateOne: {
|
|
updateOne: {
|
|
|
- filter: { pageId: data.pageId, vectorStoreRelationId: data.vectorStoreRelationId },
|
|
|
|
|
|
|
+ filter: { page: data.page, vectorStoreRelationId: data.vectorStoreRelationId },
|
|
|
update: {
|
|
update: {
|
|
|
$addToSet: { fileIds: { $each: data.fileIds } },
|
|
$addToSet: { fileIds: { $each: data.fileIds } },
|
|
|
},
|
|
},
|
|
@@ -85,7 +85,7 @@ schema.statics.upsertVectorStoreFileRelations = async function(vectorStoreFileRe
|
|
|
// Used when attached to VectorStore
|
|
// Used when attached to VectorStore
|
|
|
schema.statics.markAsAttachedToVectorStore = async function(pageIds: Types.ObjectId[]): Promise<void> {
|
|
schema.statics.markAsAttachedToVectorStore = async function(pageIds: Types.ObjectId[]): Promise<void> {
|
|
|
await this.updateMany(
|
|
await this.updateMany(
|
|
|
- { pageId: { $in: pageIds } },
|
|
|
|
|
|
|
+ { page: { $in: pageIds } },
|
|
|
{ $set: { isAttachedToVectorStore: true } },
|
|
{ $set: { isAttachedToVectorStore: true } },
|
|
|
);
|
|
);
|
|
|
};
|
|
};
|