fix(agent): honor suggested-questions limit parameter

Treat limit as the authoritative upper bound instead of clamping it down
to the agent's Starters.Count, so ?limit=N no longer always returns 6.

Fixes #2238
This commit is contained in:
Jack Wang
2026-07-23 21:23:31 +08:00
committed by lyingbug
parent e0b6a71503
commit 43839f7830
10 changed files with 152 additions and 19 deletions
+23 -3
View File
@@ -23,6 +23,16 @@ var (
ErrAgentNameRequired = errors.New("agent name is required")
)
const (
// suggestionDefaultLimit is the fallback count when neither the caller nor
// the agent configuration specifies how many suggestions to return.
suggestionDefaultLimit = 6
// suggestionMaxLimit caps how many suggestions a single request may ask for.
// It bounds the downstream candidate pool (limit*5) so an oversized limit
// cannot turn into an unbounded chunk query.
suggestionMaxLimit = 30
)
// customAgentService implements the CustomAgentService interface
type customAgentService struct {
repo interfaces.CustomAgentRepository
@@ -497,8 +507,15 @@ func (s *customAgentService) getSuggestedQuestions(
limit int,
includeCurated bool,
) ([]types.SuggestedQuestion, error) {
if limit <= 0 {
limit = 6
// A non-positive limit means "unspecified": fall back to the agent's
// configured Starters.Count (below) or the default. An explicit limit is
// authoritative and only bounded by the safety cap.
limitProvided := limit > 0
if !limitProvided {
limit = suggestionDefaultLimit
}
if limit > suggestionMaxLimit {
limit = suggestionMaxLimit
}
if err := types.AuthorizeTenantAPIKeyKnowledgeTargets(ctx, kbIDs, knowledgeIDs); err != nil {
@@ -529,7 +546,10 @@ func (s *customAgentService) getSuggestedQuestions(
if suggestionConfig == nil || !suggestionConfig.Starters.Enabled {
return []types.SuggestedQuestion{}, nil
}
if limit > suggestionConfig.Starters.Count {
// Starters.Count is the agent-author default, applied only when the
// caller did not request a specific limit. An explicit limit stays
// authoritative so ?limit=N actually changes how many are returned.
if !limitProvided && suggestionConfig.Starters.Count > 0 {
limit = suggestionConfig.Starters.Count
}
starterMode = suggestionConfig.Starters.Mode
@@ -0,0 +1,103 @@
package service
import (
"context"
"testing"
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// suggestionLimitAgentRepo returns a single curated-starter agent so the
// suggestion path resolves without touching chunk/KB repositories.
type suggestionLimitAgentRepo struct {
interfaces.CustomAgentRepository
agent *types.CustomAgent
}
func (r *suggestionLimitAgentRepo) GetAgentByID(
_ context.Context, _ string, _ uint64,
) (*types.CustomAgent, error) {
return r.agent, nil
}
func newCuratedStarterAgent(count int, items []string) *types.CustomAgent {
return &types.CustomAgent{
ID: "agent-1",
TenantID: 1,
Config: types.CustomAgentConfig{
QuestionSuggestions: &types.QuestionSuggestionConfig{
Starters: types.StarterSuggestionConfig{
Enabled: true,
Mode: types.SuggestionModeCurated,
Count: count,
Items: items,
},
},
},
}
}
// TestGetSuggestedQuestions_ExplicitLimitOverridesStarterCount is the
// regression test for issue #2238: an explicit limit must be honored instead
// of being clamped down to the agent's configured Starters.Count.
func TestGetSuggestedQuestions_ExplicitLimitOverridesStarterCount(t *testing.T) {
ctx := context.WithValue(context.Background(), types.TenantIDContextKey, uint64(1))
items := []string{"q1", "q2", "q3", "q4", "q5", "q6", "q7", "q8", "q9", "q10"}
svc := &customAgentService{
repo: &suggestionLimitAgentRepo{agent: newCuratedStarterAgent(6, items)},
}
got, err := svc.GetSuggestedQuestions(ctx, "agent-1", nil, nil, nil, 10)
require.NoError(t, err)
// Before the fix limit was clamped to Starters.Count (6); now the explicit
// request of 10 is honored.
assert.Len(t, got, 10)
}
// TestGetSuggestedQuestions_SmallerLimitStillHonored guards the other
// direction: a limit below Starters.Count must also take effect.
func TestGetSuggestedQuestions_SmallerLimitStillHonored(t *testing.T) {
ctx := context.WithValue(context.Background(), types.TenantIDContextKey, uint64(1))
items := []string{"q1", "q2", "q3", "q4", "q5", "q6"}
svc := &customAgentService{
repo: &suggestionLimitAgentRepo{agent: newCuratedStarterAgent(6, items)},
}
got, err := svc.GetSuggestedQuestions(ctx, "agent-1", nil, nil, nil, 2)
require.NoError(t, err)
assert.Len(t, got, 2)
}
// TestGetSuggestedQuestions_UnspecifiedLimitFallsBackToStarterCount confirms
// that omitting the limit (<= 0) still uses the agent's configured count.
func TestGetSuggestedQuestions_UnspecifiedLimitFallsBackToStarterCount(t *testing.T) {
ctx := context.WithValue(context.Background(), types.TenantIDContextKey, uint64(1))
items := []string{"q1", "q2", "q3", "q4", "q5", "q6", "q7", "q8"}
svc := &customAgentService{
repo: &suggestionLimitAgentRepo{agent: newCuratedStarterAgent(4, items)},
}
got, err := svc.GetSuggestedQuestions(ctx, "agent-1", nil, nil, nil, 0)
require.NoError(t, err)
assert.Len(t, got, 4)
}
// TestGetSuggestedQuestions_LimitBoundedByMax ensures an oversized limit is
// capped so it cannot inflate the downstream candidate pool without bound.
func TestGetSuggestedQuestions_LimitBoundedByMax(t *testing.T) {
ctx := context.WithValue(context.Background(), types.TenantIDContextKey, uint64(1))
items := make([]string, 0, 100)
for i := 0; i < 100; i++ {
items = append(items, "q"+string(rune('A'+i%26))+string(rune('0'+i/26)))
}
svc := &customAgentService{
repo: &suggestionLimitAgentRepo{agent: newCuratedStarterAgent(6, items)},
}
got, err := svc.GetSuggestedQuestions(ctx, "agent-1", nil, nil, nil, 999)
require.NoError(t, err)
assert.Len(t, got, suggestionMaxLimit)
}
@@ -322,9 +322,8 @@ func (s *embedChannelService) SuggestedQuestions(
if ch == nil || !ch.ShowSuggestedQuestions {
return nil, nil
}
if limit <= 0 {
limit = 6
}
// A non-positive limit is forwarded as "unspecified" so GetSuggestedQuestions
// falls back to the agent's configured starter count rather than a fixed 6.
kbIDs := s.resolveKnowledgeBaseIDs(ctx, ch)
return s.agentService.GetSuggestedQuestions(ctx, ch.AgentID, kbIDs, nil, nil, limit)
}