2026-04-01 20:52:03 +08:00
|
|
|
"""
|
2026-04-03 19:14:21 +08:00
|
|
|
API views for Agent Diary monitoring.
|
2026-04-01 20:52:03 +08:00
|
|
|
"""
|
|
|
|
|
from rest_framework.decorators import api_view
|
|
|
|
|
from rest_framework.response import Response
|
|
|
|
|
from datetime import datetime
|
2026-04-03 19:14:21 +08:00
|
|
|
from agents.models import Agent, AgentDiary
|
2026-04-01 20:52:03 +08:00
|
|
|
|
|
|
|
|
@api_view(['GET'])
|
2026-04-03 19:14:21 +08:00
|
|
|
def agent_list(request):
|
|
|
|
|
"""获取所有 Agent 状态"""
|
|
|
|
|
agents = Agent.objects.all()
|
2026-04-02 19:16:45 +08:00
|
|
|
result = []
|
2026-04-03 19:14:21 +08:00
|
|
|
for agent in agents:
|
2026-04-02 19:16:45 +08:00
|
|
|
result.append({
|
2026-04-03 19:14:21 +08:00
|
|
|
'id': agent.id,
|
|
|
|
|
'name': agent.name,
|
|
|
|
|
'emoji': agent.emoji,
|
|
|
|
|
'port': agent.port,
|
|
|
|
|
'specialty': agent.specialty,
|
|
|
|
|
'container': agent.container,
|
|
|
|
|
'app_name': agent.app_name,
|
|
|
|
|
'app_id': agent.app_id,
|
2026-04-02 19:16:45 +08:00
|
|
|
'status': 'healthy',
|
2026-04-01 20:52:03 +08:00
|
|
|
'last_check': datetime.now().isoformat()
|
|
|
|
|
})
|
2026-04-02 19:16:45 +08:00
|
|
|
return Response(result)
|
2026-04-01 20:52:03 +08:00
|
|
|
|
|
|
|
|
@api_view(['GET'])
|
2026-04-03 19:14:21 +08:00
|
|
|
def agent_detail(request, agent_id):
|
|
|
|
|
"""获取单个 Agent 详情"""
|
2026-04-02 19:16:45 +08:00
|
|
|
try:
|
2026-04-03 19:14:21 +08:00
|
|
|
agent = Agent.objects.get(id=agent_id)
|
2026-04-02 19:16:45 +08:00
|
|
|
return Response({
|
2026-04-03 19:14:21 +08:00
|
|
|
'id': agent.id,
|
|
|
|
|
'name': agent.name,
|
|
|
|
|
'emoji': agent.emoji,
|
|
|
|
|
'port': agent.port,
|
|
|
|
|
'specialty': agent.specialty,
|
|
|
|
|
'container': agent.container,
|
|
|
|
|
'app_name': agent.app_name,
|
|
|
|
|
'app_id': agent.app_id,
|
2026-04-02 19:16:45 +08:00
|
|
|
'status': 'healthy',
|
2026-04-03 19:14:21 +08:00
|
|
|
'workspace': f'/home/node/.openclaw/workspace/{agent.workspace}',
|
2026-04-02 19:16:45 +08:00
|
|
|
'last_check': datetime.now().isoformat()
|
|
|
|
|
})
|
2026-04-03 19:14:21 +08:00
|
|
|
except Agent.DoesNotExist:
|
|
|
|
|
return Response({'error': 'Agent 不存在'}, status=404)
|
2026-04-01 20:52:03 +08:00
|
|
|
|
|
|
|
|
@api_view(['GET'])
|
|
|
|
|
def tools_list(request):
|
|
|
|
|
"""获取工具列表"""
|
|
|
|
|
tools = [
|
|
|
|
|
{
|
|
|
|
|
'name': 'Git 版本控制',
|
|
|
|
|
'status': 'running',
|
|
|
|
|
'description': '代码版本管理服务',
|
|
|
|
|
'url': 'https://xjp.datalibstar.com/flying-hero/openclaw-monitor.git'
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
return Response(tools)
|
2026-04-01 22:36:06 +08:00
|
|
|
|
|
|
|
|
@api_view(['GET'])
|
2026-04-03 19:14:21 +08:00
|
|
|
def agent_memory_dates(request, agent_id):
|
|
|
|
|
"""获取 Agent 有工作记忆的日期列表 - 从数据库读取"""
|
2026-04-03 18:07:13 +08:00
|
|
|
try:
|
2026-04-03 19:14:21 +08:00
|
|
|
agent = Agent.objects.get(id=agent_id)
|
|
|
|
|
except Agent.DoesNotExist:
|
|
|
|
|
return Response({'error': 'Agent 不存在'}, status=404)
|
2026-04-01 22:36:06 +08:00
|
|
|
|
2026-04-03 18:07:13 +08:00
|
|
|
# 从数据库查询工作记忆
|
2026-04-03 19:14:21 +08:00
|
|
|
diaries = AgentDiary.objects.filter(
|
|
|
|
|
agent=agent,
|
2026-04-03 18:07:13 +08:00
|
|
|
category='memory'
|
2026-04-03 19:14:21 +08:00
|
|
|
).values_list('date', flat=True).distinct().order_by('-date')
|
2026-04-01 22:36:06 +08:00
|
|
|
|
2026-04-03 18:07:13 +08:00
|
|
|
dates = [str(date) for date in sorted(diaries, reverse=True)]
|
2026-04-01 22:36:06 +08:00
|
|
|
return Response({'dates': dates})
|
|
|
|
|
|
|
|
|
|
@api_view(['GET'])
|
2026-04-03 19:14:21 +08:00
|
|
|
def agent_memory_detail(request, agent_id, date):
|
2026-04-03 18:07:13 +08:00
|
|
|
"""获取指定日期的工作记忆内容 - 从数据库读取"""
|
|
|
|
|
try:
|
2026-04-03 19:14:21 +08:00
|
|
|
agent = Agent.objects.get(id=agent_id)
|
|
|
|
|
except Agent.DoesNotExist:
|
|
|
|
|
return Response({'error': 'Agent 不存在'}, status=404)
|
2026-04-01 22:36:06 +08:00
|
|
|
|
2026-04-03 18:07:13 +08:00
|
|
|
# 从数据库查询工作记忆
|
|
|
|
|
try:
|
2026-04-03 19:14:21 +08:00
|
|
|
diary = AgentDiary.objects.get(
|
|
|
|
|
agent=agent,
|
2026-04-03 18:07:13 +08:00
|
|
|
date=date,
|
|
|
|
|
category='memory'
|
|
|
|
|
)
|
|
|
|
|
return Response({
|
|
|
|
|
'date': str(diary.date),
|
|
|
|
|
'content': diary.content,
|
|
|
|
|
'title': diary.title,
|
|
|
|
|
'tags': diary.tags,
|
|
|
|
|
})
|
2026-04-03 19:14:21 +08:00
|
|
|
except AgentDiary.DoesNotExist:
|
2026-04-03 18:07:13 +08:00
|
|
|
return Response({'error': '该日期没有工作记忆'}, status=404)
|
2026-04-02 13:27:48 +08:00
|
|
|
|
|
|
|
|
@api_view(['GET'])
|
2026-04-03 19:14:21 +08:00
|
|
|
def agent_diary_dates(request, agent_id):
|
|
|
|
|
"""获取 Agent 有日记(成长之路)的日期列表 - 从数据库读取"""
|
2026-04-03 17:38:18 +08:00
|
|
|
try:
|
2026-04-03 19:14:21 +08:00
|
|
|
agent = Agent.objects.get(id=agent_id)
|
|
|
|
|
except Agent.DoesNotExist:
|
|
|
|
|
return Response({'error': 'Agent 不存在'}, status=404)
|
2026-04-02 13:27:48 +08:00
|
|
|
|
2026-04-03 17:38:18 +08:00
|
|
|
# 从数据库查询日记日期
|
2026-04-03 19:14:21 +08:00
|
|
|
diaries = AgentDiary.objects.filter(
|
|
|
|
|
agent=agent,
|
2026-04-03 17:38:18 +08:00
|
|
|
category='chengcai'
|
2026-04-03 19:14:21 +08:00
|
|
|
).values_list('date', flat=True).distinct().order_by('-date')
|
2026-04-02 13:27:48 +08:00
|
|
|
|
2026-04-03 17:38:18 +08:00
|
|
|
dates = [str(date) for date in sorted(diaries, reverse=True)]
|
2026-04-02 13:27:48 +08:00
|
|
|
return Response({'dates': dates})
|
|
|
|
|
|
|
|
|
|
@api_view(['GET'])
|
2026-04-03 19:14:21 +08:00
|
|
|
def agent_diary_detail(request, agent_id, date):
|
|
|
|
|
"""获取指定日期的日记内容(成长之路) - 从数据库读取"""
|
2026-04-03 17:38:18 +08:00
|
|
|
try:
|
2026-04-03 19:14:21 +08:00
|
|
|
agent = Agent.objects.get(id=agent_id)
|
|
|
|
|
except Agent.DoesNotExist:
|
|
|
|
|
return Response({'error': 'Agent 不存在'}, status=404)
|
2026-04-02 13:27:48 +08:00
|
|
|
|
2026-04-03 17:38:18 +08:00
|
|
|
# 从数据库查询日记
|
|
|
|
|
try:
|
2026-04-03 19:14:21 +08:00
|
|
|
diary = AgentDiary.objects.get(
|
|
|
|
|
agent=agent,
|
2026-04-03 17:38:18 +08:00
|
|
|
date=date,
|
|
|
|
|
category='chengcai'
|
|
|
|
|
)
|
|
|
|
|
return Response({
|
|
|
|
|
'date': str(diary.date),
|
|
|
|
|
'content': diary.content,
|
|
|
|
|
'title': diary.title,
|
|
|
|
|
'tags': diary.tags,
|
|
|
|
|
})
|
2026-04-03 19:14:21 +08:00
|
|
|
except AgentDiary.DoesNotExist:
|
2026-04-02 13:27:48 +08:00
|
|
|
return Response({'error': '该日期没有日记'}, status=404)
|