Features: - Migrate 10 agent prompts from hardcoded to database - Add grayscale preview support (DRAFT/ACTIVE distribution) - Implement 3-tier fallback (DB -> Cache -> Hardcoded) - Add version management and rollback capability Files changed: - backend/scripts/migrate-aia-prompts.ts (new migration script) - backend/src/common/prompt/prompt.fallbacks.ts (add AIA fallbacks) - backend/src/modules/aia/services/agentService.ts (integrate PromptService) - backend/src/modules/aia/services/conversationService.ts (pass userId) - backend/src/modules/aia/types/index.ts (fix AgentStage type) Documentation: - docs/03-业务模块/AIA-AI智能问答/06-开发记录/2026-01-18-Prompt管理系统集成.md - docs/02-通用能力层/00-通用能力层清单.md (add FileCard, Prompt management) - docs/00-系统总体设计/00-系统当前状态与开发指南.md (update to v3.6) Prompt codes: - AIA_SCIENTIFIC_QUESTION, AIA_PICO_ANALYSIS, AIA_TOPIC_EVALUATION - AIA_OUTCOME_DESIGN, AIA_CRF_DESIGN, AIA_SAMPLE_SIZE - AIA_PROTOCOL_WRITING, AIA_METHODOLOGY_REVIEW - AIA_PAPER_POLISH, AIA_PAPER_TRANSLATE Tested: Migration script executed, all 10 prompts inserted successfully
125 lines
3.6 KiB
TypeScript
125 lines
3.6 KiB
TypeScript
/**
|
|
* Prompt管理系统初始化脚本
|
|
*
|
|
* 功能:
|
|
* 1. 创建 capability_schema
|
|
* 2. 添加 prompt:* 权限
|
|
* 3. 更新角色权限分配
|
|
*/
|
|
|
|
import { PrismaClient } from '@prisma/client';
|
|
|
|
const prisma = new PrismaClient();
|
|
|
|
async function main() {
|
|
console.log('🚀 开始初始化 Prompt 管理系统...\n');
|
|
|
|
// 1. 创建 capability_schema
|
|
console.log('📁 Step 1: 创建 capability_schema...');
|
|
try {
|
|
await prisma.$executeRaw`CREATE SCHEMA IF NOT EXISTS capability_schema`;
|
|
console.log(' ✅ capability_schema 创建成功\n');
|
|
} catch (error) {
|
|
console.log(' ⚠️ capability_schema 可能已存在\n');
|
|
}
|
|
|
|
// 2. 添加 prompt:* 权限
|
|
console.log('🔐 Step 2: 添加 prompt:* 权限...');
|
|
|
|
const promptPermissions = [
|
|
{ code: 'prompt:view', name: '查看Prompt', description: '查看Prompt模板列表和详情', module: 'admin' },
|
|
{ code: 'prompt:edit', name: '编辑Prompt', description: '创建和修改Prompt草稿', module: 'admin' },
|
|
{ code: 'prompt:debug', name: '调试Prompt', description: '开启调试模式,在生产环境测试草稿', module: 'admin' },
|
|
{ code: 'prompt:publish', name: '发布Prompt', description: '将草稿发布为正式版', module: 'admin' },
|
|
];
|
|
|
|
for (const perm of promptPermissions) {
|
|
try {
|
|
await prisma.permissions.upsert({
|
|
where: { code: perm.code },
|
|
update: { name: perm.name, description: perm.description, module: perm.module },
|
|
create: perm,
|
|
});
|
|
console.log(` ✅ ${perm.code}`);
|
|
} catch (error) {
|
|
console.log(` ⚠️ ${perm.code} 添加失败:`, error);
|
|
}
|
|
}
|
|
console.log('');
|
|
|
|
// 3. 获取权限ID
|
|
console.log('🔗 Step 3: 更新角色权限分配...');
|
|
|
|
const permissions = await prisma.permissions.findMany({
|
|
where: { code: { startsWith: 'prompt:' } },
|
|
});
|
|
|
|
const permissionMap = new Map(permissions.map(p => [p.code, p.id]));
|
|
|
|
// SUPER_ADMIN: 全部权限
|
|
const superAdminPermissions = ['prompt:view', 'prompt:edit', 'prompt:debug', 'prompt:publish'];
|
|
for (const permCode of superAdminPermissions) {
|
|
const permId = permissionMap.get(permCode);
|
|
if (permId) {
|
|
try {
|
|
await prisma.role_permissions.upsert({
|
|
where: {
|
|
role_permission_id: { role: 'SUPER_ADMIN', permission_id: permId },
|
|
},
|
|
update: {},
|
|
create: { role: 'SUPER_ADMIN', permission_id: permId },
|
|
});
|
|
} catch (error) {
|
|
// 可能已存在
|
|
}
|
|
}
|
|
}
|
|
console.log(' ✅ SUPER_ADMIN: prompt:view, prompt:edit, prompt:debug, prompt:publish');
|
|
|
|
// PROMPT_ENGINEER: 无 publish 权限
|
|
const promptEngineerPermissions = ['prompt:view', 'prompt:edit', 'prompt:debug'];
|
|
for (const permCode of promptEngineerPermissions) {
|
|
const permId = permissionMap.get(permCode);
|
|
if (permId) {
|
|
try {
|
|
await prisma.role_permissions.upsert({
|
|
where: {
|
|
role_permission_id: { role: 'PROMPT_ENGINEER', permission_id: permId },
|
|
},
|
|
update: {},
|
|
create: { role: 'PROMPT_ENGINEER', permission_id: permId },
|
|
});
|
|
} catch (error) {
|
|
// 可能已存在
|
|
}
|
|
}
|
|
}
|
|
console.log(' ✅ PROMPT_ENGINEER: prompt:view, prompt:edit, prompt:debug (无publish)');
|
|
console.log('');
|
|
|
|
// 4. 验证
|
|
console.log('✅ Prompt 管理系统初始化完成!\n');
|
|
|
|
const allPermissions = await prisma.permissions.findMany({
|
|
where: { code: { startsWith: 'prompt:' } },
|
|
});
|
|
console.log('📋 已添加的权限:');
|
|
allPermissions.forEach(p => console.log(` - ${p.code}: ${p.name}`));
|
|
}
|
|
|
|
main()
|
|
.catch(console.error)
|
|
.finally(() => prisma.$disconnect());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|