Bläddra i källkod

feat: 完成开发

huangzhichao 1 månad sedan
förälder
incheckning
47cfa8e9d5

+ 39 - 20
src/dto/agent-server.ts

@@ -1,4 +1,11 @@
-import { IsString, IsNotEmpty, ValidateIf } from 'class-validator'
+import {
+  IsString,
+  IsNotEmpty,
+  ValidateIf,
+  IsArray,
+  IsNumber,
+  IsObject
+} from 'class-validator'
 
 export class GetDialogueHistoryDto {
   @IsString()
@@ -20,38 +27,50 @@ export class GetDialogueHistoryDto {
   recentMinutes: string
 }
 
-export class RunPromptDto {
+export class GetBasePromptDto {
   @IsString()
   @IsNotEmpty({ message: 'scene不能为空' })
   scene: string
+}
 
+export class GetUserProfileDto {
   @IsString()
-  @IsNotEmpty({ message: 'prompt不能为空' })
-  prompt: string
+  @IsNotEmpty({ message: 'user_id不能为空' })
+  userId: string
+}
 
+export class GetStaffProfileDto {
   @IsString()
-  @IsNotEmpty({ message: 'staff_profile不能为空' })
-  staff_profile: string
+  @IsNotEmpty({ message: 'staff_id不能为空' })
+  staffId: string
+}
 
+export class RunPromptDto {
   @IsString()
-  @IsNotEmpty({ message: 'user_profile不能为空' })
-  user_profile: string
+  @IsNotEmpty({ message: 'scene不能为空' })
+  scene: string
 
   @IsString()
-  @IsNotEmpty({ message: 'dialogue_history不能为空' })
-  dialogue_history: string
+  @IsNotEmpty({ message: 'prompt不能为空' })
+  prompt: string
 
-  @IsString()
-  @IsNotEmpty({ message: 'model_name不能为空' })
-  model_name: string
+  @IsObject()
+  @IsNotEmpty({ message: 'staffProfile不能为空' })
+  staffProfile: StaffProfile
 
-  @IsString()
-  @IsNotEmpty({ message: 'current_timestamp不能为空' })
-  current_timestamp: string
-}
+  @IsObject()
+  @IsNotEmpty({ message: 'userProfile不能为空' })
+  userProfile: UserProfile
+
+  @IsArray()
+  @IsNotEmpty({ message: 'dialogueHistory不能为空' })
+  dialogueHistory: DialogueHistory
 
-export class GetBasePromptDto {
   @IsString()
-  @IsNotEmpty({ message: 'scene不能为空' })
-  scene: string
+  @IsNotEmpty({ message: 'modelName不能为空' })
+  modelName: string
+
+  @IsNumber()
+  @IsNotEmpty({ message: 'currentTimestamp不能为空' })
+  currentTimestamp: number
 }

+ 25 - 12
src/module/agent-server/agent-server.controller.ts

@@ -1,7 +1,13 @@
-import { Controller, Get, Query } from '@nestjs/common'
+import { Controller, Get, Query, Post, Body, Header } from '@nestjs/common'
 import { ApiOperation, ApiTags } from '@nestjs/swagger'
 
-import { GetDialogueHistoryDto, GetBasePromptDto } from '@/dto/agent-server'
+import {
+  GetDialogueHistoryDto,
+  GetBasePromptDto,
+  GetUserProfileDto,
+  GetStaffProfileDto,
+  RunPromptDto
+} from '@/dto/agent-server'
 
 import { AgentServerService } from './service/agent-server.service'
 
@@ -39,11 +45,17 @@ export class AgentServerController {
     return this.agentServerService.listModels()
   }
 
-  // @Get('user/profile')
-  // @ApiOperation({ summary: '获取用户信息' })
-  // async getUserProfile(@Query('user_id') user_id: string) {
-  //   return this.agentServerService.getUserProfile(user_id)
-  // }
+  @Get('user/profile')
+  @ApiOperation({ summary: '获取用户信息' })
+  async getUserProfile(@Query() query: GetUserProfileDto) {
+    return this.agentServerService.getUserProfile(query.userId)
+  }
+
+  @Get('staff/profile')
+  @ApiOperation({ summary: '获取客服信息' })
+  async getStaffProfile(@Query() query: GetStaffProfileDto) {
+    return this.agentServerService.getStaffProfile(query.staffId)
+  }
 
   @Get('base-prompt')
   @ApiOperation({ summary: '获取基础提示词' })
@@ -51,9 +63,10 @@ export class AgentServerController {
     return this.agentServerService.getBasePrompt(query.scene)
   }
 
-  // @Get('run-prompt')
-  // @ApiOperation({ summary: '运行提示词' })
-  // async runPrompt(@Query() query: RunPromptDto) {
-  //   return this.agentServerService.runPrompt(query)
-  // }
+  @Post('run-prompt')
+  @Header('Content-Type', 'application/json')
+  @ApiOperation({ summary: '运行提示词' })
+  async runPrompt(@Body() body: RunPromptDto) {
+    return this.agentServerService.runPrompt(body)
+  }
 }

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

@@ -8,7 +8,7 @@ import { AgentServerService } from './service/agent-server.service'
 @Module({
   imports: [
     HttpModule.register({
-      timeout: 1000,
+      timeout: 600000,
       maxRedirects: 3
     })
   ],

+ 26 - 2
src/module/agent-server/service/agent-server-http.service.ts

@@ -32,6 +32,30 @@ export class AgentServerHttpService {
     }
   }
 
+  async makeRequestPost<T>(
+    endpoint: string,
+    params?: Record<string, any>
+  ): Promise<ServiceResponse<T>> {
+    try {
+      const url = `${this.baseUrl}/${endpoint}`
+      const responseObservable = await this.httpService.post<
+        ServiceResponse<T>
+      >(url, params, {
+        headers: {
+          'Content-Type': 'application/json'
+        }
+      })
+      const { data } = await lastValueFrom(responseObservable)
+      return data
+    } catch (error) {
+      return {
+        code: HttpStatusCode.INTERNAL_SERVER_ERROR,
+        msg: error.message,
+        data: null as T
+      }
+    }
+  }
+
   async listStaffs(): Promise<ServiceResponse<StaffsType[]>> {
     return this.makeRequest<StaffsType[]>('listStaffs')
   }
@@ -80,7 +104,7 @@ export class AgentServerHttpService {
     return this.makeRequest<string>('getBasePrompt', { scene })
   }
 
-  async runPrompt(prompt: any): Promise<ServiceResponse<any>> {
-    return this.makeRequest<any>('runPrompt', prompt)
+  async runPrompt(params: any): Promise<ServiceResponse<any>> {
+    return this.makeRequestPost<any>('runPrompt', params)
   }
 }

+ 60 - 0
src/module/agent-server/service/agent-server.service.ts

@@ -1,5 +1,6 @@
 import { Injectable } from '@nestjs/common'
 
+import { RunPromptDto } from '@/dto/agent-server'
 import { IAgentServerService } from '@/interface/agent-server.interface'
 import { ServiceResponse } from '@/response/response.interface'
 import { BusinessCode, HttpStatusCode } from '@/response/status-code.enum'
@@ -136,4 +137,63 @@ export class AgentServerService implements IAgentServerService {
 
     return userInfo
   }
+
+  async getUserProfile(userId: string): Promise<ServiceResponse<UserProfile>> {
+    const res = await this.httpService.getUserProfile(userId)
+    if (res.code !== HttpStatusCode.OK) {
+      return {
+        code: BusinessCode.BAD_REQUEST,
+        msg: '获取用户信息失败',
+        data: null
+      }
+    }
+    return {
+      code: BusinessCode.SUCCESS,
+      msg: '获取用户信息成功',
+      data: res.data
+    }
+  }
+
+  async getStaffProfile(
+    staffId: string
+  ): Promise<ServiceResponse<StaffProfile>> {
+    const res = await this.httpService.getStaffProfile(staffId)
+    if (res.code !== HttpStatusCode.OK) {
+      return {
+        code: BusinessCode.BAD_REQUEST,
+        msg: '获取客服信息失败',
+        data: null
+      }
+    }
+    return {
+      code: BusinessCode.SUCCESS,
+      msg: '获取客服信息成功',
+      data: res.data
+    }
+  }
+
+  async runPrompt(query: RunPromptDto): Promise<ServiceResponse<string>> {
+    const res = await this.httpService.runPrompt({
+      scene: query.scene,
+      prompt: query.prompt,
+      staff_profile: query.staffProfile,
+      user_profile: query.userProfile,
+      dialogue_history: query.dialogueHistory,
+      model_name: query.modelName,
+      current_timestamp: query.currentTimestamp
+    })
+    console.log(res)
+    if (res.code !== HttpStatusCode.OK) {
+      return {
+        code: BusinessCode.BAD_REQUEST,
+        msg: '运行提示词失败',
+        data: null
+      }
+    }
+    return {
+      code: BusinessCode.SUCCESS,
+      msg: '运行提示词成功',
+      data: res.data
+    }
+  }
 }