import { Body, Controller, Delete, Get, Param, Post, Put, Query, UseGuards, } from '@nestjs/common'; import type { CreateWecomBotRequest, UpdateWecomBotRequest } from '@dukang/shared-types'; import { HqAuthGuard } from '../../common/guards/hq-auth.guard'; import { HqPermissionGuard, RequireHqPermissions, } from '../../common/guards/hq-permission.guard'; import { HqOperation } from '../../common/hq-operation/hq-operation.decorator'; import { HqOperationAction } from '../../common/hq-operation/hq-operation.constants'; import { CurrentUser } from '../../common/decorators/current-user.decorator'; import type { AuthUser } from '../../common/guards/jwt-auth.guard'; import { AdminWecomBotsService } from './admin-wecom-bots.service'; import { AdminLlmConfigsService } from './admin-llm-configs.service'; import { AdminKnowledgeBasesService } from './admin-knowledge-bases.service'; @Controller('admin/wecom-bots') @UseGuards(HqAuthGuard, HqPermissionGuard) @RequireHqPermissions('wecom_bots') export class AdminWecomBotsController { constructor( private readonly service: AdminWecomBotsService, private readonly llmConfigs: AdminLlmConfigsService, private readonly knowledgeBases: AdminKnowledgeBasesService, ) {} @Get() list( @Query('name') name?: string, @Query('role') role?: string, @Query('enabled') enabled?: string, @Query('page') page?: string, @Query('pageSize') pageSize?: string, ) { return this.service.list({ name, role, enabled, page: page ? Number(page) : undefined, pageSize: pageSize ? Number(pageSize) : undefined, }); } @Post('reload') @HqOperation({ action: HqOperationAction.WECOM_BOT_RELOAD, refType: 'WECOM_BOT', batch: true, }) reload() { return this.service.reloadRuntime(); } @Get('ai-options') async aiOptions(@CurrentUser() user: AuthUser) { const actor = await this.llmConfigs.resolveActor(user.actorId); const [llmConfigs, knowledgeBases] = await Promise.all([ this.llmConfigs.options(actor), this.knowledgeBases.options(actor), ]); return { llmConfigs, knowledgeBases }; } @Get(':id') detail(@Param('id') id: string) { return this.service.detail(BigInt(id)); } @Post() @HqOperation({ action: HqOperationAction.WECOM_BOT_CREATE, refType: 'WECOM_BOT', includeBody: true, }) create(@Body() body: CreateWecomBotRequest) { return this.service.create(body); } @Put(':id') @HqOperation({ action: HqOperationAction.WECOM_BOT_UPDATE, refType: 'WECOM_BOT', refIdField: 'id', includeBody: true, }) update(@Param('id') id: string, @Body() body: UpdateWecomBotRequest) { return this.service.update(BigInt(id), body); } @Delete(':id') @HqOperation({ action: HqOperationAction.WECOM_BOT_DELETE, refType: 'WECOM_BOT', refIdField: 'id', }) remove(@Param('id') id: string) { return this.service.remove(BigInt(id)); } }