Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

4.6 KB
Raw Blame
//
//  Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//

package entity

import "encoding/json"

// ConversationMessageFields contains searchable fields and variable message metadata.
type ConversationMessageFields struct {
	MessageID   *string         `gorm:"column:message_id;size:255;index:,composite:message_lookup,priority:2" json:"id,omitempty"`
	Role        *string         `gorm:"column:role;size:32;index:,composite:message_lookup,priority:3" json:"role,omitempty"`
	Content     *string         `gorm:"column:content;type:longtext" json:"content,omitempty"`
	ContentType string          `gorm:"column:content_type;size:8" json:"-"`
	Status      *string         `gorm:"column:status;size:32" json:"status,omitempty"`
	ThumbUp     *bool           `gorm:"column:thumb_up" json:"thumbup,omitempty"`
	Feedback    *string         `gorm:"column:feedback;type:longtext" json:"feedback,omitempty"`
	CreatedAt   *float64        `gorm:"column:created_at" json:"created_at,omitempty"`
	Prompt      *string         `gorm:"column:prompt;type:longtext" json:"prompt,omitempty"`
	Usage       JSONMap         `gorm:"column:usage;type:longtext" json:"usage,omitempty"`
	ElapsedMs   *float64        `gorm:"column:elapsed_ms" json:"elapsed_ms,omitempty"`
	Reference   json.RawMessage `gorm:"column:reference;type:longtext;serializer:json" json:"reference,omitempty"`
	Metadata    string          `gorm:"column:metadata;type:longtext" json:"-"`
}

// ConversationMessage stores one ordered message for a conversation.
type ConversationMessage struct {
	ConversationID string `gorm:"column:conversation_id;primaryKey;size:32;index:,composite:message_lookup,priority:1" json:"conversation_id"`
	Position       int    `gorm:"column:position;primaryKey;autoIncrement:false;index:,composite:message_lookup,priority:4" json:"position"`
	ConversationMessageFields
	Conversation ChatSession `gorm:"foreignKey:ConversationID;references:ID;constraint:OnDelete:CASCADE" json:"-"`
}

func (ConversationMessage) TableName() string {
	return "conversation_message"
}

// ConversationReference stores one ordered reference for a conversation.
type ConversationReference struct {
	ConversationID string          `gorm:"column:conversation_id;primaryKey;size:32" json:"conversation_id"`
	Position       int             `gorm:"column:position;primaryKey;autoIncrement:false" json:"position"`
	Reference      json.RawMessage `gorm:"column:reference;type:longtext;not null" json:"reference"`
	MessagePos     *int            `gorm:"column:message_position" json:"-"`
	Conversation   ChatSession     `gorm:"foreignKey:ConversationID;references:ID;constraint:OnDelete:CASCADE" json:"-"`
}

func (ConversationReference) TableName() string {
	return "conversation_reference"
}

// API4ConversationMessage stores one ordered message for an API conversation.
type API4ConversationMessage struct {
	ConversationID string `gorm:"column:conversation_id;primaryKey;size:32;index:,composite:message_lookup,priority:1" json:"conversation_id"`
	Position       int    `gorm:"column:position;primaryKey;autoIncrement:false;index:,composite:message_lookup,priority:4" json:"position"`
	ConversationMessageFields
	Conversation API4Conversation `gorm:"foreignKey:ConversationID;references:ID;constraint:OnDelete:CASCADE" json:"-"`
}

func (API4ConversationMessage) TableName() string {
	return "api_4_conversation_message"
}

// API4ConversationReference stores one ordered reference for an API conversation.
type API4ConversationReference struct {
	ConversationID string           `gorm:"column:conversation_id;primaryKey;size:32" json:"conversation_id"`
	Position       int              `gorm:"column:position;primaryKey;autoIncrement:false" json:"position"`
	Reference      json.RawMessage  `gorm:"column:reference;type:longtext;not null" json:"reference"`
	MessagePos     *int             `gorm:"column:message_position" json:"-"`
	Conversation   API4Conversation `gorm:"foreignKey:ConversationID;references:ID;constraint:OnDelete:CASCADE" json:"-"`
}

func (API4ConversationReference) TableName() string {
	return "api_4_conversation_reference"
}