#!/usr/bin/env python3 """ 生成Android应用开发助手Agent 这是一个专门用于Android应用开发的Agent,能够帮助开发者快速生成、优化和调试Android应用 """ import sys import os sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from sqlalchemy.orm import Session from app.core.database import SessionLocal from app.models.agent import Agent from app.models.user import User from datetime import datetime import uuid def generate_android_agent(db: Session, username: str = "admin"): """生成Android应用开发助手Agent""" print("=" * 60) print("生成Android应用开发助手Agent") print("=" * 60) print() # 查找用户 user = db.query(User).filter(User.username == username).first() if not user: print(f"❌ 未找到用户 '{username}',请先创建该用户") return print(f"✅ 找到用户: {user.username} (ID: {user.id})") print() # 生成Android应用开发助手工作流配置 # 工作流结构: # 开始 -> 需求分析 -> 需求分类 -> [代码生成|架构设计|问题诊断|性能优化] -> 结果整合 -> 格式化输出 -> 结束 nodes = [] edges = [] # 1. 开始节点 start_node = { "id": "start-1", "type": "start", "position": {"x": 50, "y": 400}, "data": { "label": "开始", "output_format": "json" } } nodes.append(start_node) # 2. 需求理解与分析节点(LLM节点) requirement_analysis_node = { "id": "llm-requirement-analysis", "type": "llm", "position": {"x": 250, "y": 400}, "data": { "label": "需求理解与分析", "provider": "deepseek", "model": "deepseek-chat", "temperature": "0.3", "max_tokens": "2000", "prompt": """你是一个专业的Android开发顾问。请分析用户的需求,提取以下信息: 用户需求:{{query}} 请以JSON格式输出分析结果: { "app_type": "应用类型(工具类、社交类、电商类、游戏类等)", "core_modules": ["模块1", "模块2", ...], "target_users": "目标用户群体", "complexity": "简单|中等|复杂", "estimated_weeks": 数字, "tech_stack": ["技术1", "技术2", ...], "summary": "需求摘要" } 请确保输出是有效的JSON格式。""" } } nodes.append(requirement_analysis_node) edges.append({ "id": "e1", "source": "start-1", "target": "llm-requirement-analysis" }) # 3. 数据准备节点(Transform节点)- 用于传递数据 data_prepare_node = { "id": "transform-prepare", "type": "transform", "position": {"x": 450, "y": 400}, "data": { "label": "准备数据", "mode": "merge", "mapping": { "requirement_analysis": "{{output}}", "user_query": "{{query}}" } } } nodes.append(data_prepare_node) edges.append({ "id": "e2", "source": "llm-requirement-analysis", "target": "transform-prepare" }) # 4. 需求分类节点(条件节点) classify_condition = { "id": "condition-classify", "type": "condition", "position": {"x": 650, "y": 400}, "data": { "label": "需求分类", "condition": "{user_query} contains '生成' or {user_query} contains '创建' or {user_query} contains '写代码' or {user_query} contains '代码'" } } nodes.append(classify_condition) edges.append({ "id": "e3", "source": "transform-prepare", "target": "condition-classify" }) # 5. 代码生成节点(LLM节点)- true分支 code_generation_node = { "id": "llm-code-generation", "type": "llm", "position": {"x": 850, "y": 300}, "data": { "label": "代码生成", "provider": "deepseek", "model": "deepseek-chat", "temperature": "0.2", "max_tokens": "4000", "prompt": """你是一个专业的Android开发工程师。根据以下需求分析,生成完整的Android代码。 用户原始需求:{{user_query}} 需求分析结果:{{requirement_analysis}} 请生成以下内容: 1. Java/Kotlin代码(包含必要的注释) 2. 布局XML文件 3. 资源文件配置(如需要) 4. 依赖配置(build.gradle,如需要) 5. 使用说明 代码要求: - 遵循Android开发最佳实践 - 使用现代Android架构(推荐MVVM) - 包含错误处理 - 代码注释清晰 - 支持Android API 24+ 请以Markdown格式输出,包含代码块,使用正确的语言标识(java、kotlin、xml等)。""" } } nodes.append(code_generation_node) edges.append({ "id": "e4-true", "source": "condition-classify", "target": "llm-code-generation", "sourceHandle": "true" }) # 6. 架构设计节点(LLM节点)- false分支(当不是代码生成时) architecture_node = { "id": "llm-architecture", "type": "llm", "position": {"x": 850, "y": 500}, "data": { "label": "架构设计/问题诊断/性能优化", "provider": "deepseek", "model": "deepseek-chat", "temperature": "0.3", "max_tokens": "3000", "prompt": """你是一个Android开发专家。根据用户需求,提供专业的开发建议。 用户原始需求:{{user_query}} 需求分析结果:{{requirement_analysis}} 请根据需求类型提供相应的帮助: **如果是架构设计需求**,请提供: 1. 架构模式选择(MVP/MVVM/MVI)及理由 2. 模块划分方案 3. 技术栈选型(框架、库、工具) 4. 目录结构设计 5. 数据流设计 6. 扩展性考虑 **如果是问题诊断需求**(包含错误、崩溃、问题、bug等关键词),请提供: 1. 问题类型(崩溃/ANR/内存泄漏/网络错误等) 2. 根本原因分析 3. 解决方案(步骤清晰) 4. 修复代码示例 5. 预防措施 **如果是性能优化需求**(包含优化、性能、速度、卡顿等关键词),请提供: 1. 性能瓶颈分析 2. 优化方案(按优先级排序) 3. 具体优化代码 4. 优化前后对比 5. 性能测试建议 请以Markdown格式输出,确保内容专业、清晰、实用。""" } } nodes.append(architecture_node) edges.append({ "id": "e4-false", "source": "condition-classify", "target": "llm-architecture", "sourceHandle": "false" }) # 7. 结果整合节点(Transform节点) integration_node = { "id": "transform-integration", "type": "transform", "position": {"x": 1050, "y": 400}, "data": { "label": "结果整合", "mode": "merge", "mapping": { "requirement_analysis": "{{requirement_analysis}}", "user_query": "{{user_query}}", "solution": "{{output}}", "agent_type": "Android应用开发助手", "timestamp": "{{$timestamp}}" } } } nodes.append(integration_node) # 连接代码生成节点到整合节点 edges.append({ "id": "e5-code", "source": "llm-code-generation", "target": "transform-integration" }) # 连接架构设计节点到整合节点 edges.append({ "id": "e5-arch", "source": "llm-architecture", "target": "transform-integration" }) # 8. 格式化输出节点(LLM节点) format_node = { "id": "llm-format", "type": "llm", "position": {"x": 1250, "y": 400}, "data": { "label": "格式化输出", "provider": "deepseek", "model": "deepseek-chat", "temperature": "0.1", "max_tokens": "4000", "prompt": """请将以下Android开发方案整理成专业的Markdown文档格式。 方案内容:{{input}} 文档结构: 1. **需求分析摘要** - 应用类型 - 核心模块 - 技术复杂度 - 开发周期估算 2. **解决方案** - 详细说明 - 代码示例(如有) - 架构设计(如有) 3. **实施步骤** - 步骤清晰的实施指南 4. **注意事项** - 重要提醒 - 常见问题 5. **参考资料** - 相关文档链接 请确保格式清晰、代码高亮正确、结构完整。如果输入中已经包含格式良好的内容,请保持原有格式并适当优化。""" } } nodes.append(format_node) edges.append({ "id": "e6", "source": "transform-integration", "target": "llm-format" }) # 9. 结束节点 end_node = { "id": "end-1", "type": "end", "position": {"x": 1450, "y": 400}, "data": { "label": "结束", "description": "返回最终结果" } } nodes.append(end_node) edges.append({ "id": "e7", "source": "llm-format", "target": "end-1" }) # 创建或更新Agent workflow_config = { "nodes": nodes, "edges": edges } agent = db.query(Agent).filter( Agent.name == "Android应用开发助手", Agent.user_id == user.id ).first() if agent: agent.workflow_config = workflow_config agent.description = "帮助开发者快速生成、优化和调试Android应用。支持代码生成、架构设计、问题诊断、性能优化等功能。" agent.updated_at = datetime.now() agent.status = "published" # 设置为已发布状态,可直接使用 print("⚠️ Agent 'Android应用开发助手' 已存在,将更新它...") else: agent = Agent( id=str(uuid.uuid4()), name="Android应用开发助手", description="帮助开发者快速生成、优化和调试Android应用。支持代码生成、架构设计、问题诊断、性能优化等功能。", workflow_config=workflow_config, status="published", # 直接设置为已发布状态,可立即使用 user_id=user.id, version=1 ) db.add(agent) try: db.commit() db.refresh(agent) print() print("✅ Agent创建/更新成功!") print() print(f"📋 Agent信息:") print(f" - ID: {agent.id}") print(f" - 名称: {agent.name}") print(f" - 状态: {agent.status} (已发布,可直接使用)") print(f" - 版本: {agent.version}") print(f" - 节点数: {len(nodes)}") print(f" - 连接数: {len(edges)}") print() print("🎯 功能特性:") print(" ✅ 需求分析与理解") print(" ✅ 代码生成(Activity、ViewModel、Repository等)") print(" ✅ 架构设计建议(MVP/MVVM/MVI)") print(" ✅ 问题诊断与修复") print(" ✅ 性能优化建议") print(" ✅ 格式化输出") print() print("💡 使用提示:") print(" 1. 在Agent管理页面找到 'Android应用开发助手'") print(" 2. 点击 '使用' 按钮开始使用") print(" 3. 输入你的Android开发需求,例如:") print(" - '帮我生成一个登录功能的代码'") print(" - '设计一个电商应用的架构'") print(" - '我的应用崩溃了,错误信息是...'") print(" - '如何优化列表页面的滚动性能?'") print() return agent except Exception as e: db.rollback() print(f"❌ 创建Agent失败: {str(e)}") import traceback traceback.print_exc() return None def main(): """主函数""" db = SessionLocal() try: generate_android_agent(db, username="admin") finally: db.close() if __name__ == "__main__": main()