🔧 修复:扫描龙虾显示正确的名字和 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/?instance_id=xxx&username=xxx - 扫描指定实例
|
||||||
GET /api/v1/user/scan-local-agents/ - 扫描所有实例
|
GET /api/v1/user/scan-local-agents/?username=xxx - 扫描所有实例
|
||||||
"""
|
"""
|
||||||
def get(self, request):
|
def get(self, request):
|
||||||
instance_id = request.query_params.get('instance_id')
|
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:
|
if not instance_id:
|
||||||
# 返回所有已注册实例的 Agent
|
# 返回所有已注册实例的 Agent
|
||||||
@@ -214,8 +227,11 @@ class ScanLocalAgentsView(views.APIView):
|
|||||||
agents = []
|
agents = []
|
||||||
for inst in instances:
|
for inst in instances:
|
||||||
for agent_id in inst.agent_ids:
|
for agent_id in inst.agent_ids:
|
||||||
|
agent_info = user_agents.get(agent_id, {})
|
||||||
agents.append({
|
agents.append({
|
||||||
'agent_id': agent_id,
|
'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_id': inst.instance_id,
|
||||||
'instance_name': inst.instance_name
|
'instance_name': inst.instance_name
|
||||||
})
|
})
|
||||||
@@ -224,11 +240,15 @@ class ScanLocalAgentsView(views.APIView):
|
|||||||
from instances.models import Instance
|
from instances.models import Instance
|
||||||
try:
|
try:
|
||||||
instance = Instance.objects.get(instance_id=instance_id, is_active=True)
|
instance = Instance.objects.get(instance_id=instance_id, is_active=True)
|
||||||
agents = [{
|
for agent_id in instance.agent_ids:
|
||||||
'agent_id': agent_id,
|
agent_info = user_agents.get(agent_id, {})
|
||||||
'instance_id': instance.instance_id,
|
agents.append({
|
||||||
'instance_name': instance.instance_name
|
'agent_id': agent_id,
|
||||||
} for agent_id in instance.agent_ids]
|
'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:
|
except Instance.DoesNotExist:
|
||||||
return Response({'error': '实例不存在'}, status=status.HTTP_404_NOT_FOUND)
|
return Response({'error': '实例不存在'}, status=status.HTTP_404_NOT_FOUND)
|
||||||
|
|
||||||
|
|||||||
@@ -21,12 +21,15 @@ function LoginPage() {
|
|||||||
|
|
||||||
// 扫描本机龙虾
|
// 扫描本机龙虾
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
scanAgents();
|
if (username) {
|
||||||
}, []);
|
scanAgents();
|
||||||
|
}
|
||||||
|
}, [username]);
|
||||||
|
|
||||||
const scanAgents = async () => {
|
const scanAgents = async () => {
|
||||||
try {
|
try {
|
||||||
const res = await axios.get(`${API_BASE}/user/scan-local-agents/`);
|
// 传递 username 参数,获取绑定的龙虾信息
|
||||||
|
const res = await axios.get(`${API_BASE}/user/scan-local-agents/?username=${username}`);
|
||||||
setAgents(res.data.agents || []);
|
setAgents(res.data.agents || []);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('扫描龙虾失败:', error);
|
console.error('扫描龙虾失败:', error);
|
||||||
|
|||||||
Reference in New Issue
Block a user