feat: attachment_support

This commit is contained in:
MaoMengww
2026-04-10 12:49:20 +08:00
parent 69785ad0a3
commit aaee5f044a
26 changed files with 1029 additions and 19 deletions
+4
View File
@@ -45,6 +45,9 @@ type PipelineRequest struct {
VLMModelID string `json:"-"`
ChatModelSupportsVision bool `json:"-"`
// File attachments support
Attachments MessageAttachments `json:"-"`
// Misc request-scoped config
TenantID uint64 `json:"-"`
WebSearchEnabled bool `json:"-"`
@@ -185,6 +188,7 @@ func (c *ChatManage) Clone() *ChatManage {
Images: append([]string(nil), c.Images...),
VLMModelID: c.VLMModelID,
ChatModelSupportsVision: c.ChatModelSupportsVision,
Attachments: append(MessageAttachments(nil), c.Attachments...),
TenantID: c.TenantID,
WebSearchEnabled: c.WebSearchEnabled,
WebSearchProviderID: c.WebSearchProviderID,
+4
View File
@@ -120,6 +120,10 @@ type CustomAgentConfig struct {
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"`
// Whether audio upload (ASR transcription) is enabled for this agent (default: false)
AudioUploadEnabled bool `yaml:"audio_upload_enabled" json:"audio_upload_enabled"`
// ASR model ID for audio transcription (optional)
ASRModelID string `yaml:"asr_model_id" json:"asr_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"`
+80
View File
@@ -4,6 +4,8 @@ package types
import (
"database/sql/driver"
"encoding/json"
"fmt"
"strings"
"time"
"github.com/google/uuid"
@@ -64,6 +66,79 @@ func (m *MessageImages) Scan(value interface{}) error {
return json.Unmarshal(b, m)
}
// MessageAttachment represents a file attachment in a chat message
type MessageAttachment struct {
URL string `json:"url"` // Storage URL (provider://path)
FileName string `json:"file_name"` // Original filename
FileType string `json:"file_type"` // File extension (e.g., ".pdf", ".docx")
FileSize int64 `json:"file_size"` // File size in bytes
Content string `json:"content,omitempty"` // Extracted text content (for small text files)
IsTruncated bool `json:"is_truncated,omitempty"` // Whether content was truncated
LineCount int `json:"line_count,omitempty"` // Total line count (for text files)
}
// MessageAttachments is a slice of MessageAttachment for database storage
type MessageAttachments []MessageAttachment
// BuildPrompt returns a formatted prompt section for all attachments,
// injecting file metadata and extracted content into the LLM context.
func (attachments MessageAttachments) BuildPrompt() string {
if len(attachments) == 0 {
return ""
}
var sb strings.Builder
sb.WriteString("\n\n## 用户上传的附件\n\n")
for i, att := range attachments {
sb.WriteString(fmt.Sprintf("### 附件 %d: %s\n\n", i+1, att.FileName))
sb.WriteString(fmt.Sprintf("- **文件类型**: %s\n", att.FileType))
sb.WriteString(fmt.Sprintf("- **文件大小**: %.2f KB\n\n", float64(att.FileSize)/1024))
if att.Content != "" {
sb.WriteString("**文件内容**:\n\n")
sb.WriteString(att.Content)
sb.WriteString("\n\n")
if att.IsTruncated {
sb.WriteString(fmt.Sprintf("*注意: 此文件共有 %d 行,已截取前 500 行显示。*\n\n",
att.LineCount))
}
} else {
sb.WriteString("*此文件内容提取失败或不支持。*\n\n")
}
}
return sb.String()
}
// Value implements the driver.Valuer interface for database serialization
func (m MessageAttachments) Value() (driver.Value, error) {
if m == nil {
return json.Marshal([]MessageAttachment{})
}
return json.Marshal(m)
}
// Scan implements the sql.Scanner interface for database deserialization
func (m *MessageAttachments) Scan(value interface{}) error {
if value == nil {
*m = make(MessageAttachments, 0)
return nil
}
var b []byte
switch v := value.(type) {
case []byte:
b = v
case string:
b = []byte(v)
default:
*m = make(MessageAttachments, 0)
return nil
}
return json.Unmarshal(b, m)
}
// MentionedItems is a slice of MentionedItem for database storage
type MentionedItems []MentionedItem
@@ -119,6 +194,8 @@ type Message struct {
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"`
// Attached files (documents, audio, etc., for user messages)
Attachments MessageAttachments `json:"attachments,omitempty" gorm:"type:jsonb;column:attachments"`
// Whether message generation is complete
IsCompleted bool `json:"is_completed"`
// Whether this response is a fallback (no knowledge base match found)
@@ -194,6 +271,9 @@ func (m *Message) BeforeCreate(tx *gorm.DB) (err error) {
if m.Images == nil {
m.Images = make(MessageImages, 0)
}
if m.Attachments == nil {
m.Attachments = make(MessageAttachments, 0)
}
return nil
}
+1
View File
@@ -17,4 +17,5 @@ type QARequest struct {
WebSearchEnabled bool // Whether web search is enabled for this request
EnableMemory bool // Whether memory feature is enabled
QuotedContext string // Quoted message content from IM quote-reply (appended at LLM prompt stage, not used for retrieval)
Attachments MessageAttachments // File attachments (processed and ready for prompt injection)
}