|
|
@@ -5,6 +5,7 @@ import { type Model, type Document, Schema } from 'mongoose';
|
|
|
import { getOrCreateModel } from '~/server/util/mongoose-utils';
|
|
|
|
|
|
export interface VectorStoreFileRelation {
|
|
|
+ vectorStoreRelationId: mongoose.Types.ObjectId;
|
|
|
pageId: mongoose.Types.ObjectId;
|
|
|
fileIds: string[];
|
|
|
isAttachedToVectorStore: boolean;
|
|
|
@@ -18,7 +19,7 @@ interface VectorStoreFileRelationModel extends Model<VectorStoreFileRelation> {
|
|
|
}
|
|
|
|
|
|
export const prepareVectorStoreFileRelations = (
|
|
|
- pageId: Types.ObjectId, fileId: string, relationsMap: Map<string, VectorStoreFileRelation>,
|
|
|
+ vectorStoreRelationId: Types.ObjectId, pageId: Types.ObjectId, fileId: string, relationsMap: Map<string, VectorStoreFileRelation>,
|
|
|
): Map<string, VectorStoreFileRelation> => {
|
|
|
const pageIdStr = pageId.toHexString();
|
|
|
const existingData = relationsMap.get(pageIdStr);
|
|
|
@@ -30,6 +31,7 @@ export const prepareVectorStoreFileRelations = (
|
|
|
// If the data doesn't exist, create a new one and add it to the map
|
|
|
else {
|
|
|
relationsMap.set(pageIdStr, {
|
|
|
+ vectorStoreRelationId,
|
|
|
pageId,
|
|
|
fileIds: [fileId],
|
|
|
isAttachedToVectorStore: false,
|
|
|
@@ -40,6 +42,11 @@ export const prepareVectorStoreFileRelations = (
|
|
|
};
|
|
|
|
|
|
const schema = new Schema<VectorStoreFileRelationDocument, VectorStoreFileRelationModel>({
|
|
|
+ vectorStoreRelationId: {
|
|
|
+ type: Schema.Types.ObjectId,
|
|
|
+ ref: 'VectorStore',
|
|
|
+ required: true,
|
|
|
+ },
|
|
|
pageId: {
|
|
|
type: Schema.Types.ObjectId,
|
|
|
ref: 'Page',
|
|
|
@@ -63,7 +70,10 @@ schema.statics.upsertVectorStoreFileRelations = async function(vectorStoreFileRe
|
|
|
return {
|
|
|
updateOne: {
|
|
|
filter: { pageId: data.pageId },
|
|
|
- update: { $addToSet: { fileIds: { $each: data.fileIds } } },
|
|
|
+ update: {
|
|
|
+ $set: { vectorStoreId: data.vectorStoreRelationId },
|
|
|
+ $addToSet: { fileIds: { $each: data.fileIds } },
|
|
|
+ },
|
|
|
upsert: true,
|
|
|
},
|
|
|
};
|