|
@@ -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 { Lang } from '@growi/core';
|
|
|
import {
|
|
import {
|
|
|
PageGrant, getIdForRef, getIdStringForRef, isPopulated, type IUserHasId,
|
|
PageGrant, getIdForRef, getIdStringForRef, isPopulated, type IUserHasId,
|
|
|
} from '@growi/core';
|
|
} from '@growi/core';
|
|
@@ -35,6 +36,7 @@ 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';
|
|
|
import { openaiApiErrorHandler } from './openai-api-error-handler';
|
|
import { openaiApiErrorHandler } from './openai-api-error-handler';
|
|
|
|
|
+import { replaceAnnotationWithPageLink } from './replace-annotation-with-page-link';
|
|
|
|
|
|
|
|
const { isDeepEquals } = deepEquals;
|
|
const { isDeepEquals } = deepEquals;
|
|
|
|
|
|
|
@@ -66,6 +68,9 @@ export interface IOpenaiService {
|
|
|
// getOrCreateVectorStoreForPublicScope(): Promise<VectorStoreDocument>;
|
|
// getOrCreateVectorStoreForPublicScope(): Promise<VectorStoreDocument>;
|
|
|
deleteExpiredThreads(limit: number, apiCallInterval: number): Promise<void>; // for CronJob
|
|
deleteExpiredThreads(limit: number, apiCallInterval: number): Promise<void>; // for CronJob
|
|
|
deleteObsolatedVectorStoreRelations(): Promise<void> // for CronJob
|
|
deleteObsolatedVectorStoreRelations(): Promise<void> // for CronJob
|
|
|
|
|
+ getMessageData(
|
|
|
|
|
+ threadId: string, lang?: Lang, options?: { before?: string, after?: string, limit?: number }
|
|
|
|
|
+ ): Promise<OpenAI.Beta.Threads.Messages.MessagesPage>;
|
|
|
getVectorStoreRelation(aiAssistantId: string): Promise<VectorStoreDocument>
|
|
getVectorStoreRelation(aiAssistantId: string): Promise<VectorStoreDocument>
|
|
|
getVectorStoreRelationsByPageIds(pageId: Types.ObjectId[]): Promise<VectorStoreDocument[]>;
|
|
getVectorStoreRelationsByPageIds(pageId: Types.ObjectId[]): Promise<VectorStoreDocument[]>;
|
|
|
createVectorStoreFile(vectorStoreRelation: VectorStoreDocument, pages: PageDocument[]): Promise<void>;
|
|
createVectorStoreFile(vectorStoreRelation: VectorStoreDocument, pages: PageDocument[]): Promise<void>;
|
|
@@ -150,6 +155,22 @@ class OpenaiService implements IOpenaiService {
|
|
|
await ThreadRelationModel.deleteMany({ threadId: { $in: deletedThreadIds } });
|
|
await ThreadRelationModel.deleteMany({ threadId: { $in: deletedThreadIds } });
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ async getMessageData(
|
|
|
|
|
+ threadId: string, lang?: Lang, options?: { limit: number, before: string, after: string },
|
|
|
|
|
+ ): Promise<OpenAI.Beta.Threads.Messages.MessagesPage> {
|
|
|
|
|
+ const messages = await this.client.getMessages(threadId, options);
|
|
|
|
|
+
|
|
|
|
|
+ for await (const message of messages.data) {
|
|
|
|
|
+ for await (const content of message.content) {
|
|
|
|
|
+ if (content.type === 'text') {
|
|
|
|
|
+ await replaceAnnotationWithPageLink(content, lang);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return messages;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
// TODO: https://redmine.weseek.co.jp/issues/160332
|
|
// TODO: https://redmine.weseek.co.jp/issues/160332
|
|
|
// public async getOrCreateVectorStoreForPublicScope(): Promise<VectorStoreDocument> {
|
|
// public async getOrCreateVectorStoreForPublicScope(): Promise<VectorStoreDocument> {
|
|
|
// const vectorStoreDocument: VectorStoreDocument | null = await VectorStoreModel.findOne({ scopeType: VectorStoreScopeType.PUBLIC, isDeleted: false });
|
|
// const vectorStoreDocument: VectorStoreDocument | null = await VectorStoreModel.findOne({ scopeType: VectorStoreScopeType.PUBLIC, isDeleted: false });
|