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
+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)