Ryotaro Nagahara 1 месяц назад
Родитель
Сommit
2dfc84506b

+ 4 - 7
apps/app/src/features/openai/server/routes/get-threads.ts

@@ -12,8 +12,6 @@ import loginRequiredFactory from '~/server/middlewares/login-required';
 import type { ApiV3Response } from '~/server/routes/apiv3/interfaces/apiv3-response';
 import loggerFactory from '~/utils/logger';
 
-import { ThreadType } from '../../interfaces/thread-relation';
-import ThreadRelationModel from '../models/thread-relation';
 import { getOpenaiService } from '../services/openai';
 import { certifyAiService } from './middlewares/certify-ai-service';
 
@@ -72,11 +70,10 @@ export const getThreadsFactory = (crowi: Crowi): RequestHandler[] => {
           );
         }
 
-        const threads = await ThreadRelationModel.find({
-          aiAssistant: aiAssistantId,
-          userId: user._id,
-          type: ThreadType.KNOWLEDGE,
-        }).sort({ updatedAt: -1 });
+        const threads = await openaiService.getThreadsByAiAssistantId(
+          aiAssistantId,
+          user._id,
+        );
 
         return res.apiv3({ threads });
       } catch (err) {

+ 3 - 0
apps/app/src/features/openai/server/services/openai.ts

@@ -98,6 +98,7 @@ export interface IOpenaiService {
   ): Promise<ThreadRelationDocument>;
   getThreadsByAiAssistantId(
     aiAssistantId: string,
+    userId?: string,
   ): Promise<ThreadRelationDocument[]>;
   deleteThread(threadRelationId: string): Promise<ThreadRelationDocument>;
   deleteExpiredThreads(limit: number, apiCallInterval: number): Promise<void>; // for CronJob
@@ -290,10 +291,12 @@ class OpenaiService implements IOpenaiService {
 
   async getThreadsByAiAssistantId(
     aiAssistantId: string,
+    userId?: string,
     type: ThreadType = ThreadType.KNOWLEDGE,
   ): Promise<ThreadRelationDocument[]> {
     const threadRelations = await ThreadRelationModel.find({
       aiAssistant: aiAssistantId,
+      ...(userId != null && { userId }),
       type,
     }).sort({ updatedAt: -1 });
     return threadRelations;