feat(attachments): implement normalization and limits for temporary attachment IDs

Add functionality to normalize temporary attachment IDs, ensuring deduplication and enforcing a maximum limit of 5 attachments per message. Update related handlers and tests to support this new behavior, enhancing the robustness of attachment management in chat sessions.
This commit is contained in:
wizardchen
2026-07-16 17:18:08 +08:00
committed by lyingbug
parent 9ae17be43c
commit 85d10d99e3
7 changed files with 725 additions and 7 deletions
+1
View File
@@ -715,6 +715,7 @@ func patchEmbedChatPayload(body io.Reader, ch *types.EmbedChannel, agentMode boo
if !ch.AllowFileUpload {
delete(payload, "images")
delete(payload, "attachment_uploads")
delete(payload, "attachment_ids")
}
payload["mcp_service_ids"] = []string{}
if agentMode {
+37
View File
@@ -262,6 +262,43 @@ func TestPatchEmbedChatPayloadWebSearchBlockedWhenChannelDisabled(t *testing.T)
}
}
func TestPatchEmbedChatPayloadStripsAttachmentsWhenUploadDisabled(t *testing.T) {
ch := &types.EmbedChannel{AgentID: "agent-1", AllowFileUpload: false}
body := `{"query":"hello","images":[{"data":"x"}],"attachment_uploads":[{"file_name":"a.pdf"}],"attachment_ids":["doc-1"]}`
patched, err := patchEmbedChatPayload(strings.NewReader(body), ch, false)
if err != nil {
t.Fatal(err)
}
var payload map[string]any
if err := json.Unmarshal(patched, &payload); err != nil {
t.Fatal(err)
}
for _, key := range []string{"images", "attachment_uploads", "attachment_ids"} {
if _, ok := payload[key]; ok {
t.Fatalf("%s should be stripped when allow_file_upload is false, got %v", key, payload[key])
}
}
}
func TestPatchEmbedChatPayloadKeepsAttachmentIDsWhenUploadAllowed(t *testing.T) {
ch := &types.EmbedChannel{AgentID: "agent-1", AllowFileUpload: true}
body := `{"query":"hello","attachment_ids":["doc-1","doc-2"]}`
patched, err := patchEmbedChatPayload(strings.NewReader(body), ch, false)
if err != nil {
t.Fatal(err)
}
var payload map[string]any
if err := json.Unmarshal(patched, &payload); err != nil {
t.Fatal(err)
}
ids, ok := payload["attachment_ids"].([]any)
if !ok || len(ids) != 2 {
t.Fatalf("attachment_ids = %v, want preserved when upload allowed", payload["attachment_ids"])
}
}
func TestPatchEmbedChatPayloadAgentMode(t *testing.T) {
ch := &types.EmbedChannel{AgentID: "agent-embed-99"}
patched, err := patchEmbedChatPayload(bytes.NewReader(nil), ch, true)
+33 -5
View File
@@ -259,9 +259,13 @@ func (h *Handler) parseQARequest(c *gin.Context, logPrefix string) (*qaRequestCo
var attachmentIDs []string
var attachmentMetas types.MessageAttachments
if len(request.AttachmentIDs) > 0 {
tenantID := c.GetUint64(types.TenantIDContextKey.String())
attachmentMetas = make(types.MessageAttachments, 0, len(request.AttachmentIDs))
for _, id := range request.AttachmentIDs {
normalizedIDs, normErr := normalizeTemporaryAttachmentIDs(request.AttachmentIDs)
if normErr != nil {
return nil, nil, errors.NewBadRequestError(normErr.Error())
}
tenantID := session.TenantID
attachmentMetas = make(types.MessageAttachments, 0, len(normalizedIDs))
for _, id := range normalizedIDs {
doc, getErr := h.temporaryDocuments.Get(ctx, tenantID, sessionID, id)
if getErr != nil || doc == nil {
return nil, nil, errors.NewBadRequestError(
@@ -279,7 +283,7 @@ func (h *Handler) parseQARequest(c *gin.Context, logPrefix string) (*qaRequestCo
FileType: doc.FileType, FileSize: doc.FileSize,
})
}
attachmentIDs = request.AttachmentIDs
attachmentIDs = normalizedIDs
}
// Resolve enable_memory:
@@ -1070,7 +1074,9 @@ func (h *Handler) resolveTemporaryAttachments(streamCtx *sseStreamContext, reqCt
}
ctx := streamCtx.asyncCtx
sessionID := reqCtx.sessionID
tenantID := reqCtx.c.GetUint64(types.TenantIDContextKey.String())
// Prefer the session's tenant over gin.Context: this runs in an async
// goroutine after the HTTP handler may have returned.
tenantID := reqCtx.session.TenantID
start := time.Now()
var toolCallID string
@@ -1206,6 +1212,28 @@ func (h *Handler) persistResolvedAttachmentContent(
}
}
// normalizeTemporaryAttachmentIDs rejects oversized ID lists before any DB
// lookup, then returns a deduplicated, order-preserving list of non-empty IDs.
func normalizeTemporaryAttachmentIDs(ids []string) ([]string, error) {
if len(ids) > types.MaxTemporaryAttachmentsPerMessage {
return nil, fmt.Errorf("a message can use at most %d attachments", types.MaxTemporaryAttachmentsPerMessage)
}
out := make([]string, 0, len(ids))
seen := make(map[string]struct{}, len(ids))
for _, id := range ids {
id = strings.TrimSpace(id)
if id == "" {
continue
}
if _, ok := seen[id]; ok {
continue
}
seen[id] = struct{}{}
out = append(out, id)
}
return out, nil
}
// hasPendingAttachments reports whether any of the given documents is still
// uploading or being parsed.
func (h *Handler) hasPendingAttachments(ctx context.Context, tenantID uint64, sessionID string, ids []string) bool {
@@ -105,3 +105,24 @@ func TestPersistResolvedAttachmentContent_GuardsEmptyInput(t *testing.T) {
assert.Equal(t, 0, stub.updateCalls)
}
func TestNormalizeTemporaryAttachmentIDs(t *testing.T) {
t.Run("rejects over limit before lookup", func(t *testing.T) {
ids := []string{"a", "b", "c", "d", "e", "f"}
_, err := normalizeTemporaryAttachmentIDs(ids)
require.Error(t, err)
assert.Contains(t, err.Error(), "at most 5")
})
t.Run("dedupes and drops empty", func(t *testing.T) {
got, err := normalizeTemporaryAttachmentIDs([]string{" a ", "", "b", "a", "c"})
require.NoError(t, err)
assert.Equal(t, []string{"a", "b", "c"}, got)
})
t.Run("allows max unique ids", func(t *testing.T) {
got, err := normalizeTemporaryAttachmentIDs([]string{"1", "2", "3", "4", "5"})
require.NoError(t, err)
assert.Equal(t, []string{"1", "2", "3", "4", "5"}, got)
})
}