refactor(api): drop deprecated tenant ConversationConfig field and KV write path

The Tenant.ConversationConfig field was marked deprecated when
CustomAgent (builtin-quick-answer) replaced the per-tenant defaults
path, but the JSONB column, GET/PUT KV handlers, write helpers and
read fall-throughs all stayed wired. This had two concrete failure
modes:

  - Input-field.vue silently PUT-ed per-user model picks back into
    /tenants/kv/conversation-config, which requires Admin+ — Viewers
    and Contributors got a 403 every time they switched chat models.
  - The same write also overwrote the tenant-shared default for every
    other user, turning a per-user UX into a per-tenant footgun.

Remove the field, the struct, the validate helper, the PUT case and
the two dead Summary helpers in session/helpers.go. KnowledgeSearch
loses its tenant-conversation fallback (one chunk-tool reader); the
global config + hardcoded defaults still cover it. session_agent_qa
already required agents to declare their own rerank_model_id, so no
behaviour change there.

Frontend follows:
  - Input-field.vue: per-user 'last selected chat model' moves to
    localStorage, no API call.
  - AgentEditorModal: defaults for builtin-quick-answer creation now
    come from prompt-templates (system/context/rewrite/fallback) and
    retrieval-config (top_k / threshold), the two endpoints that
    actually own those settings now.
  - AgentSettings.vue: orphan settings page with no router/import
    referencing it; deleted.
  - api/system: getConversationConfig / updateConversationConfig and
    the ConversationConfig interface go away.

Net: 137 insertions, 2761 deletions.
This commit is contained in:
wizardchen
2026-05-15 12:14:53 +08:00
committed by lyingbug
parent b0b7cfa351
commit 1e8b5c8d40
9 changed files with 138 additions and 2762 deletions
+6 -100
View File
@@ -261,104 +261,10 @@ func getFloat64(m map[string]interface{}, key string) float64 {
return 0.0
}
// createDefaultSummaryConfig creates a default summary configuration from config
// It prioritizes tenant-level ConversationConfig, then falls back to config.yaml defaults
func (h *Handler) createDefaultSummaryConfig(ctx context.Context) *types.SummaryConfig {
// Try to get tenant from context
tenant, _ := types.TenantInfoFromContext(ctx)
// createDefaultSummaryConfig and fillSummaryConfigDefaults used to build
// per-session SummaryConfig from tenant-level ConversationConfig + config.yaml
// defaults. Both helpers became unreachable when the chat pipeline moved to
// CustomAgent (builtin-quick-answer / smart-reasoning) and the tenant-level
// ConversationConfig field was removed; deleting them avoids the only
// remaining references to that defunct path.
// Initialize with config.yaml defaults
cfg := &types.SummaryConfig{
MaxTokens: h.config.Conversation.Summary.MaxTokens,
TopP: h.config.Conversation.Summary.TopP,
TopK: h.config.Conversation.Summary.TopK,
FrequencyPenalty: h.config.Conversation.Summary.FrequencyPenalty,
PresencePenalty: h.config.Conversation.Summary.PresencePenalty,
RepeatPenalty: h.config.Conversation.Summary.RepeatPenalty,
Prompt: h.config.Conversation.Summary.Prompt,
ContextTemplate: h.config.Conversation.Summary.ContextTemplate,
NoMatchPrefix: h.config.Conversation.Summary.NoMatchPrefix,
Temperature: h.config.Conversation.Summary.Temperature,
Seed: h.config.Conversation.Summary.Seed,
MaxCompletionTokens: h.config.Conversation.Summary.MaxCompletionTokens,
}
// Override with tenant-level conversation config if available
if tenant != nil && tenant.ConversationConfig != nil {
// Use custom prompt if provided
if tenant.ConversationConfig.Prompt != "" {
cfg.Prompt = tenant.ConversationConfig.Prompt
}
// Use custom context template if provided
if tenant.ConversationConfig.ContextTemplate != "" {
cfg.ContextTemplate = tenant.ConversationConfig.ContextTemplate
}
if tenant.ConversationConfig.Temperature >= 0 {
cfg.Temperature = tenant.ConversationConfig.Temperature
}
if tenant.ConversationConfig.MaxCompletionTokens > 0 {
cfg.MaxCompletionTokens = tenant.ConversationConfig.MaxCompletionTokens
}
}
return cfg
}
// fillSummaryConfigDefaults fills missing fields in summary config with defaults
// It prioritizes tenant-level ConversationConfig, then falls back to config.yaml defaults
func (h *Handler) fillSummaryConfigDefaults(ctx context.Context, config *types.SummaryConfig) {
// Try to get tenant from context
tenant, _ := types.TenantInfoFromContext(ctx)
// Determine default values: tenant config first, then config.yaml
var defaultPrompt, defaultContextTemplate, defaultNoMatchPrefix string
var defaultTemperature float64
var defaultMaxCompletionTokens int
if tenant != nil && tenant.ConversationConfig != nil {
// Use custom prompt if provided
if tenant.ConversationConfig.Prompt != "" {
defaultPrompt = tenant.ConversationConfig.Prompt
}
// Use custom context template if provided
if tenant.ConversationConfig.ContextTemplate != "" {
defaultContextTemplate = tenant.ConversationConfig.ContextTemplate
}
defaultTemperature = tenant.ConversationConfig.Temperature
defaultMaxCompletionTokens = tenant.ConversationConfig.MaxCompletionTokens
}
// Fall back to config.yaml if tenant config is empty
if defaultPrompt == "" {
defaultPrompt = h.config.Conversation.Summary.Prompt
}
if defaultContextTemplate == "" {
defaultContextTemplate = h.config.Conversation.Summary.ContextTemplate
}
if defaultTemperature == 0 {
defaultTemperature = h.config.Conversation.Summary.Temperature
}
if defaultMaxCompletionTokens == 0 {
defaultMaxCompletionTokens = h.config.Conversation.Summary.MaxCompletionTokens
}
defaultNoMatchPrefix = h.config.Conversation.Summary.NoMatchPrefix
// Fill missing fields
if config.Prompt == "" {
config.Prompt = defaultPrompt
}
if config.ContextTemplate == "" {
config.ContextTemplate = defaultContextTemplate
}
if config.Temperature < 0 {
config.Temperature = defaultTemperature
}
if config.MaxCompletionTokens == 0 {
config.MaxCompletionTokens = defaultMaxCompletionTokens
}
if config.NoMatchPrefix == "" {
config.NoMatchPrefix = defaultNoMatchPrefix
}
}
+2 -143
View File
@@ -576,7 +576,7 @@ func (h *TenantHandler) updateTenantAgentConfigInternal(c *gin.Context) {
// GetTenantKV godoc
// @Summary 获取租户KV配置
// @Description 获取租户级别的KV配置(支持agent-config、web-search-config、conversation-config)
// @Description 获取租户级别的KV配置(支持agent-config、web-search-config 等)
// @Tags 租户管理
// @Accept json
// @Produce json
@@ -597,9 +597,6 @@ func (h *TenantHandler) GetTenantKV(c *gin.Context) {
case "web-search-config":
h.GetTenantWebSearchConfig(c)
return
case "conversation-config":
h.GetTenantConversationConfig(c)
return
case "prompt-templates":
h.GetPromptTemplates(c)
return
@@ -624,7 +621,7 @@ func (h *TenantHandler) GetTenantKV(c *gin.Context) {
// UpdateTenantKV godoc
// @Summary 更新租户KV配置
// @Description 更新租户级别的KV配置(支持agent-config、web-search-config、conversation-config)
// @Description 更新租户级别的KV配置(支持agent-config、web-search-config 等)
// @Tags 租户管理
// @Accept json
// @Produce json
@@ -646,9 +643,6 @@ func (h *TenantHandler) UpdateTenantKV(c *gin.Context) {
case "web-search-config":
h.updateTenantWebSearchConfigInternal(c)
return
case "conversation-config":
h.updateTenantConversationInternal(c)
return
case "parser-engine-config":
h.updateTenantParserEngineConfigInternal(c)
return
@@ -860,141 +854,6 @@ func (h *TenantHandler) updateTenantStorageEngineConfigInternal(c *gin.Context)
})
}
func (h *TenantHandler) buildDefaultConversationConfig() *types.ConversationConfig {
return &types.ConversationConfig{
Prompt: h.config.Conversation.Summary.Prompt,
ContextTemplate: h.config.Conversation.Summary.ContextTemplate,
Temperature: h.config.Conversation.Summary.Temperature,
MaxCompletionTokens: h.config.Conversation.Summary.MaxCompletionTokens,
MaxRounds: h.config.Conversation.MaxRounds,
EmbeddingTopK: h.config.Conversation.EmbeddingTopK,
KeywordThreshold: h.config.Conversation.KeywordThreshold,
VectorThreshold: h.config.Conversation.VectorThreshold,
RerankTopK: h.config.Conversation.RerankTopK,
RerankThreshold: h.config.Conversation.RerankThreshold,
EnableRewrite: h.config.Conversation.EnableRewrite,
EnableQueryExpansion: h.config.Conversation.EnableQueryExpansion,
FallbackStrategy: h.config.Conversation.FallbackStrategy,
FallbackResponse: h.config.Conversation.FallbackResponse,
FallbackPrompt: h.config.Conversation.FallbackPrompt,
RewritePromptUser: h.config.Conversation.RewritePromptUser,
RewritePromptSystem: h.config.Conversation.RewritePromptSystem,
}
}
func validateConversationConfig(req *types.ConversationConfig) error {
if req.MaxRounds <= 0 {
return errors.NewBadRequestError("max_rounds must be greater than 0")
}
if req.EmbeddingTopK <= 0 {
return errors.NewBadRequestError("embedding_top_k must be greater than 0")
}
if req.KeywordThreshold < 0 || req.KeywordThreshold > 1 {
return errors.NewBadRequestError("keyword_threshold must be between 0 and 1")
}
if req.VectorThreshold < 0 || req.VectorThreshold > 1 {
return errors.NewBadRequestError("vector_threshold must be between 0 and 1")
}
if req.RerankTopK <= 0 {
return errors.NewBadRequestError("rerank_top_k must be greater than 0")
}
if req.RerankThreshold < -10 || req.RerankThreshold > 10 {
return errors.NewBadRequestError("rerank_threshold must be between -10 and 10")
}
if req.Temperature < 0 || req.Temperature > 2 {
return errors.NewBadRequestError("temperature must be between 0 and 2")
}
if req.MaxCompletionTokens <= 0 || req.MaxCompletionTokens > 100000 {
return errors.NewBadRequestError("max_completion_tokens must be between 1 and 100000")
}
if req.FallbackStrategy != "" &&
req.FallbackStrategy != string(types.FallbackStrategyFixed) &&
req.FallbackStrategy != string(types.FallbackStrategyModel) {
return errors.NewBadRequestError("fallback_strategy is invalid")
}
return nil
}
// GetTenantConversationConfig godoc
// @Summary 获取租户对话配置
// @Description 获取租户的全局对话配置(默认应用于普通模式会话)
// @Tags 租户管理
// @Accept json
// @Produce json
// @Success 200 {object} map[string]interface{} "对话配置"
// @Failure 400 {object} errors.AppError "请求参数错误"
// @Security Bearer
// @Security ApiKeyAuth
// @Router /tenants/kv/conversation-config [get]
func (h *TenantHandler) GetTenantConversationConfig(c *gin.Context) {
ctx := c.Request.Context()
tenant, _ := types.TenantInfoFromContext(ctx)
if tenant == nil {
logger.Error(ctx, "Tenant is empty")
c.Error(errors.NewBadRequestError("Tenant is empty"))
return
}
// If tenant has no conversation config, return defaults from config.yaml
var response *types.ConversationConfig
logger.Info(ctx, "Tenant has no conversation config, returning defaults")
response = h.buildDefaultConversationConfig()
c.JSON(http.StatusOK, gin.H{
"success": true,
"data": response,
})
}
// updateTenantConversationInternal updates the conversation configuration for a tenant
// This sets the global conversation configuration for normal mode sessions in this tenant
func (h *TenantHandler) updateTenantConversationInternal(c *gin.Context) {
ctx := c.Request.Context()
logger.Info(ctx, "Start updating tenant conversation config")
var req types.ConversationConfig
if err := c.ShouldBindJSON(&req); err != nil {
logger.Error(ctx, "Failed to parse request parameters", err)
c.Error(errors.NewValidationError("Invalid request data").WithDetails(err.Error()))
return
}
// Validate configuration
if err := validateConversationConfig(&req); err != nil {
c.Error(err)
return
}
// Get existing tenant
tenant, _ := types.TenantInfoFromContext(ctx)
if tenant == nil {
logger.Error(ctx, "Tenant is empty")
c.Error(errors.NewBadRequestError("Tenant is empty"))
return
}
// Update conversation configuration
tenant.ConversationConfig = &req
updatedTenant, err := h.service.UpdateTenant(ctx, tenant)
if err != nil {
if appErr, ok := errors.IsAppError(err); ok {
logger.Error(ctx, "Failed to update tenant: application error", appErr)
c.Error(appErr)
} else {
logger.ErrorWithFields(ctx, err, nil)
c.Error(errors.NewInternalServerError("Failed to update tenant conversation config").WithDetails(err.Error()))
}
return
}
logger.Infof(ctx, "Tenant conversation config updated successfully, Tenant ID: %d", tenant.ID)
c.JSON(http.StatusOK, gin.H{
"success": true,
"data": updatedTenant.ConversationConfig,
"message": "Conversation configuration updated successfully",
})
}
// GetPromptTemplates godoc
// @Summary 获取提示词模板
// @Description 获取系统配置的提示词模板列表