feat: implement image upload and multimodal support

- Added functionality for image uploads in chat, allowing users to attach images for multimodal Q&A.
- Enhanced the input field to handle image selection via drag-and-drop and paste, with validation for file types and sizes.
- Updated the backend to process images alongside text queries, including support for image analysis.
- Introduced new UI components for image previews and management, improving user interaction with uploaded content.
- Added localization strings for image upload features and error messages, enhancing accessibility for users in multiple languages.

These changes significantly improve the chat experience by enabling users to incorporate images into their queries, facilitating richer interactions and responses.
This commit is contained in:
wizardchen
2026-03-11 17:17:32 +08:00
parent 8de1cfb6b5
commit 991bd29a14
42 changed files with 1554 additions and 226 deletions
+14 -4
View File
@@ -59,8 +59,13 @@ func (c *AgentConfig) Scan(value interface{}) error {
if value == nil {
return nil
}
b, ok := value.([]byte)
if !ok {
var b []byte
switch v := value.(type) {
case []byte:
b = v
case string:
b = []byte(v)
default:
return nil
}
return json.Unmarshal(b, c)
@@ -76,8 +81,13 @@ func (c *SessionAgentConfig) Scan(value interface{}) error {
if value == nil {
return nil
}
b, ok := value.([]byte)
if !ok {
var b []byte
switch v := value.(type) {
case []byte:
b = v
case string:
b = []byte(v)
default:
return nil
}
return json.Unmarshal(b, c)
+12
View File
@@ -60,6 +60,13 @@ type ChatManage struct {
FAQPriorityEnabled bool `json:"-"` // Whether FAQ priority strategy is enabled
FAQDirectAnswerThreshold float64 `json:"-"` // Threshold for direct FAQ answer (similarity > this value)
FAQScoreBoost float64 `json:"-"` // Score multiplier for FAQ results
// Image support for multimodal chat
Images []string `json:"-"` // Image URLs for MultiContent in current user message
ImageOCRText string `json:"-"` // Image description/OCR text generated by VLM (used as fallback for non-vision models)
VLMModelID string `json:"-"` // Agent-configured VLM model ID for image analysis
ChatModelSupportsVision bool `json:"-"` // Whether the chat model accepts multimodal/image input
SkipKBSearch bool `json:"-"` // Set by rewrite intent classification: true = skip KB retrieval
}
// Clone creates a deep copy of the ChatManage object
@@ -129,6 +136,11 @@ func (c *ChatManage) Clone() *ChatManage {
FAQPriorityEnabled: c.FAQPriorityEnabled,
FAQDirectAnswerThreshold: c.FAQDirectAnswerThreshold,
FAQScoreBoost: c.FAQScoreBoost,
Images: append([]string(nil), c.Images...),
ImageOCRText: c.ImageOCRText,
VLMModelID: c.VLMModelID,
ChatModelSupportsVision: c.ChatModelSupportsVision,
SkipKBSearch: c.SkipKBSearch,
}
}
+16 -2
View File
@@ -109,6 +109,15 @@ type CustomAgentConfig struct {
// When false, knowledge base retrieval happens according to KBSelectionMode
RetrieveKBOnlyWhenMentioned bool `yaml:"retrieve_kb_only_when_mentioned" json:"retrieve_kb_only_when_mentioned"`
// ===== Image Upload / Multimodal Settings =====
// Whether image upload is enabled for this agent (default: false)
ImageUploadEnabled bool `yaml:"image_upload_enabled" json:"image_upload_enabled"`
// VLM model ID for image analysis (optional, falls back to tenant-level VLM)
VLMModelID string `yaml:"vlm_model_id" json:"vlm_model_id"`
// Storage provider for image uploads: "local", "minio", "cos", "tos"
// Empty means use the global/tenant default provider.
ImageStorageProvider string `yaml:"image_storage_provider" json:"image_storage_provider"`
// ===== File Type Restriction Settings =====
// Supported file types for this agent (e.g., ["csv", "xlsx", "xls"])
// Empty means all file types are supported
@@ -174,8 +183,13 @@ func (c *CustomAgentConfig) Scan(value interface{}) error {
if value == nil {
return nil
}
b, ok := value.([]byte)
if !ok {
var b []byte
switch v := value.(type) {
case []byte:
b = v
case string:
b = []byte(v)
default:
return nil
}
return json.Unmarshal(b, c)
+2
View File
@@ -21,10 +21,12 @@ type AgentStreamEvent struct {
// AgentEngine defines the interface for agent execution engine
type AgentEngine interface {
// Execute executes the agent with conversation history and returns a stream of events
// imageURLs is optional - when provided, images are passed to the LLM as multimodal content
Execute(
ctx context.Context,
sessionID, messageID, query string,
llmContext []chat.Message,
imageURLs ...[]string,
) (*types.AgentState, error)
}
+2
View File
@@ -44,6 +44,7 @@ type SessionService interface {
session *types.Session, query string, knowledgeBaseIDs []string, knowledgeIDs []string,
assistantMessageID string, summaryModelID string, webSearchEnabled bool, eventBus *event.EventBus,
customAgent *types.CustomAgent, enableMemory bool,
imageURLs []string, imageOCRText string,
) error
// KnowledgeQAByEvent performs knowledge-based question answering by event
KnowledgeQAByEvent(ctx context.Context, chatManage *types.ChatManage, eventList []types.EventType) error
@@ -65,6 +66,7 @@ type SessionService interface {
customAgent *types.CustomAgent,
knowledgeBaseIDs []string,
knowledgeIDs []string,
imageURLs []string, imageOCRText string,
) error
// ClearContext clears the LLM context for a session
ClearContext(ctx context.Context, sessionID string) error
+55 -4
View File
@@ -28,6 +28,42 @@ type MentionedItem struct {
KBType string `json:"kb_type"` // "document" or "faq" (only for kb type)
}
// MessageImage represents an image attached to a chat message
type MessageImage struct {
URL string `json:"url"`
Caption string `json:"caption,omitempty"`
}
// MessageImages is a slice of MessageImage for database storage
type MessageImages []MessageImage
// Value implements the driver.Valuer interface for database serialization
func (m MessageImages) Value() (driver.Value, error) {
if m == nil {
return json.Marshal([]MessageImage{})
}
return json.Marshal(m)
}
// Scan implements the sql.Scanner interface for database deserialization
func (m *MessageImages) Scan(value interface{}) error {
if value == nil {
*m = make(MessageImages, 0)
return nil
}
var b []byte
switch v := value.(type) {
case []byte:
b = v
case string:
b = []byte(v)
default:
*m = make(MessageImages, 0)
return nil
}
return json.Unmarshal(b, m)
}
// MentionedItems is a slice of MentionedItem for database storage
type MentionedItems []MentionedItem
@@ -45,8 +81,13 @@ func (m *MentionedItems) Scan(value interface{}) error {
*m = make(MentionedItems, 0)
return nil
}
b, ok := value.([]byte)
if !ok {
var b []byte
switch v := value.(type) {
case []byte:
b = v
case string:
b = []byte(v)
default:
*m = make(MentionedItems, 0)
return nil
}
@@ -76,6 +117,8 @@ type Message struct {
// Mentioned knowledge bases and files (for user messages)
// Stores the @mentioned items when user sends a message
MentionedItems MentionedItems `json:"mentioned_items,omitempty" gorm:"type:jsonb,column:mentioned_items"`
// Attached images with OCR/Caption text (for user messages)
Images MessageImages `json:"images,omitempty" gorm:"type:jsonb;column:images"`
// Whether message generation is complete
IsCompleted bool `json:"is_completed"`
// Whether this response is a fallback (no knowledge base match found)
@@ -111,8 +154,13 @@ func (a *AgentSteps) Scan(value interface{}) error {
*a = make(AgentSteps, 0)
return nil
}
b, ok := value.([]byte)
if !ok {
var b []byte
switch v := value.(type) {
case []byte:
b = v
case string:
b = []byte(v)
default:
*a = make(AgentSteps, 0)
return nil
}
@@ -137,6 +185,9 @@ func (m *Message) BeforeCreate(tx *gorm.DB) (err error) {
if m.MentionedItems == nil {
m.MentionedItems = make(MentionedItems, 0)
}
if m.Images == nil {
m.Images = make(MessageImages, 0)
}
return nil
}
+1
View File
@@ -63,6 +63,7 @@ type ModelParameters struct {
ParameterSize string `yaml:"parameter_size" json:"parameter_size"` // Ollama model parameter size (e.g., "7B", "13B", "70B")
Provider string `yaml:"provider" json:"provider"` // Provider identifier: openai, aliyun, zhipu, generic
ExtraConfig map[string]string `yaml:"extra_config" json:"extra_config"` // Provider-specific configuration
SupportsVision bool `yaml:"supports_vision" json:"supports_vision"` // Whether the model accepts image/multimodal input
}
// Model represents the AI model