feat(creator_name): add creator_name field to agents and knowledge bases for enhanced visibility

Introduced a new optional field, creator_name, in both CustomAgent and KnowledgeBase types to allow the front end to display the creator's name. This enhancement enables better differentiation between resources created by the current user and those created by other members of the same tenant. Updated relevant API handlers to populate this field during list operations, ensuring accurate representation in the UI. Additionally, modified the ResourceOriginBadge component to utilize the creator_name for improved context in resource listings.

Refs: #1303
This commit is contained in:
wizardchen
2026-05-18 00:28:16 +08:00
committed by lyingbug
parent 8b681e4a2e
commit 8d4c58c5fa
13 changed files with 894 additions and 433 deletions
+54 -3
View File
@@ -1,6 +1,7 @@
package handler
import (
"context"
"net/http"
"strconv"
"strings"
@@ -16,15 +17,23 @@ import (
// CustomAgentHandler defines the HTTP handler for custom agent operations
type CustomAgentHandler struct {
service interfaces.CustomAgentService
service interfaces.CustomAgentService
disabledRepo interfaces.TenantDisabledSharedAgentRepository
// userService 仅用于 list 接口批量回填 creator_name,作用见
// KnowledgeBaseHandler.userService。
userService interfaces.UserService
}
// NewCustomAgentHandler creates a new custom agent handler instance
func NewCustomAgentHandler(service interfaces.CustomAgentService, disabledRepo interfaces.TenantDisabledSharedAgentRepository) *CustomAgentHandler {
func NewCustomAgentHandler(
service interfaces.CustomAgentService,
disabledRepo interfaces.TenantDisabledSharedAgentRepository,
userService interfaces.UserService,
) *CustomAgentHandler {
return &CustomAgentHandler{
service: service,
service: service,
disabledRepo: disabledRepo,
userService: userService,
}
}
@@ -215,6 +224,11 @@ func (h *CustomAgentHandler) ListAgents(c *gin.Context) {
return
}
// 批量回填 creator_name,作用同 KB 列表:让前端能区分「我创建」与「同租户其他成员」。
// 内建 agent(IsBuiltin=true, CreatedBy="")不会有 creator_name,前端按 builtin
// 分支单独渲染。
enrichAgentCreatorNames(ctx, h.userService, agents)
c.JSON(http.StatusOK, gin.H{
"success": true,
"data": agents,
@@ -222,6 +236,43 @@ func (h *CustomAgentHandler) ListAgents(c *gin.Context) {
})
}
// enrichAgentCreatorNames 批量把 agent.CreatedBy 解析成展示名。失败吞掉,
// 不影响列表本身可用。与 enrichKBCreatorNames 行为对齐。
func enrichAgentCreatorNames(ctx context.Context, userSvc interfaces.UserService, agents []*types.CustomAgent) {
if userSvc == nil || len(agents) == 0 {
return
}
idSet := make(map[string]struct{}, len(agents))
for _, ag := range agents {
if ag.IsBuiltin || ag.CreatedBy == "" {
continue
}
idSet[ag.CreatedBy] = struct{}{}
}
if len(idSet) == 0 {
return
}
ids := make([]string, 0, len(idSet))
for id := range idSet {
ids = append(ids, id)
}
users, err := userSvc.GetUsersByIDs(ctx, ids)
if err != nil {
logger.Warnf(ctx, "Failed to resolve agent creator names: %v", err)
return
}
for _, ag := range agents {
if ag.IsBuiltin || ag.CreatedBy == "" {
continue
}
u, ok := users[ag.CreatedBy]
if !ok || u == nil {
continue
}
ag.CreatorName = pickUserDisplayName(u)
}
}
// UpdateAgent godoc
// @Summary 更新智能体
// @Description 更新智能体的名称、描述和配置
+60
View File
@@ -32,6 +32,9 @@ type KnowledgeBaseHandler struct {
agentShareService interfaces.AgentShareService
asynqClient interfaces.TaskEnqueuer
vectorStoreService interfaces.VectorStoreService // enriches KB responses with bound store display
// userService 仅在 list 类接口里用于批量回填 creator_name;
// 真正的鉴权由 RBAC 中间件 + Lookup 完成,这里不参与决策。
userService interfaces.UserService
}
// NewKnowledgeBaseHandler creates a new knowledge base handler instance
@@ -42,6 +45,7 @@ func NewKnowledgeBaseHandler(
agentShareService interfaces.AgentShareService,
asynqClient interfaces.TaskEnqueuer,
vectorStoreService interfaces.VectorStoreService,
userService interfaces.UserService,
) *KnowledgeBaseHandler {
return &KnowledgeBaseHandler{
service: service,
@@ -50,6 +54,7 @@ func NewKnowledgeBaseHandler(
agentShareService: agentShareService,
asynqClient: asynqClient,
vectorStoreService: vectorStoreService,
userService: userService,
}
}
@@ -548,12 +553,67 @@ func (h *KnowledgeBaseHandler) ListKnowledgeBases(c *gin.Context) {
}
}
// 批量回填 creator_name,让前端列表能区分「我创建」与「同租户其他成员创建」。
// 仅在 list 接口里回填,详情 / 编辑场景不依赖这个字段;解析失败(用户已删除、
// CreatorID 为空的老数据)就让字段为空,前端按 fallback 渲染。
enrichKBCreatorNames(ctx, h.userService, kbs)
c.JSON(http.StatusOK, gin.H{
"success": true,
"data": kbs,
})
}
// enrichKBCreatorNames 把 KB 列表里的 CreatorID 批量解析成展示名(username
// 优先,退化到 email)。任意一步失败都吞掉错误:creator_name 缺失只会影响
// 卡片右下角的徽章展示,不该影响列表本身可用。
func enrichKBCreatorNames(ctx context.Context, userSvc interfaces.UserService, kbs []*types.KnowledgeBase) {
if userSvc == nil || len(kbs) == 0 {
return
}
idSet := make(map[string]struct{}, len(kbs))
for _, kb := range kbs {
if kb.CreatorID != "" {
idSet[kb.CreatorID] = struct{}{}
}
}
if len(idSet) == 0 {
return
}
ids := make([]string, 0, len(idSet))
for id := range idSet {
ids = append(ids, id)
}
users, err := userSvc.GetUsersByIDs(ctx, ids)
if err != nil {
logger.Warnf(ctx, "Failed to resolve KB creator names: %v", err)
return
}
for _, kb := range kbs {
if kb.CreatorID == "" {
continue
}
u, ok := users[kb.CreatorID]
if !ok || u == nil {
continue
}
kb.CreatorName = pickUserDisplayName(u)
}
}
// pickUserDisplayName picks the field most users will recognise: Username
// if present (it's required at registration), Email as a fallback. Used by
// both KB and Agent list enrichment so the badge text stays consistent.
func pickUserDisplayName(u *types.User) string {
if u == nil {
return ""
}
if u.Username != "" {
return u.Username
}
return u.Email
}
// TogglePinKnowledgeBase godoc
// @Summary 置顶/取消置顶知识库
// @Description 切换知识库的置顶状态