Sfoglia il codice sorgente

Impl addConditionToListByPathsArrayWithGlob

Shun Miyazawa 1 anno fa
parent
commit
e4002edb1f

+ 9 - 0
apps/app/src/features/openai/server/services/openai.ts

@@ -359,6 +359,15 @@ class OpenaiService implements IOpenaiService {
   }
 
   async createAiAssistant(data: Omit<AiAssistant, 'vectorStore'>): Promise<AiAssistantDocument> {
+
+    const Page = mongoose.model<HydratedDocument<PageDocument>, PageModel>('Page');
+    const { PageQueryBuilder } = Page;
+    const builder = new PageQueryBuilder(Page.find(), false); // includeEmpty = false
+
+    builder.addConditionToListByPathsArrayWithGlob(data.pagePathPatterns);
+
+    const pages = await builder.query.exec();
+
     const dumyVectorStoreId = '676e0d9863442b736e7ecf09';
     const aiAssistant = await AiAssistantModel.create({ ...data, vectorStore: dumyVectorStoreId });
     return aiAssistant;

+ 21 - 0
apps/app/src/server/models/page.ts

@@ -568,6 +568,27 @@ export class PageQueryBuilder {
     return this;
   }
 
+  addConditionToListByPathsArrayWithGlob(paths: string[]): PageQueryBuilder {
+    const conditions: Array<{ path: string | RegExp;}> = paths.map((path) => {
+      if (path.endsWith('/*')) {
+        const basePathWithoutGlob = path.slice(0, -2); // remove '/*'
+        const pathWithTrailingSlash = addTrailingSlash(basePathWithoutGlob);
+        const startsPattern = escapeStringRegexp(pathWithTrailingSlash);
+
+        return { path: new RegExp(`^${startsPattern}`) };
+      }
+
+      return { path: normalizePath(path) };
+    });
+
+    this.query = this.query
+      .and({
+        $or: conditions,
+      });
+
+    return this;
+  }
+
 }
 
 schema.statics.createEmptyPage = async function(