feat(agent): enhance suggested questions API with tag scopes
Update the SuggestedQuestionsRequest to include TagScopes for better filtering of knowledge items based on their parent knowledge bases. Modify related API documentation and frontend interfaces to accommodate the new structure. Ensure backend services handle the new tag scope logic correctly, including tests for the updated functionality.
This commit is contained in:
@@ -2,6 +2,7 @@ package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -561,6 +562,7 @@ func (h *CustomAgentHandler) GetAgentTypePresets(c *gin.Context) {
|
||||
// @Param id path string true "智能体ID"
|
||||
// @Param knowledge_base_ids query string false "知识库ID列表(逗号分隔),覆盖智能体默认配置"
|
||||
// @Param knowledge_ids query string false "知识ID列表(逗号分隔),限定到具体文档"
|
||||
// @Param tag_scopes query string false "带知识库归属的标签范围(JSON)"
|
||||
// @Param limit query int false "返回数量上限(默认6)"
|
||||
// @Success 200 {object} map[string]interface{} "推荐问题列表"
|
||||
// @Failure 400 {object} errors.AppError "请求参数错误"
|
||||
@@ -598,12 +600,11 @@ func (h *CustomAgentHandler) GetSuggestedQuestions(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
var tagIDs []string
|
||||
if tagIDsStr := strings.TrimSpace(c.Query("tag_ids")); tagIDsStr != "" {
|
||||
for _, id := range strings.Split(tagIDsStr, ",") {
|
||||
if trimmed := strings.TrimSpace(id); trimmed != "" {
|
||||
tagIDs = append(tagIDs, trimmed)
|
||||
}
|
||||
var tagScopes []types.TagScope
|
||||
if raw := strings.TrimSpace(c.Query("tag_scopes")); raw != "" {
|
||||
if err := json.Unmarshal([]byte(raw), &tagScopes); err != nil {
|
||||
c.Error(errors.NewBadRequestError("tag_scopes must be valid JSON"))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
@@ -614,10 +615,10 @@ func (h *CustomAgentHandler) GetSuggestedQuestions(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "Getting suggested questions for agent %s, kbIDs: %v, tagIDs: %v, limit: %d",
|
||||
secutils.SanitizeForLog(id), kbIDs, tagIDs, limit)
|
||||
logger.Infof(ctx, "Getting suggested questions for agent %s, kbIDs: %v, tagScopes: %d, limit: %d",
|
||||
secutils.SanitizeForLog(id), kbIDs, len(tagScopes), limit)
|
||||
|
||||
questions, err := h.service.GetSuggestedQuestions(ctx, id, kbIDs, knowledgeIDs, tagIDs, limit)
|
||||
questions, err := h.service.GetSuggestedQuestions(ctx, id, kbIDs, knowledgeIDs, tagScopes, limit)
|
||||
if err != nil {
|
||||
logger.ErrorWithFields(ctx, err, map[string]interface{}{
|
||||
"agent_id": id,
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"testing"
|
||||
|
||||
apperrors "github.com/Tencent/WeKnora/internal/errors"
|
||||
@@ -11,21 +12,24 @@ import (
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
type suggestedQuestionsAgentService struct {
|
||||
interfaces.CustomAgentService
|
||||
err error
|
||||
err error
|
||||
tagScopes []types.TagScope
|
||||
}
|
||||
|
||||
func (s *suggestedQuestionsAgentService) GetSuggestedQuestions(
|
||||
context.Context,
|
||||
string,
|
||||
[]string,
|
||||
[]string,
|
||||
[]string,
|
||||
int,
|
||||
_ context.Context,
|
||||
_ string,
|
||||
_ []string,
|
||||
_ []string,
|
||||
tagScopes []types.TagScope,
|
||||
_ int,
|
||||
) ([]types.SuggestedQuestion, error) {
|
||||
s.tagScopes = tagScopes
|
||||
return nil, s.err
|
||||
}
|
||||
|
||||
@@ -47,3 +51,44 @@ func TestGetSuggestedQuestionsPreservesAppErrorStatus(t *testing.T) {
|
||||
t.Fatalf("status = %d, want %d, body=%s", rec.Code, http.StatusForbidden, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetSuggestedQuestionsParsesScopedTags(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
r := gin.New()
|
||||
r.Use(middleware.ErrorHandler())
|
||||
|
||||
service := &suggestedQuestionsAgentService{}
|
||||
h := &CustomAgentHandler{service: service}
|
||||
r.GET("/agents/:id/suggested-questions", h.GetSuggestedQuestions)
|
||||
|
||||
rawScopes := `[{"knowledge_base_id":"kb-1","tag_ids":["tag-1","tag-2"]}]`
|
||||
rec := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(
|
||||
http.MethodGet,
|
||||
"/agents/agent-1/suggested-questions?tag_scopes="+url.QueryEscape(rawScopes),
|
||||
nil,
|
||||
)
|
||||
r.ServeHTTP(rec, req)
|
||||
|
||||
require.Equal(t, http.StatusOK, rec.Code, rec.Body.String())
|
||||
require.Equal(t, []types.TagScope{{KnowledgeBaseID: "kb-1", TagIDs: []string{"tag-1", "tag-2"}}}, service.tagScopes)
|
||||
}
|
||||
|
||||
func TestGetSuggestedQuestionsRejectsInvalidScopedTags(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
r := gin.New()
|
||||
r.Use(middleware.ErrorHandler())
|
||||
|
||||
h := &CustomAgentHandler{service: &suggestedQuestionsAgentService{}}
|
||||
r.GET("/agents/:id/suggested-questions", h.GetSuggestedQuestions)
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(
|
||||
http.MethodGet,
|
||||
"/agents/agent-1/suggested-questions?tag_scopes="+url.QueryEscape("not-json"),
|
||||
nil,
|
||||
)
|
||||
r.ServeHTTP(rec, req)
|
||||
|
||||
require.Equal(t, http.StatusBadRequest, rec.Code, rec.Body.String())
|
||||
}
|
||||
|
||||
@@ -314,6 +314,7 @@ func (h *Handler) parseQARequest(c *gin.Context, logPrefix string) (*qaRequestCo
|
||||
secutils.SanitizeForLogArray(kbIDs),
|
||||
secutils.SanitizeForLogArray(knowledgeIDs),
|
||||
secutils.SanitizeForLogArray(tagIDs),
|
||||
tagScopes,
|
||||
secutils.SanitizeForLogArray(mcpServiceIDs),
|
||||
secutils.SanitizeForLogArray(skillNames),
|
||||
request.WebSearchEnabled,
|
||||
@@ -372,6 +373,7 @@ func buildMessageExecutionContext(
|
||||
knowledgeBaseIDs []string,
|
||||
knowledgeIDs []string,
|
||||
tagIDs []string,
|
||||
tagScopes []types.TagScope,
|
||||
mcpServiceIDs []string,
|
||||
skillNames []string,
|
||||
webSearchEnabled bool,
|
||||
@@ -385,6 +387,7 @@ func buildMessageExecutionContext(
|
||||
KnowledgeBaseIDs: knowledgeBaseIDs,
|
||||
KnowledgeIDs: knowledgeIDs,
|
||||
TagIDs: tagIDs,
|
||||
TagScopes: cloneTagScopes(tagScopes),
|
||||
MCPServiceIDs: mcpServiceIDs,
|
||||
SkillNames: skillNames,
|
||||
WebSearchEnabled: webSearchEnabled,
|
||||
@@ -418,12 +421,14 @@ func buildMessageExecutionContext(
|
||||
KnowledgeBaseIDs []string `json:"knowledge_base_ids,omitempty"`
|
||||
KnowledgeIDs []string `json:"knowledge_ids,omitempty"`
|
||||
TagIDs []string `json:"tag_ids,omitempty"`
|
||||
TagScopes []types.TagScope `json:"tag_scopes,omitempty"`
|
||||
ModelID string `json:"model_id,omitempty"`
|
||||
}{
|
||||
QuestionSuggestions: snapshot.QuestionSuggestions,
|
||||
KnowledgeBaseIDs: knowledgeBaseIDs,
|
||||
KnowledgeIDs: knowledgeIDs,
|
||||
TagIDs: tagIDs,
|
||||
TagScopes: snapshot.TagScopes,
|
||||
ModelID: modelID,
|
||||
}
|
||||
if encoded, err := json.Marshal(hashInput); err == nil {
|
||||
@@ -434,6 +439,23 @@ func buildMessageExecutionContext(
|
||||
return snapshot, agent.ID, agentTenantID, modelID
|
||||
}
|
||||
|
||||
func cloneTagScopes(scopes []types.TagScope) []types.TagScope {
|
||||
if len(scopes) == 0 {
|
||||
return nil
|
||||
}
|
||||
cloned := make([]types.TagScope, 0, len(scopes))
|
||||
for _, scope := range scopes {
|
||||
if scope.KnowledgeBaseID == "" || len(scope.TagIDs) == 0 {
|
||||
continue
|
||||
}
|
||||
cloned = append(cloned, types.TagScope{
|
||||
KnowledgeBaseID: scope.KnowledgeBaseID,
|
||||
TagIDs: append([]string(nil), scope.TagIDs...),
|
||||
})
|
||||
}
|
||||
return cloned
|
||||
}
|
||||
|
||||
// resolveEnableMemory decides whether the memory pipeline runs for this
|
||||
// request. See the call-site comment in parseQARequest for the resolution
|
||||
// order. Lookup errors are logged but never propagate — a failure to read
|
||||
|
||||
Reference in New Issue
Block a user