feat: i18n 翻译覆盖 + DB 动态数据 + 模型选择器 API

This commit is contained in:
yuzhiran-dev
2026-05-22 16:55:18 +08:00
parent 23edb74bce
commit 1b4c19daa6
16 changed files with 763 additions and 1036 deletions
+326 -65
View File
@@ -1,4 +1,5 @@
import { Injectable } from '@nestjs/common';
import * as bcrypt from 'bcryptjs';
import { PrismaService } from '../../prisma/prisma.service';
export interface ToolDefinition {
@@ -25,44 +26,45 @@ export class AdminAiAssistantService {
private prisma: PrismaService,
) {}
getAvailableTools(): ToolDefinition[] {
private crudTools(): ToolDefinition[] {
return [
{
name: 'navigate',
description: '跳转到管理后台的某个页面',
parameters: {
path: { type: 'string', description: '页面路径,如 /admin/users、/admin/orders、/admin/analytics、/admin/enterprise、/admin/operations/banners、/admin/operations/notifications、/admin/settings/roles、/admin/settings/config、/admin/comments、/admin/courses、/admin/prompts、/admin/contents、/admin/tools' },
},
parameters: { path: { type: 'string', description: '页面路径,如 /admin/users、/admin/orders、/admin/analytics、/admin/enterprise、/admin/operations/banners、/admin/operations/notifications、/admin/settings/roles、/admin/settings/config、/admin/comments、/admin/courses、/admin/prompts、/admin/contents、/admin/tools' } },
},
{
name: 'get-dashboard',
description: '获取仪表盘概览数据(用户数、课程数、内容数、提示词数、订单数)',
parameters: {},
},
// ---- 用户管理 ----
{
name: 'list-users',
description: '列出用户,可按关键词搜索',
parameters: {
search: { type: 'string', description: '搜索关键词(可选)' },
page: { type: 'number', description: '页码(可选,默认1' },
pageSize: { type: 'number', description: '每页数量(可选,默认20' },
},
parameters: { search: { type: 'string', description: '搜索关键词(可选)' }, page: { type: 'number', description: '页码(可选)' }, pageSize: { type: 'number', description: '每页数量(可选)' } },
},
{
name: 'get-user',
description: '查看某个用户的详细信息',
parameters: {
id: { type: 'number', description: '用户ID' },
},
description: '查看用户详情(包含最近订单)',
parameters: { id: { type: 'number', description: '用户ID' } },
},
{
name: 'update-user-status',
description: '修改用户状态(启用/禁用/封禁)',
parameters: {
id: { type: 'number', description: '用户ID' },
status: { type: 'string', enum: ['ACTIVE', 'INACTIVE', 'BANNED'], description: '新状态' },
},
description: '修改用户状态',
parameters: { id: { type: 'number', description: '用户ID' }, status: { type: 'string', enum: ['ACTIVE', 'INACTIVE', 'BANNED'], description: '新状态' } },
},
{
name: 'update-user',
description: '修改用户信息(昵称、手机号、邮箱、状态等)',
parameters: { id: { type: 'number', description: '用户ID' }, nickname: { type: 'string', description: '新昵称(可选)' }, phone: { type: 'string', description: '新手机号(可选)' }, email: { type: 'string', description: '新邮箱(可选)' }, status: { type: 'string', enum: ['ACTIVE', 'INACTIVE', 'BANNED'], description: '新状态(可选)' }, memberPlan: { type: 'string', enum: ['FREE', 'MONTHLY', 'YEARLY'], description: '会员计划(可选)' }, sandboxDaily: { type: 'number', description: '每日沙盒次数(可选)' } },
},
{
name: 'delete-user',
description: '软删除一个用户',
parameters: { id: { type: 'number', description: '用户ID' } },
},
// ---- 订单 ----
{
name: 'list-orders',
description: '查看最近订单列表',
@@ -73,82 +75,169 @@ export class AdminAiAssistantService {
description: '获取数据分析概览(用户增长、收入、趋势)',
parameters: {},
},
// ---- 评论审核 ----
{
name: 'list-comments',
description: '查看评论列表,可按状态筛选',
parameters: {
status: { type: 'string', enum: ['PENDING_REVIEW', 'PUBLISHED', 'REJECTED'], description: '评论状态(可选)' },
},
parameters: { status: { type: 'string', enum: ['PENDING_REVIEW', 'PUBLISHED', 'REJECTED'], description: '评论状态(可选)' } },
},
{
name: 'approve-comment',
description: '通过一条待审核评论',
parameters: {
id: { type: 'number', description: '评论ID' },
},
name: 'approve-comment', description: '通过一条待审核评论', parameters: { id: { type: 'number', description: '评论ID' } },
},
{
name: 'reject-comment',
description: '拒绝一条评论并给出原因',
parameters: {
id: { type: 'number', description: '评论ID' },
reason: { type: 'string', description: '拒绝原因(可选)' },
},
name: 'reject-comment', description: '拒绝一条评论', parameters: { id: { type: 'number', description: '评论ID' }, reason: { type: 'string', description: '拒绝原因(可选)' } },
},
// ---- 课程管理 ----
{
name: 'list-courses', description: '查看所有课程', parameters: {},
},
{
name: 'list-banners',
description: '查看所有Banner列表',
parameters: {},
name: 'get-course', description: '查看课程详情', parameters: { id: { type: 'number', description: '课程ID' } },
},
{
name: 'list-notifications',
description: '查看所有系统通知',
parameters: {},
name: 'create-course', description: '创建新课程(title 必填,其他可选)',
parameters: { title: { type: 'string', description: '课程标题(必填)' }, description: { type: 'string', description: '课程描述' }, price: { type: 'number', description: '价格(默认0' }, isFree: { type: 'boolean', description: '是否免费(默认true' }, status: { type: 'string', enum: ['DRAFT', 'PUBLISHED', 'ARCHIVED'], description: '状态(可选)' } },
},
{
name: 'list-config',
description: '查看系统配置项',
parameters: {},
name: 'update-course', description: '修改课程信息',
parameters: { id: { type: 'number', description: '课程ID' }, title: { type: 'string', description: '新标题' }, description: { type: 'string', description: '新描述' }, price: { type: 'number', description: '新价格' }, isFree: { type: 'boolean', description: '是否免费' }, status: { type: 'string', enum: ['DRAFT', 'PUBLISHED', 'ARCHIVED'], description: '新状态' } },
},
{
name: 'list-roles',
description: '查看所有管理角色',
parameters: {},
name: 'delete-course', description: '软删除一个课程', parameters: { id: { type: 'number', description: '课程ID' } },
},
{
name: 'list-admins',
description: '查看所有管理员账号',
parameters: {},
name: 'toggle-course-status', description: '上架/下架一个课程(自动切换)', parameters: { id: { type: 'number', description: '课程ID' } },
},
// ---- 内容管理 ----
{
name: 'list-contents', description: '查看所有内容', parameters: {},
},
{
name: 'toggle-course-status',
description: '上架或下架一个课程',
parameters: {
id: { type: 'number', description: '课程ID' },
},
name: 'create-content', description: '创建内容',
parameters: { title: { type: 'string', description: '标题(必填)' }, summary: { type: 'string', description: '摘要' }, content: { type: 'string', description: '正文' }, tags: { type: 'string', description: '标签(逗号分隔)' }, status: { type: 'string', enum: ['DRAFT', 'PUBLISHED', 'ARCHIVED'], description: '状态' } },
},
{
name: 'toggle-content-status',
description: '上架或下架一个内容',
parameters: {
id: { type: 'number', description: '内容ID' },
},
name: 'update-content', description: '修改内容',
parameters: { id: { type: 'number', description: '内容ID' }, title: { type: 'string', description: '新标题' }, summary: { type: 'string', description: '新摘要' }, content: { type: 'string', description: '新正文' }, tags: { type: 'string', description: '新标签' }, status: { type: 'string', enum: ['DRAFT', 'PUBLISHED', 'ARCHIVED'], description: '新状态' } },
},
{
name: 'toggle-prompt-status',
description: '上架或下架一个提示词',
parameters: {
id: { type: 'number', description: '提示词ID' },
},
name: 'delete-content', description: '软删除一个内容', parameters: { id: { type: 'number', description: '内容ID' } },
},
{
name: 'get-enterprise-orgs',
description: '查看所有企业版组织',
parameters: {},
name: 'toggle-content-status', description: '上架/下架一个内容', parameters: { id: { type: 'number', description: '内容ID' } },
},
// ---- 提示词 ----
{
name: 'list-prompts', description: '查看所有提示词', parameters: {},
},
{
name: 'create-prompt', description: '创建提示词',
parameters: { title: { type: 'string', description: '标题(必填)' }, content: { type: 'string', description: '提示词内容(必填)' }, description: { type: 'string', description: '描述' }, tags: { type: 'string', description: '标签' }, status: { type: 'string', enum: ['DRAFT', 'PUBLISHED', 'ARCHIVED'], description: '状态' } },
},
{
name: 'update-prompt', description: '修改提示词',
parameters: { id: { type: 'number', description: '提示词ID' }, title: { type: 'string', description: '新标题' }, content: { type: 'string', description: '新内容' }, description: { type: 'string', description: '新描述' }, tags: { type: 'string', description: '新标签' }, status: { type: 'string', enum: ['DRAFT', 'PUBLISHED', 'ARCHIVED'], description: '新状态' } },
},
{
name: 'delete-prompt', description: '软删除一个提示词', parameters: { id: { type: 'number', description: '提示词ID' } },
},
{
name: 'toggle-prompt-status', description: '上架/下架一个提示词', parameters: { id: { type: 'number', description: '提示词ID' } },
},
// ---- Banner ----
{
name: 'list-banners', description: '查看所有Banner', parameters: {},
},
{
name: 'create-banner', description: '创建Banner',
parameters: { title: { type: 'string', description: '标题(必填)' }, image: { type: 'string', description: '图片URL(必填)' }, link: { type: 'string', description: '跳转链接' }, position: { type: 'string', description: '位置(默认home' }, sortOrder: { type: 'number', description: '排序(可选)' } },
},
{
name: 'update-banner', description: '修改Banner',
parameters: { id: { type: 'number', description: 'BannerID' }, title: { type: 'string', description: '新标题' }, image: { type: 'string', description: '新图片URL' }, link: { type: 'string', description: '新链接' }, sortOrder: { type: 'number', description: '新排序' } },
},
{
name: 'delete-banner', description: '删除一个Banner', parameters: { id: { type: 'number', description: 'BannerID' } },
},
// ---- 通知 ----
{
name: 'list-notifications', description: '查看系统通知', parameters: {},
},
{
name: 'create-notification', description: '创建系统通知(发送给所有用户或指定用户)',
parameters: { title: { type: 'string', description: '通知标题(必填)' }, content: { type: 'string', description: '通知内容(可选)' }, link: { type: 'string', description: '跳转链接(可选)' }, userId: { type: 'number', description: '指定用户ID,不填则发全体通知(可选)' } },
},
{
name: 'delete-notification', description: '删除一条通知', parameters: { id: { type: 'number', description: '通知ID' } },
},
// ---- 配置 ----
{
name: 'list-config', description: '查看系统配置', parameters: {},
},
{
name: 'update-config', description: '修改系统配置值',
parameters: { key: { type: 'string', description: '配置键(必填)' }, value: { type: 'string', description: '新值(必填)' }, category: { type: 'string', description: '分类(可选)' }, description: { type: 'string', description: '描述(可选)' } },
},
// ---- 角色权限 ----
{
name: 'list-roles', description: '查看所有管理角色', parameters: {},
},
{
name: 'create-role', description: '创建管理角色',
parameters: { name: { type: 'string', description: '角色名称(必填)' }, description: { type: 'string', description: '角色描述' }, permissions: { type: 'array', description: '权限列表,如["dashboard","users","courses"](可选)' } },
},
{
name: 'update-role', description: '修改角色',
parameters: { id: { type: 'string', description: '角色ID' }, name: { type: 'string', description: '新名称' }, description: { type: 'string', description: '新描述' }, permissions: { type: 'array', description: '新权限列表' } },
},
{
name: 'delete-role', description: '禁用一个角色', parameters: { id: { type: 'string', description: '角色ID' } },
},
// ---- 管理员 ----
{
name: 'list-admins', description: '查看所有管理员', parameters: {},
},
{
name: 'create-admin', description: '创建管理员账号',
parameters: { username: { type: 'string', description: '用户名(必填)' }, password: { type: 'string', description: '密码(必填)' }, nickname: { type: 'string', description: '昵称(可选)' }, roleId: { type: 'string', description: '角色ID(可选)' } },
},
{
name: 'update-admin', description: '修改管理员信息',
parameters: { id: { type: 'number', description: '管理员ID' }, nickname: { type: 'string', description: '新昵称' }, roleId: { type: 'string', description: '新角色ID' }, password: { type: 'string', description: '新密码' } },
},
{
name: 'delete-admin', description: '禁用一个管理员账号', parameters: { id: { type: 'number', description: '管理员ID' } },
},
// ---- 企业版 ----
{
name: 'get-enterprise-orgs', description: '查看所有企业组织', parameters: {},
},
{
name: 'create-organization', description: '创建企业组织',
parameters: { name: { type: 'string', description: '组织名称(必填)' }, description: { type: 'string', description: '描述' }, contactName: { type: 'string', description: '联系人' }, contactPhone: { type: 'string', description: '联系电话' } },
},
{
name: 'update-organization', description: '修改企业组织信息',
parameters: { id: { type: 'number', description: '组织ID' }, name: { type: 'string', description: '新名称' }, description: { type: 'string', description: '新描述' }, contactName: { type: 'string', description: '新联系人' }, contactPhone: { type: 'string', description: '新电话' } },
},
{
name: 'delete-organization', description: '删除一个企业组织', parameters: { id: { type: 'number', description: '组织ID' } },
},
{
name: 'add-org-member', description: '向企业组织添加成员',
parameters: { organizationId: { type: 'number', description: '组织ID' }, userId: { type: 'number', description: '用户ID' }, role: { type: 'string', enum: ['ADMIN', 'MEMBER'], description: '角色(默认MEMBER' } },
},
{
name: 'remove-org-member', description: '从企业组织移除成员',
parameters: { organizationId: { type: 'number', description: '组织ID' }, userId: { type: 'number', description: '用户ID' } },
},
];
}
getAvailableTools(): ToolDefinition[] {
return this.crudTools();
}
getToolsDescription(): string {
return this.getAvailableTools().map(t => {
const params = Object.entries(t.parameters)
@@ -299,6 +388,178 @@ export class AdminAiAssistantService {
return { success: true, data: { items, total: items.length }, summary: `${items.length} 个企业组织` };
}
// ---- 用户 CRUD ----
case 'update-user': {
const { id, ...data } = call.params;
await this.prisma.user.update({ where: { id }, data });
return { success: true, data: { id }, summary: `用户 ${id} 已更新` };
}
case 'delete-user': {
await this.prisma.user.update({ where: { id: call.params.id }, data: { deletedAt: new Date() } });
return { success: true, data: { id: call.params.id }, summary: `用户 ${call.params.id} 已删除` };
}
// ---- 课程 CRUD ----
case 'list-courses': {
const items = await this.prisma.course.findMany({ where: { deletedAt: null }, take: 50, orderBy: { createdAt: 'desc' }, select: { id: true, title: true, price: true, isFree: true, status: true, createdAt: true } });
return { success: true, data: { items, total: items.length }, summary: `${items.length} 个课程` };
}
case 'get-course': {
const item = await this.prisma.course.findUnique({ where: { id: call.params.id } });
if (!item) throw new Error('课程不存在');
return { success: true, data: item, summary: `课程: ${item.title}` };
}
case 'create-course': {
const created = await this.prisma.course.create({ data: { title: call.params.title, description: call.params.description || '', price: call.params.price ?? 0, isFree: call.params.isFree ?? true, status: call.params.status || 'DRAFT' } });
return { success: true, data: created, summary: `课程「${created.title}」已创建 (ID:${created.id})` };
}
case 'update-course': {
const { id: courseId, ...courseData } = call.params;
await this.prisma.course.update({ where: { id: courseId }, data: courseData });
return { success: true, data: { id: courseId }, summary: `课程 ${courseId} 已更新` };
}
case 'delete-course': {
await this.prisma.course.update({ where: { id: call.params.id }, data: { deletedAt: new Date() } });
return { success: true, data: { id: call.params.id }, summary: `课程 ${call.params.id} 已删除` };
}
// ---- 内容 CRUD ----
case 'list-contents': {
const items = await this.prisma.content.findMany({ where: { deletedAt: null }, take: 50, orderBy: { createdAt: 'desc' }, select: { id: true, title: true, status: true, contentType: true, createdAt: true } });
return { success: true, data: { items, total: items.length }, summary: `${items.length} 个内容` };
}
case 'create-content': {
const created = await this.prisma.content.create({ data: { title: call.params.title, summary: call.params.summary || '', content: call.params.content || '', tags: call.params.tags || '', status: call.params.status || 'DRAFT' } });
return { success: true, data: created, summary: `内容「${created.title}」已创建 (ID:${created.id})` };
}
case 'update-content': {
const { id: contentId, ...contentData } = call.params;
await this.prisma.content.update({ where: { id: contentId }, data: contentData });
return { success: true, data: { id: contentId }, summary: `内容 ${contentId} 已更新` };
}
case 'delete-content': {
await this.prisma.content.update({ where: { id: call.params.id }, data: { deletedAt: new Date() } });
return { success: true, data: { id: call.params.id }, summary: `内容 ${call.params.id} 已删除` };
}
// ---- 提示词 CRUD ----
case 'list-prompts': {
const items = await this.prisma.prompt.findMany({ where: { deletedAt: null }, take: 50, orderBy: { createdAt: 'desc' }, select: { id: true, title: true, status: true, isPublic: true, createdAt: true } });
return { success: true, data: { items, total: items.length }, summary: `${items.length} 个提示词` };
}
case 'create-prompt': {
const created = await this.prisma.prompt.create({ data: { title: call.params.title, content: call.params.content, description: call.params.description || '', tags: call.params.tags || '', status: call.params.status || 'PUBLISHED' } });
return { success: true, data: created, summary: `提示词「${created.title}」已创建 (ID:${created.id})` };
}
case 'update-prompt': {
const { id: promptId, ...promptData } = call.params;
await this.prisma.prompt.update({ where: { id: promptId }, data: promptData });
return { success: true, data: { id: promptId }, summary: `提示词 ${promptId} 已更新` };
}
case 'delete-prompt': {
await this.prisma.prompt.update({ where: { id: call.params.id }, data: { deletedAt: new Date() } });
return { success: true, data: { id: call.params.id }, summary: `提示词 ${call.params.id} 已删除` };
}
// ---- Banner CRUD ----
case 'create-banner': {
const created = await this.prisma.banner.create({ data: { title: call.params.title, image: call.params.image, link: call.params.link || '', position: call.params.position || 'home', sortOrder: call.params.sortOrder ?? 0 } });
return { success: true, data: created, summary: `Banner「${created.title}」已创建` };
}
case 'update-banner': {
const { id: bannerId, ...bannerData } = call.params;
await this.prisma.banner.update({ where: { id: bannerId }, data: bannerData });
return { success: true, data: { id: bannerId }, summary: `Banner ${bannerId} 已更新` };
}
case 'delete-banner': {
await this.prisma.banner.delete({ where: { id: call.params.id } });
return { success: true, data: { id: call.params.id }, summary: `Banner ${call.params.id} 已删除` };
}
// ---- 通知 ----
case 'create-notification': {
const { title: notifTitle, content: notifContent, link: notifLink, userId: notifUserId } = call.params;
if (notifUserId) {
await this.prisma.notification.create({ data: { userId: notifUserId, type: 'system', title: notifTitle, content: notifContent || '', link: notifLink || '' } });
return { success: true, data: {}, summary: `通知已发送给用户 ${notifUserId}` };
}
const users = await this.prisma.user.findMany({ where: { deletedAt: null }, select: { id: true } });
await this.prisma.notification.createMany({ data: users.map(u => ({ userId: u.id, type: 'system', title: notifTitle, content: notifContent || '', link: notifLink || '' })) });
return { success: true, data: { userCount: users.length }, summary: `通知已发送给 ${users.length} 个用户` };
}
case 'delete-notification': {
await this.prisma.notification.delete({ where: { id: call.params.id } });
return { success: true, data: { id: call.params.id }, summary: `通知 ${call.params.id} 已删除` };
}
// ---- 配置 ----
case 'update-config': {
const { key: cfgKey, value: cfgValue, category: cfgCategory, description: cfgDesc } = call.params;
const existing = await this.prisma.systemConfig.findUnique({ where: { key: cfgKey } });
if (existing) {
await this.prisma.systemConfig.update({ where: { key: cfgKey }, data: { value: cfgValue } });
return { success: true, data: { key: cfgKey }, summary: `配置 ${cfgKey} 已更新为 ${cfgValue}` };
}
await this.prisma.systemConfig.create({ data: { key: cfgKey, value: cfgValue, category: cfgCategory || 'general', description: cfgDesc || '' } });
return { success: true, data: { key: cfgKey }, summary: `配置 ${cfgKey} 已创建` };
}
// ---- 角色 CRUD ----
case 'create-role': {
const created = await this.prisma.adminRole.create({ data: { name: call.params.name, description: call.params.description || '', permissions: call.params.permissions || [] } });
return { success: true, data: created, summary: `角色「${created.name}」已创建 (ID:${created.id})` };
}
case 'update-role': {
const { id: roleId, ...roleData } = call.params;
if (roleData.permissions) roleData.permissions = JSON.stringify(roleData.permissions);
await this.prisma.adminRole.update({ where: { id: roleId }, data: roleData });
return { success: true, data: { id: roleId }, summary: `角色 ${roleId} 已更新` };
}
case 'delete-role': {
await this.prisma.adminRole.update({ where: { id: call.params.id }, data: { status: 'INACTIVE' } });
return { success: true, data: { id: call.params.id }, summary: `角色 ${call.params.id} 已禁用` };
}
// ---- 管理员 CRUD ----
case 'create-admin': {
const hash = await bcrypt.hash(call.params.password, 10);
const created = await this.prisma.adminUser.create({ data: { username: call.params.username, passwordHash: hash, nickname: call.params.nickname || call.params.username, roleId: call.params.roleId || null } });
return { success: true, data: { id: created.id }, summary: `管理员「${created.username}」已创建` };
}
case 'update-admin': {
const { id: adminId, password, ...adminData } = call.params;
if (password) adminData['passwordHash'] = await bcrypt.hash(password, 10);
await this.prisma.adminUser.update({ where: { id: adminId }, data: adminData as any });
return { success: true, data: { id: adminId }, summary: `管理员 ${adminId} 已更新` };
}
case 'delete-admin': {
await this.prisma.adminUser.update({ where: { id: call.params.id }, data: { status: 'INACTIVE' } });
return { success: true, data: { id: call.params.id }, summary: `管理员 ${call.params.id} 已禁用` };
}
// ---- 企业版 ----
case 'create-organization': {
const created = await this.prisma.organization.create({ data: { name: call.params.name, description: call.params.description || '', contactName: call.params.contactName || '', contactPhone: call.params.contactPhone || '' } });
return { success: true, data: created, summary: `组织「${created.name}」已创建` };
}
case 'update-organization': {
const { id: orgId, ...orgData } = call.params;
await this.prisma.organization.update({ where: { id: orgId }, data: orgData });
return { success: true, data: { id: orgId }, summary: `组织 ${orgId} 已更新` };
}
case 'delete-organization': {
await this.prisma.organization.update({ where: { id: call.params.id }, data: { status: 'INACTIVE' } });
return { success: true, data: { id: call.params.id }, summary: `组织 ${call.params.id} 已删除` };
}
case 'add-org-member': {
await this.prisma.organizationMember.create({ data: { organizationId: call.params.organizationId, userId: call.params.userId, role: call.params.role || 'MEMBER' } });
return { success: true, data: {}, summary: `用户 ${call.params.userId} 已加入组织 ${call.params.organizationId}` };
}
case 'remove-org-member': {
await this.prisma.organizationMember.delete({ where: { organizationId_userId: { organizationId: call.params.organizationId, userId: call.params.userId } } });
return { success: true, data: {}, summary: `用户 ${call.params.userId} 已从组织 ${call.params.organizationId} 移除` };
}
default:
throw new Error(`未知工具: ${call.tool}`);
}