|
|
@@ -65,11 +65,10 @@ const convertPathPatternsToRegExp = (pagePathPatterns: string[]): Array<string |
|
|
|
};
|
|
|
|
|
|
export interface IOpenaiService {
|
|
|
- getOrCreateThread(
|
|
|
- userId: string, vectorStoreRelation: VectorStoreDocument, threadId?: string, initialUserMessage?: string
|
|
|
+ createThread(
|
|
|
+ userId: string, vectorStoreRelation: VectorStoreDocument, initialUserMessage: string
|
|
|
): Promise<ThreadRelationDocument>;
|
|
|
getThreads(vectorStoreRelationId: string): Promise<ThreadRelationDocument[]>
|
|
|
- // getOrCreateVectorStoreForPublicScope(): Promise<VectorStoreDocument>;
|
|
|
deleteThread(threadRelationId: string): Promise<ThreadRelationDocument>;
|
|
|
deleteExpiredThreads(limit: number, apiCallInterval: number): Promise<void>; // for CronJob
|
|
|
deleteObsolatedVectorStoreRelations(): Promise<void> // for CronJob
|
|
|
@@ -83,8 +82,6 @@ export interface IOpenaiService {
|
|
|
deleteVectorStoreFile(vectorStoreRelationId: Types.ObjectId, pageId: Types.ObjectId): Promise<void>;
|
|
|
deleteVectorStoreFilesByPageIds(pageIds: Types.ObjectId[]): Promise<void>;
|
|
|
deleteObsoleteVectorStoreFile(limit: number, apiCallInterval: number): Promise<void>; // for CronJob
|
|
|
- // rebuildVectorStoreAll(): Promise<void>;
|
|
|
- // rebuildVectorStore(page: HydratedDocument<PageDocument>): Promise<void>;
|
|
|
isAiAssistantUsable(aiAssistantId: string, user: IUserHasId): Promise<boolean>;
|
|
|
createAiAssistant(data: Omit<AiAssistant, 'vectorStore'>): Promise<AiAssistantDocument>;
|
|
|
updateAiAssistant(aiAssistantId: string, data: Omit<AiAssistant, 'vectorStore'>): Promise<AiAssistantDocument>;
|
|
|
@@ -125,53 +122,29 @@ class OpenaiService implements IOpenaiService {
|
|
|
return threadTitle;
|
|
|
}
|
|
|
|
|
|
- async getOrCreateThread(
|
|
|
- userId: string, vectorStoreRelation: VectorStoreDocument, threadId?: string, initialUserMessage?: string,
|
|
|
- ): Promise<ThreadRelationDocument> {
|
|
|
- if (threadId == null) {
|
|
|
- let threadTitle: string | null = null;
|
|
|
- if (initialUserMessage != null) {
|
|
|
- try {
|
|
|
- threadTitle = await this.generateThreadTitle(initialUserMessage);
|
|
|
- }
|
|
|
- catch (err) {
|
|
|
- logger.error(err);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
+ async createThread(userId: string, vectorStoreRelation: VectorStoreDocument, initialUserMessage: string): Promise<ThreadRelationDocument> {
|
|
|
+ let threadTitle: string | null = null;
|
|
|
+ if (initialUserMessage != null) {
|
|
|
try {
|
|
|
- const thread = await this.client.createThread(vectorStoreRelation.vectorStoreId);
|
|
|
- const threadRelation = await ThreadRelationModel.create({
|
|
|
- userId,
|
|
|
- threadId: thread.id,
|
|
|
- vectorStore: vectorStoreRelation._id,
|
|
|
- title: threadTitle,
|
|
|
- });
|
|
|
- return threadRelation;
|
|
|
+ threadTitle = await this.generateThreadTitle(initialUserMessage);
|
|
|
}
|
|
|
catch (err) {
|
|
|
- throw new Error(err);
|
|
|
+ logger.error(err);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- const threadRelation = await ThreadRelationModel.findOne({ threadId });
|
|
|
- if (threadRelation == null) {
|
|
|
- throw new Error('ThreadRelation document is not exists');
|
|
|
- }
|
|
|
-
|
|
|
- // Check if a thread entity exists
|
|
|
- // If the thread entity does not exist, the thread-relation document is deleted
|
|
|
try {
|
|
|
- const thread = await this.client.retrieveThread(threadRelation.threadId);
|
|
|
-
|
|
|
- // Update expiration date if thread entity exists
|
|
|
- await threadRelation.updateThreadExpiration();
|
|
|
-
|
|
|
+ const thread = await this.client.createThread(vectorStoreRelation.vectorStoreId);
|
|
|
+ const threadRelation = await ThreadRelationModel.create({
|
|
|
+ userId,
|
|
|
+ threadId: thread.id,
|
|
|
+ vectorStore: vectorStoreRelation._id,
|
|
|
+ title: threadTitle,
|
|
|
+ });
|
|
|
return threadRelation;
|
|
|
}
|
|
|
catch (err) {
|
|
|
- await openaiApiErrorHandler(err, { notFoundError: async() => { await threadRelation.remove() } });
|
|
|
- throw new Error(err);
|
|
|
+ throw err;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -192,6 +165,7 @@ class OpenaiService implements IOpenaiService {
|
|
|
await threadRelation.remove();
|
|
|
}
|
|
|
catch (err) {
|
|
|
+ await openaiApiErrorHandler(err, { notFoundError: async() => { await threadRelation.remove() } });
|
|
|
throw err;
|
|
|
}
|
|
|
|