Files
aiagent/backend/scripts/generate_android_log_agent.py
2026-03-06 22:31:41 +08:00

187 lines
6.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""
生成Android日志获取Agent
"""
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from app.core.database import SessionLocal
from app.models.agent import Agent
from app.models.workflow import Workflow
import uuid
from datetime import datetime
def generate_android_log_agent():
"""生成Android日志获取Agent"""
db = SessionLocal()
try:
# 检查是否已存在
existing = db.query(Agent).filter(Agent.name == "Android日志获取助手").first()
if existing:
print(f"Agent 'Android日志获取助手' 已存在ID: {existing.id}")
return existing.id
# 创建工作流
workflow_id = str(uuid.uuid4())
nodes = [
{
"id": "start",
"type": "start",
"position": {"x": 100, "y": 200},
"data": {"label": "开始"}
},
{
"id": "intent-recognize",
"type": "llm",
"position": {"x": 400, "y": 200},
"data": {
"label": "意图识别",
"provider": "deepseek",
"model": "deepseek-chat",
"temperature": 0.3,
"max_tokens": 200,
"prompt": """分析用户请求,提取以下信息:
1. 命令类型logcat获取日志、devices列出设备、shell执行shell命令
2. 日志标签过滤(如果有)
3. 日志级别过滤V/D/I/W/E/F/S如果有
4. 最大行数默认100
用户请求:{{input.query}}
请以JSON格式返回
{
"command": "logcat|devices|shell",
"filter_tag": "标签或shell命令可选",
"level": "V|D|I|W|E|F|S可选",
"max_lines": 100
}"""
}
},
{
"id": "json-parse",
"type": "json",
"position": {"x": 700, "y": 200},
"data": {
"label": "解析参数",
"operation": "parse",
"json_path": "$"
}
},
{
"id": "llm-with-tools",
"type": "llm",
"position": {"x": 1000, "y": 200},
"data": {
"label": "执行ADB命令",
"provider": "deepseek",
"model": "deepseek-chat",
"temperature": 0.7,
"max_tokens": 2000,
"enable_tools": True,
"selected_tools": ["adb_log"],
"prompt": """根据解析的参数使用adb_log工具获取Android设备日志。
参数:
- command: {{json-parse.output.command}}
- filter_tag: {{json-parse.output.filter_tag}}
- level: {{json-parse.output.level}}
- max_lines: {{json-parse.output.max_lines}}
请调用adb_log工具获取日志然后分析日志内容提取关键信息并总结。"""
}
},
{
"id": "end",
"type": "end",
"position": {"x": 1300, "y": 200},
"data": {"label": "结束"}
}
]
edges = [
{
"id": "e1",
"source": "start",
"target": "intent-recognize",
"sourceHandle": "right",
"targetHandle": "left"
},
{
"id": "e2",
"source": "intent-recognize",
"target": "json-parse",
"sourceHandle": "right",
"targetHandle": "left"
},
{
"id": "e3",
"source": "json-parse",
"target": "llm-with-tools",
"sourceHandle": "right",
"targetHandle": "left"
},
{
"id": "e4",
"source": "llm-with-tools",
"target": "end",
"sourceHandle": "right",
"targetHandle": "left"
}
]
workflow = Workflow(
id=workflow_id,
name="Android日志获取工作流",
description="通过ADB命令获取Android设备日志的工作流",
nodes=nodes,
edges=edges,
created_at=datetime.now(),
updated_at=datetime.now()
)
db.add(workflow)
db.commit()
db.refresh(workflow)
# 创建Agent
agent_id = str(uuid.uuid4())
agent = Agent(
id=agent_id,
name="Android日志获取助手",
description="通过ADB命令获取和分析Android设备日志的智能助手。支持获取logcat日志、列出设备、执行shell命令等功能。",
workflow_config={
"workflow_id": workflow_id,
"nodes": nodes,
"edges": edges
},
created_at=datetime.now(),
updated_at=datetime.now()
)
db.add(agent)
db.commit()
db.refresh(agent)
print(f"✅ Android日志获取Agent创建成功")
print(f" Agent ID: {agent_id}")
print(f" Agent名称: {agent.name}")
print(f" 工作流ID: {workflow_id}")
print(f"\n使用示例:")
print(f" python3 test_workflow_tool.py -a \"Android日志获取助手\" -i '{{\"query\": \"获取最近的错误日志\"}}'")
return agent_id
except Exception as e:
db.rollback()
print(f"❌ 创建Agent失败: {e}")
import traceback
traceback.print_exc()
return None
finally:
db.close()
if __name__ == "__main__":
generate_android_log_agent()