Просмотр исходного кода

Implement Thread model (embedded mode)

Shun Miyazawa 1 год назад
Родитель
Сommit
6088dcd703

+ 43 - 28
apps/app/src/features/openai/server/models/thread-relation.ts

@@ -11,44 +11,66 @@ const generateExpirationDate = (): Date => {
   return expirationDate;
   return expirationDate;
 };
 };
 
 
+
+/*
+*  Thread Model
+*/
 interface Thread {
 interface Thread {
   threadId: string;
   threadId: string;
   expiredAt: Date;
   expiredAt: Date;
 }
 }
+
+interface ThreadDocument extends Thread, Document {
+  updateExpiration(): Promise<void>;
+}
+
+type ThreadModel = Model<ThreadDocument>
+
+const threadSchema = new Schema<Thread, ThreadDocument, ThreadModel>({
+  threadId: {
+    type: String,
+    required: true,
+  },
+  expiredAt: {
+    type: Date,
+    required: true,
+  },
+});
+
+threadSchema.methods.updateExpiration = async function(): Promise<void> {
+  this.expiredAt = generateExpirationDate();
+  this.parent().save();
+};
+
+
+/*
+*  Thread Relation Model
+*/
 interface ThreadRelation {
 interface ThreadRelation {
   userId: mongoose.Types.ObjectId;
   userId: mongoose.Types.ObjectId;
-  threads: Thread[];
+  threads: ThreadDocument[];
+}
+interface ThreadRelationDocument extends ThreadRelation, Document {
+  updateExpiration(threadId: string): Promise<void>;
 }
 }
 
 
-interface ThreadDocument extends ThreadRelation, Document {}
-
-interface ThreadRelationModel extends Model<ThreadDocument> {
+interface ThreadRelationModel extends Model<ThreadRelationDocument> {
   upsertThreadRelation(userId: string, threadId: string): Promise<void>;
   upsertThreadRelation(userId: string, threadId: string): Promise<void>;
-  getThreadRelationAndUpdateExpiration(userId: string, threadId: string): Promise<ThreadDocument | null>
+  getThreadRelation(userId: string, threadId: string): Promise<ThreadRelationDocument | null>
 }
 }
 
 
-const schema = new Schema<ThreadDocument, ThreadRelationModel>({
+const threadRelationSchema = new Schema<ThreadRelationDocument, ThreadRelationModel>({
   userId: {
   userId: {
     type: Schema.Types.ObjectId,
     type: Schema.Types.ObjectId,
     ref: 'User',
     ref: 'User',
     required: true,
     required: true,
     unique: true,
     unique: true,
   },
   },
-  threads: [{
-    threadId: {
-      type: String,
-      required: true,
-    },
-    expiredAt: {
-      type: Date,
-      required: true,
-    },
-  },
-  ],
+  threads: [threadSchema],
 });
 });
 
 
 
 
-schema.statics.upsertThreadRelation = async function(userId: string, threadId: string) {
+threadRelationSchema.statics.upsertThreadRelation = async function(userId: string, threadId: string) {
   const expirationDate = generateExpirationDate();
   const expirationDate = generateExpirationDate();
 
 
   await this.updateOne(
   await this.updateOne(
@@ -66,16 +88,9 @@ schema.statics.upsertThreadRelation = async function(userId: string, threadId: s
 };
 };
 
 
 
 
-schema.statics.getThreadRelationAndUpdateExpiration = async function(userId: string, threadId: string): Promise<ThreadDocument | null> {
-  const expirationDate = generateExpirationDate();
-
-  const result = await this.findOneAndUpdate(
-    { userId, 'threads.threadId': threadId },
-    { $set: { 'threads.$.expiredAt': expirationDate } }, // Extend DAYS_UNTIL_EXPIRATION days from the retrieved time
-    { new: true },
-  );
-
+threadRelationSchema.statics.getThreadRelation = async function(userId: string, threadId: string): Promise<ThreadRelationDocument | null> {
+  const result = await this.findOne({ userId, 'threads.threadId': threadId });
   return result;
   return result;
 };
 };
 
 
-export default getOrCreateModel<ThreadDocument, ThreadRelationModel>('ThreadRelation', schema);
+export default getOrCreateModel<ThreadRelationDocument, ThreadRelationModel>('ThreadRelation', threadRelationSchema);

+ 5 - 1
apps/app/src/features/openai/server/services/openai.ts

@@ -52,7 +52,7 @@ class OpenaiService implements IOpenaiService {
       return thread;
       return thread;
     }
     }
 
 
-    const threadRelation = await ThreadRelationModel.getThreadRelationAndUpdateExpiration(userId, threadId);
+    const threadRelation = await ThreadRelationModel.getThreadRelation(userId, threadId);
     const threadDocument = threadRelation?.threads.find(thread => thread.threadId === threadId);
     const threadDocument = threadRelation?.threads.find(thread => thread.threadId === threadId);
     if (threadDocument == null) {
     if (threadDocument == null) {
       return;
       return;
@@ -60,6 +60,10 @@ class OpenaiService implements IOpenaiService {
 
 
     // Check if a thread entity exists
     // Check if a thread entity exists
     const thread = await this.client.retrieveThread(threadDocument.threadId);
     const thread = await this.client.retrieveThread(threadDocument.threadId);
+
+    // Update expiration date if thread entity exists
+    await threadDocument.updateExpiration();
+
     return thread;
     return thread;
   }
   }