Explorar o código

Change return type of POST: /openai/thread from 'OpenAI.Beta.Threads.Thread' to 'ThreadRelationDocument'

Shun Miyazawa hai 1 ano
pai
achega
ce294e1354

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

@@ -57,7 +57,7 @@ export const createThreadHandlersFactory: CreateThreadFactory = (crowi) => {
         const vectorStoreRelation = await openaiService.getVectorStoreRelation(aiAssistantId);
 
         const thread = await openaiService.getOrCreateThread(req.user._id, vectorStoreRelation, filteredThreadId, initialUserMessage);
-        return res.apiv3({ thread });
+        return res.apiv3(thread);
       }
       catch (err) {
         logger.error(err);

+ 5 - 6
apps/app/src/features/openai/server/services/openai.ts

@@ -65,7 +65,7 @@ const convertPathPatternsToRegExp = (pagePathPatterns: string[]): Array<string |
 export interface IOpenaiService {
   getOrCreateThread(
     userId: string, vectorStoreRelation: VectorStoreDocument, threadId?: string, initialUserMessage?: string
-  ): Promise<OpenAI.Beta.Threads.Thread | undefined>;
+  ): Promise<ThreadRelationDocument>;
   getThreads(vectorStoreRelationId: string): Promise<ThreadRelationDocument[]>
   // getOrCreateVectorStoreForPublicScope(): Promise<VectorStoreDocument>;
   deleteExpiredThreads(limit: number, apiCallInterval: number): Promise<void>; // for CronJob
@@ -123,7 +123,7 @@ class OpenaiService implements IOpenaiService {
 
   async getOrCreateThread(
       userId: string, vectorStoreRelation: VectorStoreDocument, threadId?: string, initialUserMessage?: string,
-  ): Promise<OpenAI.Beta.Threads.Thread> {
+  ): Promise<ThreadRelationDocument> {
     if (threadId == null) {
       let threadTitle: string | null = null;
       if (initialUserMessage != null) {
@@ -137,14 +137,13 @@ class OpenaiService implements IOpenaiService {
 
       try {
         const thread = await this.client.createThread(vectorStoreRelation.vectorStoreId);
-        await ThreadRelationModel.create({
+        const threadRelation = await ThreadRelationModel.create({
           userId,
           threadId: thread.id,
           vectorStore: vectorStoreRelation._id,
           title: threadTitle,
         });
-
-        return thread;
+        return threadRelation;
       }
       catch (err) {
         throw new Error(err);
@@ -164,7 +163,7 @@ class OpenaiService implements IOpenaiService {
       // Update expiration date if thread entity exists
       await threadRelation.updateThreadExpiration();
 
-      return thread;
+      return threadRelation;
     }
     catch (err) {
       await openaiApiErrorHandler(err, { notFoundError: async() => { await threadRelation.remove() } });