mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-08-09 08:57:57 +08:00
feat: add agent filter counts (#17984)
This commit is contained in:
@@ -681,6 +681,18 @@ _COMPILATION_TEMPLATE_GROUP_CATEGORY = "compilation_template_group"
|
||||
@login_required
|
||||
@add_tenant_id_to_kwargs
|
||||
def list_agents(tenant_id):
|
||||
if request.args.get("type") == "filter":
|
||||
tenants = TenantService.get_joined_tenants_by_user_id(tenant_id)
|
||||
joined_tenant_ids = list({member["tenant_id"] for member in tenants} | {tenant_id})
|
||||
owners = UserCanvasService.get_owner_filter(joined_tenant_ids, tenant_id)
|
||||
categories = UserCanvasService.get_category_filter(joined_tenant_ids, tenant_id)
|
||||
return get_json_result(
|
||||
data={
|
||||
"filter": {"owner": owners, "canvas_category": categories},
|
||||
"total": sum(owner["count"] for owner in owners),
|
||||
}
|
||||
)
|
||||
|
||||
keywords = request.args.get("keywords", "")
|
||||
canvas_category_list = [item for item in request.args.get("canvas_category", "").strip().split(",") if item]
|
||||
canvas_type = request.args.get("canvas_type")
|
||||
@@ -707,6 +719,7 @@ def list_agents(tenant_id):
|
||||
effective_owner_ids = list(requested_owner_ids)
|
||||
else:
|
||||
effective_owner_ids = list(authorized_owner_ids)
|
||||
include_template_groups = tenant_id in effective_owner_ids
|
||||
|
||||
# Groups-only: when ``compilation_template_group`` is the only selected
|
||||
# category, return just the caller's template groups (no agents) via
|
||||
@@ -715,11 +728,12 @@ def list_agents(tenant_id):
|
||||
if canvas_category_list == [_COMPILATION_TEMPLATE_GROUP_CATEGORY]:
|
||||
from api.db.services.compilation_template_group_service import CompilationTemplateGroupService
|
||||
|
||||
try:
|
||||
groups = CompilationTemplateGroupService.list_saved(tenant_id, keywords, "", order_by, desc)
|
||||
except Exception:
|
||||
logging.exception("list_agents: compilation template group list failed for tenant=%s", tenant_id)
|
||||
groups = []
|
||||
groups = []
|
||||
if include_template_groups:
|
||||
try:
|
||||
groups = CompilationTemplateGroupService.list_saved(tenant_id, keywords, "", order_by, desc)
|
||||
except Exception:
|
||||
logging.exception("list_agents: compilation template group list failed for tenant=%s", tenant_id)
|
||||
for group in groups:
|
||||
group["type"] = _COMPILATION_TEMPLATE_GROUP_CATEGORY
|
||||
total = len(groups)
|
||||
@@ -758,11 +772,12 @@ def list_agents(tenant_id):
|
||||
)
|
||||
# Groups are owner-only (no team sharing), so they're scoped to the
|
||||
# caller. Keyword filters the group name; scope is left unfiltered.
|
||||
try:
|
||||
groups = CompilationTemplateGroupService.list_saved(tenant_id, keywords, "", order_by, desc)
|
||||
except Exception:
|
||||
logging.exception("list_agents: compilation template group merge failed for tenant=%s", tenant_id)
|
||||
groups = []
|
||||
groups = []
|
||||
if include_template_groups:
|
||||
try:
|
||||
groups = CompilationTemplateGroupService.list_saved(tenant_id, keywords, "", order_by, desc)
|
||||
except Exception:
|
||||
logging.exception("list_agents: compilation template group merge failed for tenant=%s", tenant_id)
|
||||
|
||||
items: list[dict] = []
|
||||
for agent in agents:
|
||||
@@ -802,11 +817,12 @@ def list_agents(tenant_id):
|
||||
tags,
|
||||
canvas_type,
|
||||
)
|
||||
try:
|
||||
groups = CompilationTemplateGroupService.list_saved(tenant_id, keywords, "", order_by, desc)
|
||||
except Exception:
|
||||
logging.exception("list_agents: compilation template group mixed failed for tenant=%s", tenant_id)
|
||||
groups = []
|
||||
groups = []
|
||||
if include_template_groups:
|
||||
try:
|
||||
groups = CompilationTemplateGroupService.list_saved(tenant_id, keywords, "", order_by, desc)
|
||||
except Exception:
|
||||
logging.exception("list_agents: compilation template group mixed failed for tenant=%s", tenant_id)
|
||||
|
||||
items = []
|
||||
for agent in agents:
|
||||
|
||||
@@ -21,11 +21,12 @@ from operator import or_
|
||||
from uuid import uuid4
|
||||
from agent.canvas import Canvas
|
||||
from api.db import CanvasCategory, TenantPermission
|
||||
from api.db.db_models import DB, CanvasTemplate, User, UserCanvas, API4Conversation, UserCanvasVersion
|
||||
from api.db.db_models import DB, CanvasTemplate, CompilationTemplateGroup, User, UserCanvas, API4Conversation, UserCanvasVersion
|
||||
from api.db.services.api_service import API4ConversationService
|
||||
from api.db.services.common_service import CommonService
|
||||
from api.db.services.user_canvas_version import UserCanvasVersionService
|
||||
from common.misc_utils import get_uuid, thread_pool_exec
|
||||
from common.constants import StatusEnum
|
||||
from api.utils.api_utils import get_data_openai
|
||||
import tiktoken
|
||||
from peewee import fn
|
||||
@@ -197,6 +198,63 @@ class UserCanvasService(CommonService):
|
||||
|
||||
return agents_list, count
|
||||
|
||||
@classmethod
|
||||
@DB.connection_context()
|
||||
def get_owner_filter(cls, joined_tenant_ids, user_id):
|
||||
owner_filter = cls.model.user_id.in_(joined_tenant_ids) & ((cls.model.permission == TenantPermission.TEAM.value) | (cls.model.user_id == user_id))
|
||||
owners = (
|
||||
cls.model.select(
|
||||
cls.model.user_id.alias("id"),
|
||||
User.nickname.alias("label"),
|
||||
fn.COUNT(cls.model.id).alias("count"),
|
||||
)
|
||||
.join(User, on=(cls.model.user_id == User.id))
|
||||
.where(owner_filter)
|
||||
.group_by(cls.model.user_id, User.nickname)
|
||||
)
|
||||
owner_list = list(owners.dicts())
|
||||
group_count = (
|
||||
CompilationTemplateGroup.select()
|
||||
.where(
|
||||
CompilationTemplateGroup.tenant_id == user_id,
|
||||
CompilationTemplateGroup.status == StatusEnum.VALID.value,
|
||||
)
|
||||
.count()
|
||||
)
|
||||
if group_count:
|
||||
current_owner = next((owner for owner in owner_list if owner["id"] == user_id), None)
|
||||
if current_owner:
|
||||
current_owner["count"] += group_count
|
||||
else:
|
||||
nickname = User.select(User.nickname).where(User.id == user_id).scalar()
|
||||
owner_list.append({"id": user_id, "label": nickname or "", "count": group_count})
|
||||
return owner_list
|
||||
|
||||
@classmethod
|
||||
@DB.connection_context()
|
||||
def get_category_filter(cls, joined_tenant_ids, user_id):
|
||||
category_filter = cls.model.user_id.in_(joined_tenant_ids) & ((cls.model.permission == TenantPermission.TEAM.value) | (cls.model.user_id == user_id))
|
||||
categories = (
|
||||
cls.model.select(
|
||||
cls.model.canvas_category.alias("id"),
|
||||
fn.COUNT(cls.model.id).alias("count"),
|
||||
)
|
||||
.where(category_filter)
|
||||
.group_by(cls.model.canvas_category)
|
||||
)
|
||||
category_list = list(categories.dicts())
|
||||
group_count = (
|
||||
CompilationTemplateGroup.select()
|
||||
.where(
|
||||
CompilationTemplateGroup.tenant_id == user_id,
|
||||
CompilationTemplateGroup.status == StatusEnum.VALID.value,
|
||||
)
|
||||
.count()
|
||||
)
|
||||
if group_count:
|
||||
category_list.append({"id": "compilation_template_group", "count": group_count})
|
||||
return category_list
|
||||
|
||||
@classmethod
|
||||
@DB.connection_context()
|
||||
def list_tags(cls, joined_tenant_ids, user_id, canvas_category=None):
|
||||
|
||||
Reference in New Issue
Block a user