Skip to content

Commit

Permalink
Refactor uploadFile and convertMarkdownToHtml to use simplified param…
Browse files Browse the repository at this point in the history
…eters
  • Loading branch information
miya committed Dec 23, 2024
1 parent bac5032 commit 1bebb75
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 22 deletions.
24 changes: 6 additions & 18 deletions apps/app/src/features/openai/server/services/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,6 @@ let isVectorStoreForPublicScopeExist = false;

type VectorStoreFileRelationsMap = Map<string, VectorStoreFileRelation>

// type guard
const isPagePopulatedToShowRevision = (page: HydratedDocument<PageDocument>): page is IPagePopulatedToShowRevision & PageDocument => {
if (page?.revision != null && !isPopulated(page.revision)) {
return false;
}

return true;
};
export interface IOpenaiService {
getOrCreateThread(userId: string, vectorStoreId?: string, threadId?: string): Promise<OpenAI.Beta.Threads.Thread | undefined>;
getOrCreateVectorStoreForPublicScope(): Promise<VectorStoreDocument>;
Expand Down Expand Up @@ -166,9 +158,9 @@ class OpenaiService implements IOpenaiService {
// }
// }

private async uploadFile(page: IPagePopulatedToShowRevision): Promise<OpenAI.Files.FileObject> {
const convertedHtml = await convertMarkdownToHtml(page);
const file = await toFile(Readable.from(convertedHtml), `${page._id}.html`);
private async uploadFile(pageId: Types.ObjectId, pagePath: string, revisionBody: string): Promise<OpenAI.Files.FileObject> {
const convertedHtml = await convertMarkdownToHtml({ pagePath, revisionBody });
const file = await toFile(Readable.from(convertedHtml), `${pageId}.html`);
const uploadedFile = await this.client.uploadFile(file);
return uploadedFile;
}
Expand All @@ -194,19 +186,15 @@ class OpenaiService implements IOpenaiService {
const vectorStoreFileRelationsMap: VectorStoreFileRelationsMap = new Map();
const processUploadFile = async(page: HydratedDocument<PageDocument>) => {
if (page._id != null && page.grant === PageGrant.GRANT_PUBLIC && page.revision != null) {
if (isPagePopulatedToShowRevision(page)) {
if (page.revision.body.length < 0) {
return;
}

const uploadedFile = await this.uploadFile(page);
if (isPopulated(page.revision) && page.revision.body.length > 0) {
const uploadedFile = await this.uploadFile(page._id, page.path, page.revision.body);
prepareVectorStoreFileRelations(vectorStore._id, page._id, uploadedFile.id, vectorStoreFileRelationsMap);
return;
}

const pagePopulatedToShowRevision = await page.populateDataToShowRevision();
if (pagePopulatedToShowRevision.revision != null && pagePopulatedToShowRevision.revision.body.length > 0) {
const uploadedFile = await this.uploadFile(pagePopulatedToShowRevision);
const uploadedFile = await this.uploadFile(page._id, page.path, pagePopulatedToShowRevision.revision.body);
prepareVectorStoreFileRelations(vectorStore._id, page._id, uploadedFile.id, vectorStoreFileRelationsMap);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { dynamicImport } from '@cspell/dynamic-import';
import type { IPagePopulatedToShowRevision } from '@growi/core/dist/interfaces';
import type { Root, Code } from 'mdast';
import type * as RehypeMeta from 'rehype-meta';
import type * as RehypeStringify from 'rehype-stringify';
Expand Down Expand Up @@ -56,7 +55,12 @@ const initializeModules = async(): Promise<void> => {
};
};

export const convertMarkdownToHtml = async(page: IPagePopulatedToShowRevision): Promise<string> => {
type ConvertMarkdownToHtmlParams = {
pagePath: string;
revisionBody: string;
};

export const convertMarkdownToHtml = async({ pagePath, revisionBody }: ConvertMarkdownToHtmlParams): Promise<string> => {
await initializeModules();

const {
Expand All @@ -82,9 +86,9 @@ export const convertMarkdownToHtml = async(page: IPagePopulatedToShowRevision):
.use(sanitizeMarkdown)
.use(remarkRehype)
.use(rehypeMeta, {
title: page.path,
title: pagePath,
})
.use(rehypeStringify);

return processor.processSync(page.revision?.body).toString();
return processor.processSync(revisionBody).toString();
};

0 comments on commit 1bebb75

Please sign in to comment.