Browse Source

feat: 添加模块接口

huangzhichao 2 days ago
parent
commit
732fd17259

+ 3 - 1
src/app.module.ts

@@ -1,5 +1,6 @@
 import { Module } from '@nestjs/common'
 
+import { AgentExecutionModule } from '@/module/agent-execution/agent-execution.module'
 import { AgentListModule } from '@/module/agent-list/agent-list.module'
 import { AgentModuleModule } from '@/module/agent-module/agent-module.module'
 import { AgentServerModule } from '@/module/agent-server/agent-server.module'
@@ -16,7 +17,8 @@ import { TencentCloudModule } from '@/module/tencent-cloud/tencent-cloud.module'
     ChatManagementModule,
     AgentListModule,
     AgentTestModule,
-    AgentModuleModule
+    AgentModuleModule,
+    AgentExecutionModule
   ]
 })
 export class AppModule {}

+ 31 - 0
src/dto/agent-execution.ts

@@ -0,0 +1,31 @@
+import { IsString, IsNotEmpty, IsNumber } from 'class-validator'
+
+export class CreateAgentTaskDto {
+  @IsNumber()
+  @IsNotEmpty({ message: 'agentId不能为空' })
+  agentId: number
+
+  @IsString()
+  @IsNotEmpty({ message: 'taskPrompt不能为空' })
+  taskPrompt: string
+
+  @IsString()
+  @IsNotEmpty({ message: 'user不能为空' })
+  user: string
+}
+
+export class GetAgentTaskListDto {
+  @IsNumber()
+  @IsNotEmpty({ message: 'pageNum不能为空' })
+  pageNum: number
+
+  @IsNumber()
+  @IsNotEmpty({ message: 'pageSize不能为空' })
+  pageSize: number
+}
+
+export class GetAgentTaskDetailDto {
+  @IsNumber()
+  @IsNotEmpty({ message: 'agentTaskId不能为空' })
+  agentTaskId: number
+}

+ 7 - 0
src/interface/agent-execution.interface.ts

@@ -0,0 +1,7 @@
+import { ServiceResponse } from '../response/response.interface'
+
+export interface IAgentExecutionService {
+  createAgentTask(params: Record<string, any>): Promise<ServiceResponse<any>>
+  getAgentTaskList(params: Record<string, any>): Promise<ServiceResponse<any>>
+  getAgentTaskDetail(params: Record<string, any>): Promise<ServiceResponse<any>>
+}

+ 34 - 0
src/module/agent-execution/agent-execution.controller.ts

@@ -0,0 +1,34 @@
+import { Controller, Get, Query, Post, Body } from '@nestjs/common'
+import { ApiOperation, ApiTags } from '@nestjs/swagger'
+
+import {
+  CreateAgentTaskDto,
+  GetAgentTaskListDto,
+  GetAgentTaskDetailDto
+} from '@/dto/agent-execution'
+
+import { AgentExecutionService } from './service/agent-execution.service'
+
+@ApiTags('agent-execution')
+@Controller('agent-execution')
+export class AgentExecutionController {
+  constructor(private readonly agentExecutionService: AgentExecutionService) {}
+
+  @Post('create-agent-task')
+  @ApiOperation({ summary: '创建任务' })
+  async createAgentTask(@Body() body: CreateAgentTaskDto) {
+    return this.agentExecutionService.createAgentTask(body)
+  }
+
+  @Get('get-agent-task-list')
+  @ApiOperation({ summary: '获取任务列表' })
+  async getAgentTaskList(@Query() query: GetAgentTaskListDto) {
+    return this.agentExecutionService.getAgentTaskList(query)
+  }
+
+  @Get('get-agent-task-detail')
+  @ApiOperation({ summary: '获取任务详情' })
+  async getAgentTaskDetail(@Query() query: GetAgentTaskDetailDto) {
+    return this.agentExecutionService.getAgentTaskDetail(query)
+  }
+}

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

@@ -0,0 +1,14 @@
+import { Module } from '@nestjs/common'
+
+import { HttpClientModule } from '@/shared/http-client/http-client.module'
+
+import { AgentExecutionController } from './agent-execution.controller'
+import { AgentExecutionHttpService } from './service/agent-execution-http.service'
+import { AgentExecutionService } from './service/agent-execution.service'
+
+@Module({
+  imports: [HttpClientModule],
+  controllers: [AgentExecutionController],
+  providers: [AgentExecutionHttpService, AgentExecutionService]
+})
+export class AgentExecutionModule {}

+ 46 - 0
src/module/agent-execution/service/agent-execution-http.service.ts

@@ -0,0 +1,46 @@
+import { Injectable } from '@nestjs/common'
+
+import { ServiceResponse } from '@/response/response.interface'
+import { HttpClientService } from '@/shared/http-client/http-client.service'
+
+@Injectable()
+export class AgentExecutionHttpService {
+  // private readonly baseUrl = 'http://192.168.206.189:8083/api'
+  private readonly baseUrl = 'http://192.168.206.189:4090/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 createAgentTask(
+    params: Record<string, any>
+  ): Promise<ServiceResponse<any>> {
+    return this.makeRequestPost<any>('createAgentTask', params)
+  }
+
+  async getAgentTaskList(
+    params: Record<string, any>
+  ): Promise<ServiceResponse<any>> {
+    return this.makeRequest<any>('getAgentTaskList', params)
+  }
+
+  async getAgentTaskDetail(
+    params: Record<string, any>
+  ): Promise<ServiceResponse<any>> {
+    return this.makeRequest<any>('getAgentTaskDetail', params)
+  }
+}

+ 67 - 0
src/module/agent-execution/service/agent-execution.service.ts

@@ -0,0 +1,67 @@
+import { Injectable } from '@nestjs/common'
+
+import { IAgentExecutionService } from '@/interface/agent-execution.interface'
+import { ServiceResponse } from '@/response/response.interface'
+import { BusinessCode, HttpStatusCode } from '@/response/status-code.enum'
+
+import { AgentExecutionHttpService } from './agent-execution-http.service'
+
+@Injectable()
+export class AgentExecutionService implements IAgentExecutionService {
+  constructor(private readonly httpService: AgentExecutionHttpService) {}
+
+  async createAgentTask(
+    params: Record<string, any>
+  ): Promise<ServiceResponse<any>> {
+    const { code, data, msg } = await this.httpService.createAgentTask(params)
+    if (code !== HttpStatusCode.OK) {
+      return {
+        code: BusinessCode.BAD_REQUEST,
+        msg: msg || '创建任务失败',
+        data: null
+      }
+    }
+    return {
+      code: BusinessCode.SUCCESS,
+      data,
+      msg: msg || '创建任务成功'
+    }
+  }
+
+  async getAgentTaskList(
+    params: Record<string, any>
+  ): Promise<ServiceResponse<any>> {
+    const { code, data, msg } = await this.httpService.getAgentTaskList(params)
+    if (code !== HttpStatusCode.OK) {
+      return {
+        code: BusinessCode.BAD_REQUEST,
+        msg: msg || '获取任务列表失败',
+        data: null
+      }
+    }
+    return {
+      code: BusinessCode.SUCCESS,
+      data,
+      msg: msg || '获取任务列表成功'
+    }
+  }
+
+  async getAgentTaskDetail(
+    params: Record<string, any>
+  ): Promise<ServiceResponse<any>> {
+    const { code, data, msg } =
+      await this.httpService.getAgentTaskDetail(params)
+    if (code !== HttpStatusCode.OK) {
+      return {
+        code: BusinessCode.BAD_REQUEST,
+        msg: msg || '获取任务详情失败',
+        data: null
+      }
+    }
+    return {
+      code: BusinessCode.SUCCESS,
+      data,
+      msg: msg || '获取任务详情成功'
+    }
+  }
+}