Explorar o código

feat: 完成agent list 接口

huangzhichao hai 3 días
pai
achega
3c621d75f6

+ 2 - 1
.env

@@ -1,2 +1,3 @@
 secretId = AKIDPfjDbpqfWEiPxISQHF0nNjrlTyFJB7DA
-secretKey = nPqPqDQUL5e13va4KOFuMdiaHl26zf9Q
+secretKey = nPqPqDQUL5e13va4KOFuMdiaHl26zf9Q
+PORT = 8083

+ 1 - 0
.env.test

@@ -0,0 +1 @@
+PORT = 4090

+ 9 - 3
src/app.module.ts

@@ -1,12 +1,18 @@
 import { Module } from '@nestjs/common'
 
+import { AgentListModule } from '@/module/agent-list/agent-list.module'
 import { AgentServerModule } from '@/module/agent-server/agent-server.module'
+import { ChatManagementModule } from '@/module/chat-management/chat-management.module'
 import { SqlAgentModule } from '@/module/sql-agent/sql-agent.module'
 import { TencentCloudModule } from '@/module/tencent-cloud/tencent-cloud.module'
 
-import { ChatManagementModule } from '@/module/chat-management/chat-management.module'
-
 @Module({
-  imports: [SqlAgentModule, TencentCloudModule, AgentServerModule, ChatManagementModule]
+  imports: [
+    SqlAgentModule,
+    TencentCloudModule,
+    AgentServerModule,
+    ChatManagementModule,
+    AgentListModule
+  ]
 })
 export class AppModule {}

+ 62 - 0
src/dto/agent-list.ts

@@ -0,0 +1,62 @@
+import {
+  IsString,
+  IsNotEmpty,
+  IsNumber,
+  IsArray,
+  IsObject
+} from 'class-validator'
+
+export class GetNativeAgentListDto {
+  @IsNumber()
+  @IsNotEmpty({ message: 'page不能为空' })
+  page: number
+
+  @IsNumber()
+  @IsNotEmpty({ message: 'page_size不能为空' })
+  pageSize: number
+
+  @IsString()
+  createUser: string
+
+  @IsString()
+  updateUser: string
+}
+
+export class GetNativeAgentConfigurationDto {
+  @IsString()
+  @IsNotEmpty({ message: 'agent_id不能为空' })
+  agentId: string
+}
+
+export class SaveNativeAgentConfigurationDto {
+  @IsNumber()
+  agentId: number
+
+  @IsString()
+  @IsNotEmpty({ message: 'name不能为空' })
+  name: string
+
+  @IsString()
+  displayName: string
+
+  @IsNumber()
+  type: number
+
+  @IsString()
+  executionModel: string
+
+  @IsString()
+  systemPrompt: string
+
+  @IsString()
+  taskPrompt: string
+
+  @IsArray()
+  tools: string[]
+
+  @IsArray()
+  subAgents: number[]
+
+  @IsObject()
+  extraParams: Record<string, any>
+}

+ 14 - 0
src/interface/agent-list.interface.ts

@@ -0,0 +1,14 @@
+import { ServiceResponse } from '../response/response.interface'
+
+export interface IAgentListService {
+  getNativeAgentList(
+    params: Record<string, any>
+  ): Promise<ServiceResponse<AgentListType[]>>
+  getNativeAgentConfiguration(
+    agentId: string
+  ): Promise<ServiceResponse<AgentConfigurationType[]>>
+  saveNativeAgentConfiguration(
+    params: SaveAgentConfigurationType
+  ): Promise<ServiceResponse<SaveAgentConfigurationType>>
+  getToolList(): Promise<ServiceResponse<any>>
+}

+ 60 - 0
src/module/agent-list/agent-list.controller.ts

@@ -0,0 +1,60 @@
+import { Controller, Get, Query, Post, Body } from '@nestjs/common'
+import { ApiOperation, ApiTags } from '@nestjs/swagger'
+
+import {
+  GetNativeAgentListDto,
+  GetNativeAgentConfigurationDto,
+  SaveNativeAgentConfigurationDto
+} from '@/dto/agent-list'
+
+import { AgentListService } from './service/agent-list.service'
+
+@ApiTags('agent-list')
+@Controller('agent-list')
+export class AgentListController {
+  constructor(private readonly agentListService: AgentListService) {}
+
+  @Get('native-agent-list')
+  @ApiOperation({ summary: '获取原生客服列表' })
+  async getNativeAgentList(@Query() query: GetNativeAgentListDto) {
+    return this.agentListService.getNativeAgentList({
+      page: query.page,
+      page_size: query.pageSize,
+      create_user: query.createUser,
+      update_user: query.updateUser
+    })
+  }
+
+  @Get('native-agent-configuration')
+  @ApiOperation({ summary: '获取原生客服配置' })
+  async getNativeAgentConfiguration(
+    @Query() query: GetNativeAgentConfigurationDto
+  ) {
+    return this.agentListService.getNativeAgentConfiguration(query.agentId)
+  }
+
+  @Post('native-agent-configuration')
+  @ApiOperation({ summary: '保存原生客服配置' })
+  async saveNativeAgentConfiguration(
+    @Body() body: SaveNativeAgentConfigurationDto
+  ) {
+    return this.agentListService.saveNativeAgentConfiguration({
+      agent_id: body.agentId,
+      name: body.name,
+      display_name: body.displayName,
+      type: body.type,
+      execution_model: body.executionModel,
+      system_prompt: body.systemPrompt,
+      task_prompt: body.taskPrompt,
+      tools: body.tools,
+      sub_agents: body.subAgents,
+      extra_params: body.extraParams
+    })
+  }
+
+  @Get('tool-list')
+  @ApiOperation({ summary: '获取工具列表' })
+  async getToolList() {
+    return this.agentListService.getToolList()
+  }
+}

+ 14 - 0
src/module/agent-list/agent-list.module.ts

@@ -0,0 +1,14 @@
+import { Module } from '@nestjs/common'
+
+import { HttpClientModule } from '@/shared/http-client/http-client.module'
+
+import { AgentListController } from './agent-list.controller'
+import { AgentListHttpService } from './service/agent-list-http.service'
+import { AgentListService } from './service/agent-list.service'
+
+@Module({
+  imports: [HttpClientModule],
+  controllers: [AgentListController],
+  providers: [AgentListHttpService, AgentListService]
+})
+export class AgentListModule {}

+ 55 - 0
src/module/agent-list/service/agent-list-http.service.ts

@@ -0,0 +1,55 @@
+import { Injectable } from '@nestjs/common'
+
+import { ServiceResponse } from '@/response/response.interface'
+import { HttpClientService } from '@/shared/http-client/http-client.service'
+
+@Injectable()
+export class AgentListHttpService {
+  private readonly baseUrl = 'http://192.168.206.189:8083/api'
+
+  constructor(private readonly httpClientService: HttpClientService) {}
+
+  private async makeRequest<T>(
+    endpoint: string,
+    params?: Record<string, any>
+  ): Promise<ServiceResponse<T>> {
+    const url = `${this.baseUrl}/${endpoint}`
+    return this.httpClientService.get<T>(url, params)
+  }
+
+  async makeRequestPost<T>(
+    endpoint: string,
+    params?: Record<string, any>
+  ): Promise<ServiceResponse<T>> {
+    const url = `${this.baseUrl}/${endpoint}`
+    return this.httpClientService.post<T>(url, params)
+  }
+
+  async getNativeAgentList(
+    params: Record<string, any>
+  ): Promise<ServiceResponse<AgentListType[]>> {
+    return this.makeRequest<AgentListType[]>('getNativeAgentList', params)
+  }
+
+  async getNativeAgentConfiguration(
+    agent_id: string
+  ): Promise<ServiceResponse<AgentConfigurationType[]>> {
+    return this.makeRequest<AgentConfigurationType[]>(
+      'getNativeAgentConfiguration',
+      { agent_id }
+    )
+  }
+
+  async saveNativeAgentConfiguration(
+    params: SaveAgentConfigurationType
+  ): Promise<ServiceResponse<SaveAgentConfigurationType>> {
+    return this.makeRequestPost<SaveAgentConfigurationType>(
+      'saveNativeAgentConfiguration',
+      params
+    )
+  }
+
+  async getToolList(): Promise<ServiceResponse<any>> {
+    return this.makeRequest<any>('getToolList')
+  }
+}

+ 85 - 0
src/module/agent-list/service/agent-list.service.ts

@@ -0,0 +1,85 @@
+import { Injectable } from '@nestjs/common'
+
+import { IAgentListService } from '@/interface/agent-list.interface'
+import { ServiceResponse } from '@/response/response.interface'
+import { BusinessCode, HttpStatusCode } from '@/response/status-code.enum'
+
+import { AgentListHttpService } from './agent-list-http.service'
+
+@Injectable()
+export class AgentListService implements IAgentListService {
+  constructor(private readonly httpService: AgentListHttpService) {}
+
+  async getNativeAgentList(
+    params: Record<string, any>
+  ): Promise<ServiceResponse<AgentListType[]>> {
+    const { code, data, msg } =
+      await this.httpService.getNativeAgentList(params)
+    if (code !== HttpStatusCode.OK) {
+      return {
+        code: BusinessCode.BAD_REQUEST,
+        msg: msg || '获取客服列表失败',
+        data: null
+      }
+    }
+    return {
+      code: BusinessCode.SUCCESS,
+      msg: '获取客服列表成功',
+      data: data
+    }
+  }
+
+  async getNativeAgentConfiguration(
+    agentId: string
+  ): Promise<ServiceResponse<AgentConfigurationType[]>> {
+    const { code, data, msg } =
+      await this.httpService.getNativeAgentConfiguration(agentId)
+    if (code !== HttpStatusCode.OK) {
+      return {
+        code: BusinessCode.BAD_REQUEST,
+        msg: msg || '获取客服列表失败',
+        data: null
+      }
+    }
+    return {
+      code: BusinessCode.SUCCESS,
+      msg: '获取客服列表成功',
+      data: data
+    }
+  }
+
+  async saveNativeAgentConfiguration(
+    params: SaveAgentConfigurationType
+  ): Promise<ServiceResponse<SaveAgentConfigurationType>> {
+    const { code, data, msg } =
+      await this.httpService.saveNativeAgentConfiguration(params)
+    if (code !== HttpStatusCode.OK) {
+      return {
+        code: BusinessCode.BAD_REQUEST,
+        msg: msg || '保存客服列表失败',
+        data: null
+      }
+    }
+    return {
+      code: BusinessCode.SUCCESS,
+      msg: '保存客服列表成功',
+      data: data
+    }
+  }
+
+  async getToolList(): Promise<ServiceResponse<any>> {
+    const { code, data, msg } = await this.httpService.getToolList()
+    if (code !== HttpStatusCode.OK) {
+      return {
+        code: BusinessCode.BAD_REQUEST,
+        msg: msg || '获取工具列表失败',
+        data: null
+      }
+    }
+    return {
+      code: BusinessCode.SUCCESS,
+      msg: '获取工具列表成功',
+      data: data
+    }
+  }
+}

+ 37 - 0
src/module/agent-list/type.d.ts

@@ -0,0 +1,37 @@
+type AgentListType = {
+  id: number
+  name: string
+  display_name: string
+  type: number
+  execution_model: string
+  create_time: string
+  update_time: string
+}
+
+type AgentConfigurationType = {
+  id: number
+  name: string
+  display_name: string
+  type: number
+  execution_model: string
+  system_prompt: string
+  task_prompt: string
+  tools: string[]
+  sub_agents: number[]
+  extra_params: Record<string, string>
+  create_time: string
+  update_time: string
+}
+
+type SaveAgentConfigurationType = {
+  agent_id?: number
+  name: string
+  display_name?: string
+  type?: number
+  execution_model?: string
+  system_prompt?: string
+  task_prompt?: string
+  tools?: string[]
+  sub_agents?: number[]
+  extra_params?: Record<string, string>
+}

+ 5 - 8
src/module/agent-server/agent-server.module.ts

@@ -1,18 +1,15 @@
-import { HttpModule } from '@nestjs/axios'
 import { Module } from '@nestjs/common'
 
+import { HttpClientModule } from '@/shared/http-client/http-client.module'
+
 import { AgentServerController } from './agent-server.controller'
 import { AgentServerHttpService } from './service/agent-server-http.service'
 import { AgentServerService } from './service/agent-server.service'
 
 @Module({
-  imports: [
-    HttpModule.register({
-      timeout: 600000,
-      maxRedirects: 3
-    })
-  ],
+  imports: [HttpClientModule],
   controllers: [AgentServerController],
-  providers: [AgentServerHttpService, AgentServerService]
+  providers: [AgentServerHttpService, AgentServerService],
+  exports: [AgentServerService]
 })
 export class AgentServerModule {}

+ 6 - 37
src/module/agent-server/service/agent-server-http.service.ts

@@ -1,59 +1,28 @@
-import { HttpService } from '@nestjs/axios'
 import { Injectable } from '@nestjs/common'
-import { lastValueFrom } from 'rxjs'
 
 import { ServiceResponse } from '@/response/response.interface'
-import { HttpStatusCode } from '@/response/status-code.enum'
+import { HttpClientService } from '@/shared/http-client/http-client.service'
 
 @Injectable()
 export class AgentServerHttpService {
   private readonly baseUrl = 'http://192.168.206.189:8083/api'
 
-  constructor(private readonly httpService: HttpService) {}
+  constructor(private readonly httpClientService: HttpClientService) {}
 
   private async makeRequest<T>(
     endpoint: string,
     params?: Record<string, any>
   ): Promise<ServiceResponse<T>> {
-    try {
-      const url = `${this.baseUrl}/${endpoint}`
-      const responseObservable = await this.httpService.get<ServiceResponse<T>>(
-        url,
-        { params }
-      )
-      const { data } = await lastValueFrom(responseObservable)
-      return data
-    } catch (error) {
-      return {
-        code: HttpStatusCode.INTERNAL_SERVER_ERROR,
-        msg: error.message,
-        data: null as T
-      }
-    }
+    const url = `${this.baseUrl}/${endpoint}`
+    return this.httpClientService.get<T>(url, params)
   }
 
   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
-      }
-    }
+    const url = `${this.baseUrl}/${endpoint}`
+    return this.httpClientService.post<T>(url, params)
   }
 
   async listStaffs(): Promise<ServiceResponse<StaffsType[]>> {

+ 5 - 8
src/module/chat-management/chat-management.module.ts

@@ -1,18 +1,15 @@
-import { HttpModule } from '@nestjs/axios'
 import { Module } from '@nestjs/common'
 
+import { HttpClientModule } from '@/shared/http-client/http-client.module'
+
 import { ChatManagementController } from './chat-management.controller'
 import { ChatManagementHttpService } from './service/chat-management-http.service'
 import { ChatManagementService } from './service/chat-management.service'
 
 @Module({
-  imports: [
-    HttpModule.register({
-      timeout: 600000,
-      maxRedirects: 3
-    })
-  ],
+  imports: [HttpClientModule],
   controllers: [ChatManagementController],
-  providers: [ChatManagementHttpService, ChatManagementService]
+  providers: [ChatManagementHttpService, ChatManagementService],
+  exports: [ChatManagementService]
 })
 export class ChatManagementModule {}

+ 6 - 37
src/module/chat-management/service/chat-management-http.service.ts

@@ -1,59 +1,28 @@
-import { HttpService } from '@nestjs/axios'
 import { Injectable } from '@nestjs/common'
-import { lastValueFrom } from 'rxjs'
 
 import { ServiceResponse } from '@/response/response.interface'
-import { HttpStatusCode } from '@/response/status-code.enum'
+import { HttpClientService } from '@/shared/http-client/http-client.service'
 
 @Injectable()
 export class ChatManagementHttpService {
   private readonly baseUrl = 'http://192.168.206.189:8083/api'
 
-  constructor(private readonly httpService: HttpService) {}
+  constructor(private readonly httpClientService: HttpClientService) {}
 
   private async makeRequest<T>(
     endpoint: string,
     params?: Record<string, any>
   ): Promise<ServiceResponse<T>> {
-    try {
-      const url = `${this.baseUrl}/${endpoint}`
-      const responseObservable = await this.httpService.get<ServiceResponse<T>>(
-        url,
-        { params }
-      )
-      const { data } = await lastValueFrom(responseObservable)
-      return data
-    } catch (error) {
-      return {
-        code: HttpStatusCode.INTERNAL_SERVER_ERROR,
-        msg: error.message,
-        data: null as T
-      }
-    }
+    const url = `${this.baseUrl}/${endpoint}`
+    return this.httpClientService.get<T>(url, params)
   }
 
   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
-      }
-    }
+    const url = `${this.baseUrl}/${endpoint}`
+    return this.httpClientService.post<T>(url, params)
   }
 
   // 获取对话总览列表

+ 0 - 1
src/prompt/sql-prompt/sql.ts

@@ -2,7 +2,6 @@ import QUERY_EXAMPLES from './query-examples'
 import SCHEMA_DEFINITIONS from './table-base.json'
 
 export function buildSqlPrompt(input: string) {
-  // console.log(businessRuleEnhancement(JSON.stringify(QUERY_EXAMPLES)))
   return {
     prompt_id: 'bi_sql_phase_prompt_v2',
     language: 'zh',

+ 11 - 0
src/shared/http-client/http-client.module.ts

@@ -0,0 +1,11 @@
+import { HttpModule } from '@nestjs/axios'
+import { Module } from '@nestjs/common'
+
+import { HttpClientService } from './http-client.service'
+
+@Module({
+  imports: [HttpModule],
+  providers: [HttpClientService],
+  exports: [HttpClientService]
+})
+export class HttpClientModule {} 

+ 54 - 0
src/shared/http-client/http-client.service.ts

@@ -0,0 +1,54 @@
+import { HttpService } from '@nestjs/axios'
+import { Injectable } from '@nestjs/common'
+import { lastValueFrom } from 'rxjs'
+
+import { ServiceResponse } from '@/response/response.interface'
+import { HttpStatusCode } from '@/response/status-code.enum'
+
+@Injectable()
+export class HttpClientService {
+  constructor(private readonly httpService: HttpService) {}
+
+  async get<T>(
+    url: string,
+    params?: Record<string, any>
+  ): Promise<ServiceResponse<T>> {
+    try {
+      const responseObservable = await this.httpService.get<ServiceResponse<T>>(
+        url,
+        { params }
+      )
+      const { data } = await lastValueFrom(responseObservable)
+      return data
+    } catch (error) {
+      return {
+        code: HttpStatusCode.INTERNAL_SERVER_ERROR,
+        msg: error.message,
+        data: null as T
+      }
+    }
+  }
+
+  async post<T>(
+    url: string,
+    params?: Record<string, any>
+  ): Promise<ServiceResponse<T>> {
+    try {
+      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
+      }
+    }
+  }
+}