feat: 实现 AI-First 代理系统
核心功能: - AIAgent 模型:AI 代理身份管理 - AIOperationLog: AI 操作日志记录 - AITask: AI 异步任务系统 - AIWebhook: AI webhook 订阅 API 端点: - POST /api/agents/auth/ - AI 代理认证 - GET/POST /api/agents/ - 代理管理 - GET /api/agent-logs/ - 操作日志查询 - GET/POST /api/agent-tasks/ - 任务管理 - GET/POST /api/agent-webhooks/ - Webhook 管理 - POST /api/batch/ - 批量操作 预置 AI 代理: - content-moderator-ai: 内容审核 AI - content-generator-ai: 内容生成 AI - service-curator-ai: 服务推荐 AI - analytics-ai: 数据分析 AI - admin-ai: 管理员 AI 文档: - AI_AGENT.md: AI-First 设计文档 - init_agents.py: AI 代理初始化脚本 测试: - 认证系统测试通过 - JWT token 生成正常 - 权限系统工作正常
This commit is contained in:
94
city-manual/backend/init_agents.py
Normal file
94
city-manual/backend/init_agents.py
Normal file
@@ -0,0 +1,94 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
初始化 AI 代理 - 创建默认的 AI 代理账号
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import django
|
||||
|
||||
# 设置 Django 环境
|
||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'city_manual.settings')
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
django.setup()
|
||||
|
||||
from agents.models import AIAgent
|
||||
|
||||
def create_default_agents():
|
||||
"""创建默认的 AI 代理"""
|
||||
|
||||
agents = [
|
||||
{
|
||||
'agent_id': 'content-moderator-ai',
|
||||
'name': '内容审核 AI',
|
||||
'description': '负责审核用户提交的文章和服务内容',
|
||||
'permissions': ['read', 'review', 'approve', 'write'],
|
||||
'rate_limit': 1000,
|
||||
},
|
||||
{
|
||||
'agent_id': 'content-generator-ai',
|
||||
'name': '内容生成 AI',
|
||||
'description': '自动生成城市介绍、旅游攻略等内容',
|
||||
'permissions': ['read', 'write'],
|
||||
'rate_limit': 100,
|
||||
},
|
||||
{
|
||||
'agent_id': 'service-curator-ai',
|
||||
'name': '服务推荐 AI',
|
||||
'description': '自动发现和推荐本地特色服务',
|
||||
'permissions': ['read', 'write'],
|
||||
'rate_limit': 100,
|
||||
},
|
||||
{
|
||||
'agent_id': 'analytics-ai',
|
||||
'name': '数据分析 AI',
|
||||
'description': '分析用户行为和平台数据',
|
||||
'permissions': ['read', 'analytics'],
|
||||
'rate_limit': 500,
|
||||
},
|
||||
{
|
||||
'agent_id': 'admin-ai',
|
||||
'name': '管理员 AI',
|
||||
'description': '全自动管理员,拥有所有权限',
|
||||
'permissions': ['read', 'write', 'review', 'delete', 'batch', 'analytics'],
|
||||
'rate_limit': 10000,
|
||||
},
|
||||
]
|
||||
|
||||
import uuid
|
||||
|
||||
for agent_data in agents:
|
||||
agent, created = AIAgent.objects.get_or_create(
|
||||
agent_id=agent_data['agent_id'],
|
||||
defaults={
|
||||
'name': agent_data['name'],
|
||||
'description': agent_data['description'],
|
||||
'secret_key': uuid.uuid4().hex,
|
||||
'permissions': agent_data['permissions'],
|
||||
'rate_limit': agent_data['rate_limit'],
|
||||
}
|
||||
)
|
||||
|
||||
if created:
|
||||
print(f"✅ 创建 AI 代理:{agent.name} ({agent.agent_id})")
|
||||
print(f" 密钥:{agent.secret_key}")
|
||||
print(f" 权限:{', '.join(agent.permissions)}")
|
||||
print()
|
||||
else:
|
||||
print(f"⚠️ AI 代理已存在:{agent.name}")
|
||||
print(f" 密钥:{agent.secret_key}")
|
||||
print()
|
||||
|
||||
if __name__ == '__main__':
|
||||
print("=" * 60)
|
||||
print("🤖 初始化 AI 代理系统")
|
||||
print("=" * 60)
|
||||
print()
|
||||
|
||||
create_default_agents()
|
||||
|
||||
print("=" * 60)
|
||||
print("✅ AI 代理初始化完成!")
|
||||
print()
|
||||
print("⚠️ 请妥善保管密钥,用于 AI 代理认证")
|
||||
print("=" * 60)
|
||||
Reference in New Issue
Block a user