Shun Miyazawa 1 год назад
Родитель
Сommit
c385c31bd3

+ 2 - 1
apps/app/src/features/openai/client/components/AiAssistant/AiAssistantSidebar/AiAssistantSidebar.tsx

@@ -46,7 +46,7 @@ type FormData = {
 };
 
 type AiAssistantSidebarSubstanceProps = {
-  isEditorAssistant?: boolean;
+  isEditorAssistant: boolean;
   aiAssistantData?: AiAssistantHasId;
   threadData?: IThreadRelationHasId;
   closeAiAssistantSidebar: () => void
@@ -160,6 +160,7 @@ const AiAssistantSidebarSubstance: React.FC<AiAssistantSidebarSubstanceProps> =
     if (currentThreadId_ == null) {
       try {
         const res = await apiv3Post<IThreadRelationHasId>('/openai/thread', {
+          isEditorAssistant,
           aiAssistantId: isEditorAssistant ? selectedAiAssistant?._id : aiAssistantData?._id,
           initialUserMessage: isEditorAssistant ? undefined : newUserMessage.content,
         });

+ 1 - 0
apps/app/src/features/openai/interfaces/thread-relation.ts

@@ -7,6 +7,7 @@ export interface IThreadRelation {
   aiAssistant: Ref<AiAssistant>
   threadId: string;
   title?: string;
+  isEditorAssistant: boolean;
   expiredAt: Date;
 }
 

+ 4 - 0
apps/app/src/features/openai/server/models/thread-relation.ts

@@ -37,6 +37,10 @@ const schema = new Schema<ThreadRelationDocument, ThreadRelationModel>({
   title: {
     type: String,
   },
+  isEditorAssistant: {
+    type: Boolean,
+    required: true,
+  },
   expiredAt: {
     type: Date,
     default: generateExpirationDate,

+ 3 - 2
apps/app/src/features/openai/server/routes/thread.ts

@@ -17,6 +17,7 @@ import { certifyAiService } from './middlewares/certify-ai-service';
 const logger = loggerFactory('growi:routes:apiv3:openai:thread');
 
 type ReqBody = {
+  isEditorAssistant: boolean,
   aiAssistantId?: string,
   initialUserMessage?: string,
 }
@@ -43,8 +44,8 @@ export const createThreadHandlersFactory: CreateThreadFactory = (crowi) => {
       }
 
       try {
-        const { aiAssistantId, initialUserMessage } = req.body;
-        const thread = await openaiService.createThread(req.user._id, aiAssistantId, initialUserMessage);
+        const { aiAssistantId, initialUserMessage, isEditorAssistant } = req.body;
+        const thread = await openaiService.createThread(req.user._id, isEditorAssistant, aiAssistantId, initialUserMessage);
         return res.apiv3(thread);
       }
       catch (err) {

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

@@ -65,7 +65,7 @@ const convertPathPatternsToRegExp = (pagePathPatterns: string[]): Array<string |
 };
 
 export interface IOpenaiService {
-  createThread(userId: string, aiAssistantId?: string, initialUserMessage?: string): Promise<ThreadRelationDocument>;
+  createThread(userId: string, isEditorAssistant: boolean, aiAssistantId?: string, initialUserMessage?: string): Promise<ThreadRelationDocument>;
   getThreadsByAiAssistantId(aiAssistantId: string): Promise<ThreadRelationDocument[]>
   deleteThread(threadRelationId: string): Promise<ThreadRelationDocument>;
   deleteExpiredThreads(limit: number, apiCallInterval: number): Promise<void>; // for CronJob
@@ -117,7 +117,7 @@ class OpenaiService implements IOpenaiService {
     return threadTitle;
   }
 
-  async createThread(userId: string, aiAssistantId?: string, initialUserMessage?: string): Promise<ThreadRelationDocument> {
+  async createThread(userId: string, isEditorAssistant: boolean, aiAssistantId?: string, initialUserMessage?: string): Promise<ThreadRelationDocument> {
     let threadTitle: string | null = null;
     if (initialUserMessage != null) {
       try {
@@ -133,6 +133,7 @@ class OpenaiService implements IOpenaiService {
       const thread = await this.client.createThread(vectorStoreRelation?.vectorStoreId);
       const threadRelation = await ThreadRelationModel.create({
         userId,
+        isEditorAssistant,
         aiAssistant: aiAssistantId,
         threadId: thread.id,
         title: threadTitle,
@@ -158,7 +159,7 @@ class OpenaiService implements IOpenaiService {
   }
 
   async getThreadsByAiAssistantId(aiAssistantId: string): Promise<ThreadRelationDocument[]> {
-    const threadRelations = await ThreadRelationModel.find({ aiAssistant: aiAssistantId });
+    const threadRelations = await ThreadRelationModel.find({ aiAssistant: aiAssistantId, isEditorAssistant: false });
     return threadRelations;
   }