Explorar o código

feat: 完成查询历史记录接口

huangzhichao hai 1 mes
pai
achega
516fe51513

+ 17 - 19
src/dto/agent-server.ts

@@ -1,31 +1,23 @@
 import { IsString, IsNotEmpty, ValidateIf } from 'class-validator'
 
-export class GetStaffProfileDto {
-  @IsString()
-  @IsNotEmpty({ message: 'staff_id不能为空' })
-  staff_id: string
-}
-
-export class ListUsersDto {
-  @ValidateIf((o) => !o.user_union_id)
+export class GetDialogueHistoryDto {
   @IsString()
-  user_name: string
+  @IsNotEmpty({ message: 'staffId不能为空' })
+  staffId: string
 
-  @ValidateIf((o) => !o.user_name)
   @IsString()
-  user_union_id: string
-}
+  @ValidateIf((o) => !o.unionId)
+  @IsNotEmpty({ message: 'userName不能为空' })
+  userName: string
 
-export class GetDialogueHistoryDto {
   @IsString()
-  @IsNotEmpty({ message: 'staff_id不能为空' })
-  staff_id: string
+  @ValidateIf((o) => !o.userName)
+  @IsNotEmpty({ message: 'unionId不能为空' })
+  unionId: string
 
   @IsString()
-  @IsNotEmpty({ message: 'user_id不能为空' })
-  user_id: string
-
-  recent_minutes: string
+  @IsNotEmpty({ message: 'recentMinutes不能为空' })
+  recentMinutes: string
 }
 
 export class RunPromptDto {
@@ -57,3 +49,9 @@ export class RunPromptDto {
   @IsNotEmpty({ message: 'current_timestamp不能为空' })
   current_timestamp: string
 }
+
+export class GetBasePromptDto {
+  @IsString()
+  @IsNotEmpty({ message: 'scene不能为空' })
+  scene: string
+}

+ 7 - 12
src/interface/agent-server.interface.ts

@@ -2,19 +2,14 @@ import { ServiceResponse } from '../response/response.interface'
 
 export interface IAgentServerService {
   listStaffs(): Promise<ServiceResponse<StaffsType[]>>
-  getStaffProfile(staff_id: string): Promise<ServiceResponse<StaffProfile>>
-  getUserProfile(user_id: string): Promise<ServiceResponse<UserProfile>>
-  listUsers(
-    user_name: string,
-    user_union_id: string
-  ): Promise<ServiceResponse<UserProfile[]>>
   getDialogueHistory(
-    staff_id: string,
-    user_id: string,
-    recent_minutes: string
-  ): Promise<ServiceResponse<DialogueHistory[]>>
+    staffId: string,
+    userName: string,
+    unionId: string,
+    recentMinutes: string
+  ): Promise<ServiceResponse<UserInfo[]>>
   listModels(): Promise<ServiceResponse<Model[]>>
   listScenes(): Promise<ServiceResponse<any[]>>
-  getBasePrompt(): Promise<ServiceResponse<string>>
-  runPrompt(prompt: any): Promise<ServiceResponse<any>>
+  getBasePrompt(scene: string): Promise<ServiceResponse<string>>
+  // runPrompt(prompt: any): Promise<ServiceResponse<any>>
 }

+ 24 - 42
src/module/agent-server/agent-server.controller.ts

@@ -1,7 +1,7 @@
 import { Controller, Get, Query } from '@nestjs/common'
 import { ApiOperation, ApiTags } from '@nestjs/swagger'
 
-import { ListUsersDto, RunPromptDto } from '@/dto/agent-server'
+import { GetDialogueHistoryDto, GetBasePromptDto } from '@/dto/agent-server'
 
 import { AgentServerService } from './service/agent-server.service'
 
@@ -16,62 +16,44 @@ export class AgentServerController {
     return this.agentServerService.listStaffs()
   }
 
-  @Get('staff/profile')
-  @ApiOperation({ summary: '获取客服信息' })
-  async getStaffProfile(@Query('staff_id') staff_id: string) {
-    return this.agentServerService.getStaffProfile(staff_id)
-  }
-
-  @Get('user/profile')
-  @ApiOperation({ summary: '获取用户信息' })
-  async getUserProfile(@Query('user_id') user_id: string) {
-    return this.agentServerService.getUserProfile(user_id)
-  }
-
-  @Get('users')
-  @ApiOperation({ summary: '获取用户列表' })
-  async listUsers(@Query() query: ListUsersDto) {
-    return this.agentServerService.listUsers(
-      query.user_name,
-      query.user_union_id
-    )
-  }
-
   @Get('dialogue/history')
   @ApiOperation({ summary: '获取对话历史' })
-  async getDialogueHistory(
-    @Query('staff_id') staff_id: string,
-    @Query('user_id') user_id: string,
-    @Query('recent_minutes') recent_minutes: string
-  ) {
+  async getDialogueHistory(@Query() query: GetDialogueHistoryDto) {
     return this.agentServerService.getDialogueHistory(
-      staff_id,
-      user_id,
-      recent_minutes
+      query.staffId,
+      query.userName,
+      query.unionId,
+      query.recentMinutes
     )
   }
 
+  @Get('scenes')
+  @ApiOperation({ summary: '获取场景列表' })
+  async listScenes() {
+    return this.agentServerService.listScenes()
+  }
+
   @Get('models')
   @ApiOperation({ summary: '获取模型列表' })
   async listModels() {
     return this.agentServerService.listModels()
   }
 
-  @Get('scenes')
-  @ApiOperation({ summary: '获取场景列表' })
-  async listScenes() {
-    return this.agentServerService.listScenes()
-  }
+  // @Get('user/profile')
+  // @ApiOperation({ summary: '获取用户信息' })
+  // async getUserProfile(@Query('user_id') user_id: string) {
+  //   return this.agentServerService.getUserProfile(user_id)
+  // }
 
   @Get('base-prompt')
   @ApiOperation({ summary: '获取基础提示词' })
-  async getBasePrompt() {
-    return this.agentServerService.getBasePrompt()
+  async getBasePrompt(@Query() query: GetBasePromptDto) {
+    return this.agentServerService.getBasePrompt(query.scene)
   }
 
-  @Get('run-prompt')
-  @ApiOperation({ summary: '运行提示词' })
-  async runPrompt(@Query() query: RunPromptDto) {
-    return this.agentServerService.runPrompt(query)
-  }
+  // @Get('run-prompt')
+  // @ApiOperation({ summary: '运行提示词' })
+  // async runPrompt(@Query() query: RunPromptDto) {
+  //   return this.agentServerService.runPrompt(query)
+  // }
 }

+ 1 - 7
src/module/agent-server/agent-server.module.ts

@@ -13,12 +13,6 @@ import { AgentServerService } from './service/agent-server.service'
     })
   ],
   controllers: [AgentServerController],
-  providers: [
-    {
-      provide: 'IAgentServerService',
-      useClass: AgentServerHttpService
-    },
-    AgentServerService
-  ]
+  providers: [AgentServerHttpService, AgentServerService]
 })
 export class AgentServerModule {}

+ 3 - 4
src/module/agent-server/service/agent-server-http.service.ts

@@ -2,12 +2,11 @@ import { HttpService } from '@nestjs/axios'
 import { Injectable } from '@nestjs/common'
 import { lastValueFrom } from 'rxjs'
 
-import { IAgentServerService } from '@/interface/agent-server.interface'
 import { ServiceResponse } from '@/response/response.interface'
 import { HttpStatusCode } from '@/response/status-code.enum'
 
 @Injectable()
-export class AgentServerHttpService implements IAgentServerService {
+export class AgentServerHttpService {
   private readonly baseUrl = 'http://192.168.206.189:8083/api'
 
   constructor(private readonly httpService: HttpService) {}
@@ -77,8 +76,8 @@ export class AgentServerHttpService implements IAgentServerService {
     return this.makeRequest<any[]>('listScenes')
   }
 
-  async getBasePrompt(): Promise<ServiceResponse<string>> {
-    return this.makeRequest<string>('getBasePrompt')
+  async getBasePrompt(scene: string): Promise<ServiceResponse<string>> {
+    return this.makeRequest<string>('getBasePrompt', { scene })
   }
 
   async runPrompt(prompt: any): Promise<ServiceResponse<any>> {

+ 96 - 54
src/module/agent-server/service/agent-server.service.ts

@@ -1,97 +1,139 @@
-import { Inject, Injectable } from '@nestjs/common'
+import { Injectable } from '@nestjs/common'
 
 import { IAgentServerService } from '@/interface/agent-server.interface'
 import { ServiceResponse } from '@/response/response.interface'
-import { HttpStatusCode } from '@/response/status-code.enum'
+import { BusinessCode, HttpStatusCode } from '@/response/status-code.enum'
+
+import { AgentServerHttpService } from './agent-server-http.service'
 
 @Injectable()
 export class AgentServerService implements IAgentServerService {
-  constructor(
-    @Inject('IAgentServerService')
-    private readonly httpService: IAgentServerService
-  ) {}
+  constructor(private readonly httpService: AgentServerHttpService) {}
 
   async listStaffs(): Promise<ServiceResponse<StaffsType[]>> {
-    return this.httpService.listStaffs()
+    const res = await this.httpService.listStaffs()
+    if (res.code !== HttpStatusCode.OK) {
+      return {
+        code: BusinessCode.BAD_REQUEST,
+        msg: '获取客服列表失败',
+        data: null
+      }
+    }
+    return {
+      code: BusinessCode.SUCCESS,
+      msg: '获取客服列表成功',
+      data: res.data
+    }
   }
 
-  async getStaffProfile(
-    staff_id: string
-  ): Promise<ServiceResponse<StaffProfile>> {
-    if (!staff_id) {
+  async listScenes(): Promise<ServiceResponse<Scene[]>> {
+    const res = await this.httpService.listScenes()
+    if (res.code !== HttpStatusCode.OK) {
       return {
-        code: HttpStatusCode.BAD_REQUEST,
-        msg: 'staff_id不能为空',
+        code: BusinessCode.BAD_REQUEST,
+        msg: '获取场景列表失败',
         data: null
       }
     }
-    return this.httpService.getStaffProfile(staff_id)
+    return {
+      code: BusinessCode.SUCCESS,
+      msg: '获取场景列表成功',
+      data: res.data
+    }
   }
 
-  async getUserProfile(user_id: string): Promise<ServiceResponse<UserProfile>> {
-    if (!user_id) {
+  async listModels(): Promise<ServiceResponse<Model[]>> {
+    const res = await this.httpService.listModels()
+    if (res.code !== HttpStatusCode.OK) {
       return {
-        code: HttpStatusCode.BAD_REQUEST,
-        msg: 'user_id不能为空',
+        code: BusinessCode.BAD_REQUEST,
+        msg: '获取模型列表失败',
         data: null
       }
     }
-    return this.httpService.getUserProfile(user_id)
+    return {
+      code: BusinessCode.SUCCESS,
+      msg: '获取模型列表成功',
+      data: res.data
+    }
   }
 
-  async listUsers(
-    user_name: string,
-    user_union_id: string
-  ): Promise<ServiceResponse<UserProfile[]>> {
-    if (!user_name && !user_union_id) {
+  async getBasePrompt(scene: string): Promise<ServiceResponse<string>> {
+    const res = await this.httpService.getBasePrompt(scene)
+    if (res.code !== HttpStatusCode.OK) {
       return {
-        code: HttpStatusCode.BAD_REQUEST,
-        msg: 'user_name和user_union_id不能同时为空',
+        code: BusinessCode.BAD_REQUEST,
+        msg: '获取基础提示词失败',
         data: null
       }
     }
-    return this.httpService.listUsers(user_name, user_union_id)
+    return {
+      code: BusinessCode.SUCCESS,
+      msg: '获取基础提示词成功',
+      data: res.data
+    }
   }
 
   async getDialogueHistory(
-    staff_id: string,
-    user_id: string,
-    recent_minutes: string
-  ): Promise<ServiceResponse<DialogueHistory[]>> {
-    if (!staff_id || !user_id) {
+    staffId: string,
+    userName: string,
+    unionId: string,
+    recentMinutes: string
+  ): Promise<ServiceResponse<UserInfo[]>> {
+    const user = await this.httpServiceListUsers(userName, unionId)
+    if (user.length === 0) {
       return {
-        code: HttpStatusCode.BAD_REQUEST,
-        msg: 'staff_id和user_id不能为空',
+        code: BusinessCode.BAD_REQUEST,
+        msg: '用户不存在',
         data: null
       }
     }
-    return this.httpService.getDialogueHistory(
-      staff_id,
-      user_id,
-      recent_minutes
+
+    const dialogueHistory = await Promise.all(
+      user.map(async (user) => {
+        const res = await this.getDialogueHistoryByUserId(
+          staffId,
+          user,
+          recentMinutes
+        )
+        return res
+      })
     )
-  }
 
-  async listModels(): Promise<ServiceResponse<Model[]>> {
-    return this.httpService.listModels()
+    return {
+      code: BusinessCode.SUCCESS,
+      msg: '获取对话历史成功',
+      data: dialogueHistory
+    }
   }
 
-  async listScenes(): Promise<ServiceResponse<any[]>> {
-    return this.httpService.listScenes()
+  async httpServiceListUsers(
+    userName: string,
+    unionId: string
+  ): Promise<UserProfile[]> {
+    const res = await this.httpService.listUsers(userName, unionId)
+    if (res.code !== HttpStatusCode.OK) {
+      return []
+    }
+    return res.data
   }
 
-  async getBasePrompt(): Promise<ServiceResponse<string>> {
-    return this.httpService.getBasePrompt()
-  }
+  async getDialogueHistoryByUserId(
+    staffId: string,
+    user: UserProfile,
+    recentMinutes: string
+  ): Promise<UserInfo> {
+    const res = await this.httpService.getDialogueHistory(
+      staffId,
+      user.third_party_user_id,
+      recentMinutes
+    )
 
-  async runPrompt(prompt: any): Promise<ServiceResponse<any>> {
-    if (!prompt) {
-      return {
-        code: HttpStatusCode.BAD_REQUEST,
-        msg: 'prompt不能为空',
-        data: null
-      }
+    const userInfo: UserInfo = {
+      ...user,
+      history: res.data || []
     }
-    return this.httpService.runPrompt(prompt)
+
+    return userInfo
   }
 }

+ 8 - 1
src/module/agent-server/type.d.ts

@@ -17,8 +17,11 @@ type StaffProfile = {
 }
 
 type UserProfile = {
-  user_id: string
+  gender: number
+  iconurl: string
   name: string
+  third_party_user_id: string
+  wxid: string
 }
 
 type DialogueHistory = {
@@ -42,3 +45,7 @@ type BasePrompt = {
   model_name: string
   content: string
 }
+
+interface UserInfo extends UserProfile {
+  history: DialogueHistory[]
+}