|
@@ -2,6 +2,7 @@ import assert from 'node:assert';
|
|
|
import { Readable, Transform } from 'stream';
|
|
import { Readable, Transform } from 'stream';
|
|
|
import { pipeline } from 'stream/promises';
|
|
import { pipeline } from 'stream/promises';
|
|
|
|
|
|
|
|
|
|
+import type { IPagePopulatedToShowRevision } from '@growi/core';
|
|
|
import { PageGrant, isPopulated } from '@growi/core';
|
|
import { PageGrant, isPopulated } from '@growi/core';
|
|
|
import type { HydratedDocument, Types } from 'mongoose';
|
|
import type { HydratedDocument, Types } from 'mongoose';
|
|
|
import mongoose from 'mongoose';
|
|
import mongoose from 'mongoose';
|
|
@@ -20,7 +21,7 @@ import { createBatchStream } from '~/server/util/batch-stream';
|
|
|
import loggerFactory from '~/utils/logger';
|
|
import loggerFactory from '~/utils/logger';
|
|
|
|
|
|
|
|
import { OpenaiServiceTypes } from '../../interfaces/ai';
|
|
import { OpenaiServiceTypes } from '../../interfaces/ai';
|
|
|
-import { sanitizeMarkdown } from '../utils/sanitize-markdown';
|
|
|
|
|
|
|
+import { convertMarkdownToHtml } from '../utils/convert-markdown-to-html';
|
|
|
|
|
|
|
|
import { getClient } from './client-delegator';
|
|
import { getClient } from './client-delegator';
|
|
|
// import { splitMarkdownIntoChunks } from './markdown-splitter/markdown-token-splitter';
|
|
// import { splitMarkdownIntoChunks } from './markdown-splitter/markdown-token-splitter';
|
|
@@ -157,9 +158,9 @@ class OpenaiService implements IOpenaiService {
|
|
|
// }
|
|
// }
|
|
|
// }
|
|
// }
|
|
|
|
|
|
|
|
- private async uploadFile(pageId: Types.ObjectId, body: string): Promise<OpenAI.Files.FileObject> {
|
|
|
|
|
- const sanitizedMarkdown = await sanitizeMarkdown(body);
|
|
|
|
|
- const file = await toFile(Readable.from(sanitizedMarkdown), `${pageId}.md`);
|
|
|
|
|
|
|
+ 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);
|
|
const uploadedFile = await this.client.uploadFile(file);
|
|
|
return uploadedFile;
|
|
return uploadedFile;
|
|
|
}
|
|
}
|
|
@@ -183,17 +184,17 @@ class OpenaiService implements IOpenaiService {
|
|
|
async createVectorStoreFile(pages: Array<HydratedDocument<PageDocument>>): Promise<void> {
|
|
async createVectorStoreFile(pages: Array<HydratedDocument<PageDocument>>): Promise<void> {
|
|
|
const vectorStore = await this.getOrCreateVectorStoreForPublicScope();
|
|
const vectorStore = await this.getOrCreateVectorStoreForPublicScope();
|
|
|
const vectorStoreFileRelationsMap: VectorStoreFileRelationsMap = new Map();
|
|
const vectorStoreFileRelationsMap: VectorStoreFileRelationsMap = new Map();
|
|
|
- const processUploadFile = async(page: 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 (isPopulated(page.revision) && page.revision.body.length > 0) {
|
|
|
- const uploadedFile = await this.uploadFile(page._id, page.revision.body);
|
|
|
|
|
|
|
+ const uploadedFile = await this.uploadFile(page._id, page.path, page.revision.body);
|
|
|
prepareVectorStoreFileRelations(vectorStore._id, page._id, uploadedFile.id, vectorStoreFileRelationsMap);
|
|
prepareVectorStoreFileRelations(vectorStore._id, page._id, uploadedFile.id, vectorStoreFileRelationsMap);
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
const pagePopulatedToShowRevision = await page.populateDataToShowRevision();
|
|
const pagePopulatedToShowRevision = await page.populateDataToShowRevision();
|
|
|
if (pagePopulatedToShowRevision.revision != null && pagePopulatedToShowRevision.revision.body.length > 0) {
|
|
if (pagePopulatedToShowRevision.revision != null && pagePopulatedToShowRevision.revision.body.length > 0) {
|
|
|
- const uploadedFile = await this.uploadFile(page._id, pagePopulatedToShowRevision.revision.body);
|
|
|
|
|
|
|
+ const uploadedFile = await this.uploadFile(page._id, page.path, pagePopulatedToShowRevision.revision.body);
|
|
|
prepareVectorStoreFileRelations(vectorStore._id, page._id, uploadedFile.id, vectorStoreFileRelationsMap);
|
|
prepareVectorStoreFileRelations(vectorStore._id, page._id, uploadedFile.id, vectorStoreFileRelationsMap);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|