Browse Source

Impl updateAiAssistant

Shun Miyazawa 1 year ago
parent
commit
aa00de423a

+ 2 - 0
apps/app/src/features/openai/interfaces/ai-assistant.ts

@@ -39,6 +39,8 @@ export interface AiAssistant {
 
 export type IApiv3AiAssistantCreateParams = Omit<AiAssistant, 'owner' | 'vectorStore'>
 
+export type AiAssistantUpdateData = Partial<Omit<AiAssistant, 'owner' | 'vectorStore'>>
+
 export type AccessibleAiAssistants = {
   myAiAssistants: AiAssistant[],
   teamAiAssistants: AiAssistant[],

+ 6 - 2
apps/app/src/features/openai/server/routes/update-ai-assistant.ts

@@ -10,6 +10,7 @@ import { apiV3FormValidator } from '~/server/middlewares/apiv3-form-validator';
 import type { ApiV3Response } from '~/server/routes/apiv3/interfaces/apiv3-response';
 import loggerFactory from '~/utils/logger';
 
+import { type AiAssistantUpdateData } from '../../interfaces/ai-assistant';
 import { getOpenaiService } from '../services/openai';
 
 import { certifyAiService } from './middlewares/certify-ai-service';
@@ -23,7 +24,9 @@ type ReqParams = {
   id: string,
 }
 
-type Req = Request<ReqParams, Response, undefined> & {
+type ReqBody = AiAssistantUpdateData;
+
+type Req = Request<ReqParams, Response, ReqBody> & {
   user: IUserHasId,
 }
 
@@ -42,8 +45,9 @@ export const updateAiAssistantsFactory: UpdateAiAssistantsFactory = (crowi) => {
 
       try {
         const openaiService = getOpenaiService();
+        const updatedAiAssistant = await openaiService?.updateAiAssistant(id, user._id, {});
 
-        return res.apiv3({ });
+        return res.apiv3({ updatedAiAssistant });
       }
       catch (err) {
         logger.error(err);

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

@@ -24,6 +24,7 @@ import { createBatchStream } from '~/server/util/batch-stream';
 import loggerFactory from '~/utils/logger';
 
 import { OpenaiServiceTypes } from '../../interfaces/ai';
+import type { AiAssistantUpdateData } from '../../interfaces/ai-assistant';
 import {
   type AccessibleAiAssistants, type AiAssistant, AiAssistantAccessScope, AiAssistantShareScope,
 } from '../../interfaces/ai-assistant';
@@ -68,6 +69,7 @@ export interface IOpenaiService {
   // rebuildVectorStoreAll(): Promise<void>;
   // rebuildVectorStore(page: HydratedDocument<PageDocument>): Promise<void>;
   createAiAssistant(data: Omit<AiAssistant, 'vectorStore'>): Promise<AiAssistantDocument>;
+  updateAiAssistant(ownerId: string, aiAssistantId: string, data: AiAssistantUpdateData): Promise<AiAssistantDocument>;
   getAccessibleAiAssistants(user: IUserHasId): Promise<AccessibleAiAssistants>
   deleteAiAssistant(ownerId: string, aiAssistantId: string): Promise<AiAssistantDocument>
 }
@@ -605,6 +607,17 @@ class OpenaiService implements IOpenaiService {
     };
   }
 
+  async updateAiAssistant(ownerId: string, aiAssistantId: string, data: AiAssistantUpdateData): Promise<AiAssistantDocument> {
+    const aiAssistant = await AiAssistantModel.findOne({ owner: ownerId, _id: aiAssistantId });
+    if (aiAssistant == null) {
+      throw new Error('AiAssistant document does not exist');
+    }
+
+    // Implement the logic to update AiAssistant
+
+    return aiAssistant;
+  }
+
   async deleteAiAssistant(ownerId: string, aiAssistantId: string): Promise<AiAssistantDocument> {
     const aiAssistant = await AiAssistantModel.findOne({ owner: ownerId, _id: aiAssistantId });
     if (aiAssistant == null) {