Răsfoiți Sursa

Simplify cvertMarkdownToHtml argument types

Shun Miyazawa 1 an în urmă
părinte
comite
c7068fbde9

+ 13 - 2
apps/app/src/features/openai/server/services/openai.ts

@@ -36,6 +36,13 @@ let isVectorStoreForPublicScopeExist = false;
 
 
 type VectorStoreFileRelationsMap = Map<string, VectorStoreFileRelation>
 type VectorStoreFileRelationsMap = Map<string, VectorStoreFileRelation>
 
 
+const isPagePopulatedToShowRevision = (page: HydratedDocument<PageDocument>): page is IPagePopulatedToShowRevision & PageDocument => {
+  if (page?.revision != null && !isPopulated(page.revision)) {
+    return false;
+  }
+
+  return true;
+};
 export interface IOpenaiService {
 export interface IOpenaiService {
   getOrCreateThread(userId: string, vectorStoreId?: string, threadId?: string): Promise<OpenAI.Beta.Threads.Thread | undefined>;
   getOrCreateThread(userId: string, vectorStoreId?: string, threadId?: string): Promise<OpenAI.Beta.Threads.Thread | undefined>;
   getOrCreateVectorStoreForPublicScope(): Promise<VectorStoreDocument>;
   getOrCreateVectorStoreForPublicScope(): Promise<VectorStoreDocument>;
@@ -158,7 +165,7 @@ class OpenaiService implements IOpenaiService {
   //   }
   //   }
   // }
   // }
 
 
-  private async uploadFile(page: HydratedDocument<PageDocument> | IPagePopulatedToShowRevision): Promise<OpenAI.Files.FileObject> {
+  private async uploadFile(page: IPagePopulatedToShowRevision): Promise<OpenAI.Files.FileObject> {
     const convertedHtml = await convertMarkdownToHtml(page);
     const convertedHtml = await convertMarkdownToHtml(page);
     const file = await toFile(Readable.from(convertedHtml), `${page._id}.html`);
     const file = await toFile(Readable.from(convertedHtml), `${page._id}.html`);
     const uploadedFile = await this.client.uploadFile(file);
     const uploadedFile = await this.client.uploadFile(file);
@@ -186,7 +193,11 @@ class OpenaiService implements IOpenaiService {
     const vectorStoreFileRelationsMap: VectorStoreFileRelationsMap = new Map();
     const vectorStoreFileRelationsMap: VectorStoreFileRelationsMap = new Map();
     const processUploadFile = async(page: HydratedDocument<PageDocument>) => {
     const processUploadFile = async(page: HydratedDocument<PageDocument>) => {
       if (page._id != null && page.grant === PageGrant.GRANT_PUBLIC && page.revision != null) {
       if (page._id != null && page.grant === PageGrant.GRANT_PUBLIC && page.revision != null) {
-        if (isPopulated(page.revision) && page.revision.body.length > 0) {
+        if (isPagePopulatedToShowRevision(page)) {
+          if (page.revision.body.length > 0) {
+            return;
+          }
+
           const uploadedFile = await this.uploadFile(page);
           const uploadedFile = await this.uploadFile(page);
           prepareVectorStoreFileRelations(vectorStore._id, page._id, uploadedFile.id, vectorStoreFileRelationsMap);
           prepareVectorStoreFileRelations(vectorStore._id, page._id, uploadedFile.id, vectorStoreFileRelationsMap);
           return;
           return;

+ 2 - 10
apps/app/src/features/openai/server/utils/convert-markdown-to-html.ts

@@ -1,8 +1,6 @@
 import { dynamicImport } from '@cspell/dynamic-import';
 import { dynamicImport } from '@cspell/dynamic-import';
-import { isPopulated } from '@growi/core';
 import type { IPagePopulatedToShowRevision } from '@growi/core/dist/interfaces';
 import type { IPagePopulatedToShowRevision } from '@growi/core/dist/interfaces';
 import type { Root, Code } from 'mdast';
 import type { Root, Code } from 'mdast';
-import type { HydratedDocument } from 'mongoose';
 import type * as RehypeMeta from 'rehype-meta';
 import type * as RehypeMeta from 'rehype-meta';
 import type * as RehypeStringify from 'rehype-stringify';
 import type * as RehypeStringify from 'rehype-stringify';
 import type * as RemarkParse from 'remark-parse';
 import type * as RemarkParse from 'remark-parse';
@@ -10,8 +8,6 @@ import type * as RemarkRehype from 'remark-rehype';
 import type * as Unified from 'unified';
 import type * as Unified from 'unified';
 import type * as UnistUtilVisit from 'unist-util-visit';
 import type * as UnistUtilVisit from 'unist-util-visit';
 
 
-import type { PageDocument } from '~/server/models/page';
-
 interface ModuleCache {
 interface ModuleCache {
   unified?: typeof Unified.unified;
   unified?: typeof Unified.unified;
   visit?: typeof UnistUtilVisit.visit;
   visit?: typeof UnistUtilVisit.visit;
@@ -60,7 +56,7 @@ const initializeModules = async(): Promise<void> => {
   };
   };
 };
 };
 
 
-export const convertMarkdownToHtml = async(page: HydratedDocument<PageDocument> | IPagePopulatedToShowRevision): Promise<string> => {
+export const convertMarkdownToHtml = async(page: IPagePopulatedToShowRevision): Promise<string> => {
   await initializeModules();
   await initializeModules();
 
 
   const {
   const {
@@ -81,10 +77,6 @@ export const convertMarkdownToHtml = async(page: HydratedDocument<PageDocument>
     };
     };
   };
   };
 
 
-  const revisionBody = page.revision != null && isPopulated(page.revision)
-    ? page.revision.body
-    : undefined;
-
   const processor = unified()
   const processor = unified()
     .use(remarkParse)
     .use(remarkParse)
     .use(sanitizeMarkdown)
     .use(sanitizeMarkdown)
@@ -94,5 +86,5 @@ export const convertMarkdownToHtml = async(page: HydratedDocument<PageDocument>
     })
     })
     .use(rehypeStringify);
     .use(rehypeStringify);
 
 
-  return processor.processSync(revisionBody).toString();
+  return processor.processSync(page.revision?.body).toString();
 };
 };