feat(memory-management): implement server-side user preference for memory feature
Updated the memory management logic to utilize a server-side per-user preference for enabling memory. The `enable_memory` field is now conditionally set based on user preferences, allowing for better control in both normal and embedded contexts. Adjusted relevant API handlers and request structures to support this change, ensuring backward compatibility with existing clients. Refs: #1303
This commit is contained in:
@@ -203,6 +203,17 @@ func (h *Handler) parseQARequest(c *gin.Context, logPrefix string) (*qaRequestCo
|
||||
logger.Infof(ctx, "[%s] all attachments processed", logPrefix)
|
||||
}
|
||||
|
||||
// Resolve enable_memory:
|
||||
// 1. Explicit value in request → honour it. Used by embedded mode
|
||||
// (force false) and by older clients still sending the literal bool.
|
||||
// 2. Not set → fall back to the calling user's stored preference.
|
||||
// The toggle is persisted server-side per user (see PUT
|
||||
// /auth/me/preferences); this is the canonical path for the
|
||||
// normal logged-in web UI now that it no longer sends the field.
|
||||
// 3. No user / no preference → false. API-key-only callers never
|
||||
// had memory enabled in practice, keep that behaviour.
|
||||
enableMemory := h.resolveEnableMemory(ctx, request.EnableMemory)
|
||||
|
||||
// Build request context
|
||||
reqCtx := &qaRequestContext{
|
||||
ctx: ctx,
|
||||
@@ -224,7 +235,7 @@ func (h *Handler) parseQARequest(c *gin.Context, logPrefix string) (*qaRequestCo
|
||||
knowledgeIDs: secutils.SanitizeForLogArray(knowledgeIDs),
|
||||
summaryModelID: secutils.SanitizeForLog(request.SummaryModelID),
|
||||
webSearchEnabled: request.WebSearchEnabled,
|
||||
enableMemory: request.EnableMemory,
|
||||
enableMemory: enableMemory,
|
||||
mentionedItems: convertMentionedItems(request.MentionedItems),
|
||||
effectiveTenantID: effectiveTenantID,
|
||||
images: request.Images,
|
||||
@@ -235,6 +246,32 @@ func (h *Handler) parseQARequest(c *gin.Context, logPrefix string) (*qaRequestCo
|
||||
return reqCtx, &request, nil
|
||||
}
|
||||
|
||||
// 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
|
||||
// the user's preference shouldn't break the chat request itself, we just
|
||||
// fall back to false (the safe default).
|
||||
func (h *Handler) resolveEnableMemory(ctx context.Context, override *bool) bool {
|
||||
if override != nil {
|
||||
return *override
|
||||
}
|
||||
if h.userService == nil {
|
||||
return false
|
||||
}
|
||||
user, err := h.userService.GetCurrentUser(ctx)
|
||||
if err != nil {
|
||||
// API-key-only callers or revoked sessions land here; the chat
|
||||
// request itself stays authorised via the middleware that already
|
||||
// ran, we just have nobody to look preferences up for.
|
||||
logger.Debugf(ctx, "enable_memory: no user in context, defaulting to false: %v", err)
|
||||
return false
|
||||
}
|
||||
if user.Preferences.EnableMemory != nil {
|
||||
return *user.Preferences.EnableMemory
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// resolveAgent resolves the custom agent by ID, trying shared agent first, then own agent.
|
||||
// Returns (nil, 0) if agentID is empty or not found.
|
||||
func (h *Handler) resolveAgent(ctx context.Context, c *gin.Context, agentID string) (*types.CustomAgent, uint64) {
|
||||
|
||||
Reference in New Issue
Block a user