🔧 修复:扫描龙虾显示正确的名字和 emoji
问题: - 扫描 API 只返回 agent_id 和 instance_name - 没有返回 agent_name 和 agent_emoji - 导致所有龙虾都显示成 🤖 修复: - 扫描 API 增加 username 参数 - 从 user.linked_agents 获取 agent_name/emoji - 前端在 username 变化时重新扫描 结果: ✅ flying_hero → 飞行侠 🦸 ✅ lobster_monitor → 龙虾监控 🦞
This commit is contained in:
@@ -201,11 +201,24 @@ class ScanLocalAgentsView(views.APIView):
|
||||
"""
|
||||
扫描本机龙虾列表
|
||||
|
||||
GET /api/v1/user/scan-local-agents/?instance_id=xxx - 扫描指定实例
|
||||
GET /api/v1/user/scan-local-agents/ - 扫描所有实例
|
||||
GET /api/v1/user/scan-local-agents/?instance_id=xxx&username=xxx - 扫描指定实例
|
||||
GET /api/v1/user/scan-local-agents/?username=xxx - 扫描所有实例
|
||||
"""
|
||||
def get(self, request):
|
||||
instance_id = request.query_params.get('instance_id')
|
||||
username = request.query_params.get('username')
|
||||
|
||||
# 获取用户绑定的龙虾信息(用于获取 agent_name 和 emoji)
|
||||
user_agents = {}
|
||||
if username:
|
||||
from django.contrib.auth import get_user_model
|
||||
User = get_user_model()
|
||||
try:
|
||||
user = User.objects.get(username=username)
|
||||
for agent in user.linked_agents:
|
||||
user_agents[agent['agent_id']] = agent
|
||||
except User.DoesNotExist:
|
||||
pass
|
||||
|
||||
if not instance_id:
|
||||
# 返回所有已注册实例的 Agent
|
||||
@@ -214,8 +227,11 @@ class ScanLocalAgentsView(views.APIView):
|
||||
agents = []
|
||||
for inst in instances:
|
||||
for agent_id in inst.agent_ids:
|
||||
agent_info = user_agents.get(agent_id, {})
|
||||
agents.append({
|
||||
'agent_id': agent_id,
|
||||
'agent_name': agent_info.get('agent_name', agent_id),
|
||||
'agent_emoji': agent_info.get('agent_emoji', '🤖'),
|
||||
'instance_id': inst.instance_id,
|
||||
'instance_name': inst.instance_name
|
||||
})
|
||||
@@ -224,11 +240,15 @@ class ScanLocalAgentsView(views.APIView):
|
||||
from instances.models import Instance
|
||||
try:
|
||||
instance = Instance.objects.get(instance_id=instance_id, is_active=True)
|
||||
agents = [{
|
||||
'agent_id': agent_id,
|
||||
'instance_id': instance.instance_id,
|
||||
'instance_name': instance.instance_name
|
||||
} for agent_id in instance.agent_ids]
|
||||
for agent_id in instance.agent_ids:
|
||||
agent_info = user_agents.get(agent_id, {})
|
||||
agents.append({
|
||||
'agent_id': agent_id,
|
||||
'agent_name': agent_info.get('agent_name', agent_id),
|
||||
'agent_emoji': agent_info.get('agent_emoji', '🤖'),
|
||||
'instance_id': instance.instance_id,
|
||||
'instance_name': instance.instance_name
|
||||
})
|
||||
except Instance.DoesNotExist:
|
||||
return Response({'error': '实例不存在'}, status=status.HTTP_404_NOT_FOUND)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user