fix(chat): 智能体支持配置分级显示
This commit is contained in:
1 parent
a451951e7a
commit
70d7435b5f
27 files changed
+645
-76
No files matched your search
@@ -0,0 +1,105 @@
|
||||
"""
|
||||
services/agent_category.py
|
||||
|
||||
用途:从 Agent.extra_config 解析 P1 分类字段(category / sub_category / sort_order),并提供排序与序列化。
|
||||
依赖:无外部服务;由 api/agents、api/user 在返回列表时调用。
|
||||
逻辑:分类信息暂存 extra_config,P2 可迁移至独立 agent_categories 表。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
|
||||
def parse_agent_category_meta(extra_config: dict | None) -> dict[str, Any]:
|
||||
"""
|
||||
从 extra_config 解析 Agent 分类元数据。
|
||||
核心逻辑:category / sub_category 去首尾空白;sort_order 非法值回退为 0。
|
||||
|
||||
Args:
|
||||
extra_config: Agent.extra_config JSON 字典,可为 None。
|
||||
|
||||
Returns:
|
||||
含 category (str)、sub_category (str)、sort_order (int) 的字典。
|
||||
"""
|
||||
extra = extra_config if isinstance(extra_config, dict) else {}
|
||||
category = str(extra.get('category') or '').strip()
|
||||
sub_category = str(extra.get('sub_category') or '').strip()
|
||||
sort_raw = extra.get('sort_order')
|
||||
try:
|
||||
sort_order = int(sort_raw) if sort_raw is not None and sort_raw != '' else 0
|
||||
except (TypeError, ValueError):
|
||||
sort_order = 0
|
||||
return {
|
||||
'category': category,
|
||||
'sub_category': sub_category,
|
||||
'sort_order': sort_order,
|
||||
}
|
||||
|
||||
|
||||
def _row_extra_config(row: Any) -> dict | None:
|
||||
"""从 ORM 行或 dict 行读取 extra_config。"""
|
||||
if hasattr(row, 'extra_config'):
|
||||
return row.extra_config
|
||||
if isinstance(row, dict):
|
||||
return row.get('extra_config')
|
||||
return None
|
||||
|
||||
|
||||
def _row_id(row: Any) -> int:
|
||||
"""从 ORM 行或 dict 行读取主键 id。"""
|
||||
if hasattr(row, 'id'):
|
||||
return int(row.id)
|
||||
if isinstance(row, dict):
|
||||
return int(row.get('id') or 0)
|
||||
return 0
|
||||
|
||||
|
||||
def agent_category_sort_key(row: Any) -> tuple:
|
||||
"""
|
||||
生成分类排序键:sort_order → category → sub_category → id。
|
||||
核心逻辑:未配置 category 的 Agent 排在同 sort_order 下的末尾(用 \\uffff 占位)。
|
||||
"""
|
||||
meta = parse_agent_category_meta(_row_extra_config(row))
|
||||
category_key = meta['category'] or '\uffff'
|
||||
sub_key = meta['sub_category'] or '\uffff'
|
||||
return (meta['sort_order'], category_key, sub_key, _row_id(row))
|
||||
|
||||
|
||||
def sort_agent_rows(rows: list) -> list:
|
||||
"""
|
||||
按分类元数据对 Agent 行列表原地排序并返回。
|
||||
"""
|
||||
return sorted(rows, key=agent_category_sort_key)
|
||||
|
||||
|
||||
def serialize_agent_category_fields(row: Any) -> dict[str, Any]:
|
||||
"""
|
||||
序列化分类相关 API 字段。
|
||||
空字符串 category / sub_category 输出为 None,便于前端区分「未配置」。
|
||||
"""
|
||||
meta = parse_agent_category_meta(_row_extra_config(row))
|
||||
return {
|
||||
'category': meta['category'] or None,
|
||||
'sub_category': meta['sub_category'] or None,
|
||||
'sort_order': meta['sort_order'],
|
||||
}
|
||||
|
||||
|
||||
def serialize_agent_list_item(row: Any) -> dict[str, Any]:
|
||||
"""
|
||||
序列化用户可见 Agent 列表项(聊天侧栏、工作台首页共用)。
|
||||
核心逻辑:合并基础字段、dify_app_type 与分类元数据。
|
||||
"""
|
||||
extra = _row_extra_config(row) or {}
|
||||
name = row.name if hasattr(row, 'name') else row.get('name')
|
||||
description = row.description if hasattr(row, 'description') else row.get('description')
|
||||
icon_url = row.icon_url if hasattr(row, 'icon_url') else row.get('icon_url')
|
||||
return {
|
||||
'id': _row_id(row),
|
||||
'name': name,
|
||||
'description': description,
|
||||
'icon_url': icon_url,
|
||||
'dify_app_type': extra.get('dify_app_type') or 'chatflow',
|
||||
**serialize_agent_category_fields(row),
|
||||
}
|
||||
Reference in new issue
Block a user