feat: Implement manual knowledge editing and enhance knowledge base features

- Added functionality for manual knowledge entry, allowing users to create and edit Markdown content directly in the browser.
- Introduced new API endpoints for creating and updating manual knowledge entries.
- Enhanced UI components to support the new manual knowledge editor, including integration with existing knowledge base features.
- Updated knowledge retrieval logic to accommodate manual entries, ensuring seamless user experience across knowledge types.
- Improved agent configuration to support custom system prompts, enhancing flexibility in agent responses.
This commit is contained in:
wizardchen
2025-11-12 21:27:56 +08:00
parent a342a0aa14
commit 69f3f30d83
36 changed files with 3495 additions and 412 deletions
+12 -11
View File
@@ -10,17 +10,18 @@ import (
// AgentConfig represents the full agent configuration (used at tenant level and runtime)
// This includes all configuration parameters for agent execution
type AgentConfig struct {
Enabled bool `json:"enabled"` // Whether agent mode is enabled
MaxIterations int `json:"max_iterations"` // Maximum number of ReAct iterations
ReflectionEnabled bool `json:"reflection_enabled"` // Whether to enable reflection
AllowedTools []string `json:"allowed_tools"` // List of allowed tool names
Temperature float64 `json:"temperature"` // LLM temperature for agent
ThinkingModelID string `json:"thinking_model_id"` // Model ID for reasoning
RerankModelID string `json:"rerank_model_id"` // Model ID for reranking search results
KnowledgeBases []string `json:"knowledge_bases"` // Accessible knowledge base IDs
SystemPrompt string `json:"system_prompt,omitempty"` // System prompt template with placeholders (optional)
WebSearchEnabled bool `json:"web_search_enabled"` // Whether web search tool is enabled
WebSearchMaxResults int `json:"web_search_max_results"` // Maximum number of web search results (default: 5)
Enabled bool `json:"enabled"` // Whether agent mode is enabled
MaxIterations int `json:"max_iterations"` // Maximum number of ReAct iterations
ReflectionEnabled bool `json:"reflection_enabled"` // Whether to enable reflection
AllowedTools []string `json:"allowed_tools"` // List of allowed tool names
Temperature float64 `json:"temperature"` // LLM temperature for agent
ThinkingModelID string `json:"thinking_model_id"` // Model ID for reasoning
RerankModelID string `json:"rerank_model_id"` // Model ID for reranking search results
KnowledgeBases []string `json:"knowledge_bases"` // Accessible knowledge base IDs
SystemPrompt string `json:"system_prompt,omitempty"` // System prompt template with placeholders (optional)
UseCustomSystemPrompt bool `json:"use_custom_system_prompt"` // Whether to use custom system prompt instead of default
WebSearchEnabled bool `json:"web_search_enabled"` // Whether web search tool is enabled
WebSearchMaxResults int `json:"web_search_max_results"` // Maximum number of web search results (default: 5)
}
// SessionAgentConfig represents session-level agent configuration
+4
View File
@@ -24,6 +24,8 @@ type KnowledgeService interface {
CreateKnowledgeFromPassage(ctx context.Context, kbID string, passage []string) (*types.Knowledge, error)
// CreateKnowledgeFromPassageSync creates knowledge from text passages and waits until chunks are indexed.
CreateKnowledgeFromPassageSync(ctx context.Context, kbID string, passage []string) (*types.Knowledge, error)
// CreateKnowledgeFromManual creates or saves manual Markdown knowledge content.
CreateKnowledgeFromManual(ctx context.Context, kbID string, payload *types.ManualKnowledgePayload) (*types.Knowledge, error)
// GetKnowledgeByID retrieves knowledge by ID.
GetKnowledgeByID(ctx context.Context, id string) (*types.Knowledge, error)
// GetKnowledgeBatch retrieves a batch of knowledge by IDs.
@@ -42,6 +44,8 @@ type KnowledgeService interface {
GetKnowledgeFile(ctx context.Context, id string) (io.ReadCloser, string, error)
// UpdateKnowledge updates knowledge information.
UpdateKnowledge(ctx context.Context, knowledge *types.Knowledge) error
// UpdateManualKnowledge updates manual Markdown knowledge content.
UpdateManualKnowledge(ctx context.Context, knowledgeID string, payload *types.ManualKnowledgePayload) (*types.Knowledge, error)
// CloneKnowledgeBase clones knowledge to another knowledge base.
CloneKnowledgeBase(ctx context.Context, srcID, dstID string) error
// UpdateImageInfo updates image information for a knowledge chunk.
+123
View File
@@ -1,6 +1,7 @@
package types
import (
"encoding/json"
"fmt"
"time"
@@ -8,6 +9,16 @@ import (
"gorm.io/gorm"
)
const (
KnowledgeTypeManual = "manual"
)
const (
ManualKnowledgeFormatMarkdown = "markdown"
ManualKnowledgeStatusDraft = "draft"
ManualKnowledgeStatusPublish = "publish"
)
// Knowledge represents a knowledge entity in the system.
// It contains metadata about the knowledge source, its processing status,
// and references to the physical file if applicable.
@@ -77,6 +88,118 @@ func (k *Knowledge) BeforeCreate(tx *gorm.DB) (err error) {
return nil
}
// ManualKnowledgeMetadata stores metadata for manual Markdown knowledge content.
type ManualKnowledgeMetadata struct {
Content string `json:"content"`
Format string `json:"format"`
Status string `json:"status"`
Version int `json:"version"`
UpdatedAt string `json:"updated_at"`
}
// ManualKnowledgePayload represents the payload for manual knowledge operations.
type ManualKnowledgePayload struct {
Title string `json:"title"`
Content string `json:"content"`
Status string `json:"status"`
}
// NewManualKnowledgeMetadata creates a new ManualKnowledgeMetadata instance.
func NewManualKnowledgeMetadata(content, status string, version int) *ManualKnowledgeMetadata {
if version <= 0 {
version = 1
}
return &ManualKnowledgeMetadata{
Content: content,
Format: ManualKnowledgeFormatMarkdown,
Status: status,
Version: version,
UpdatedAt: time.Now().UTC().Format(time.RFC3339),
}
}
// ToJSON converts the metadata to JSON type.
func (m *ManualKnowledgeMetadata) ToJSON() (JSON, error) {
if m == nil {
return nil, nil
}
if m.Format == "" {
m.Format = ManualKnowledgeFormatMarkdown
}
if m.Status == "" {
m.Status = ManualKnowledgeStatusDraft
}
if m.Version <= 0 {
m.Version = 1
}
if m.UpdatedAt == "" {
m.UpdatedAt = time.Now().UTC().Format(time.RFC3339)
}
bytes, err := json.Marshal(m)
if err != nil {
return nil, err
}
return JSON(bytes), nil
}
// ManualMetadata parses and returns manual knowledge metadata.
func (k *Knowledge) ManualMetadata() (*ManualKnowledgeMetadata, error) {
if len(k.Metadata) == 0 {
return nil, nil
}
var metadata ManualKnowledgeMetadata
if err := json.Unmarshal(k.Metadata, &metadata); err != nil {
return nil, err
}
if metadata.Format == "" {
metadata.Format = ManualKnowledgeFormatMarkdown
}
if metadata.Version <= 0 {
metadata.Version = 1
}
return &metadata, nil
}
// SetManualMetadata sets manual knowledge metadata onto the knowledge instance.
func (k *Knowledge) SetManualMetadata(meta *ManualKnowledgeMetadata) error {
if meta == nil {
k.Metadata = nil
return nil
}
jsonValue, err := meta.ToJSON()
if err != nil {
return err
}
k.Metadata = jsonValue
return nil
}
// IsManual returns true if the knowledge item is manual Markdown knowledge.
func (k *Knowledge) IsManual() bool {
return k != nil && k.Type == KnowledgeTypeManual
}
// EnsureManualDefaults sets default values for manual knowledge entries.
func (k *Knowledge) EnsureManualDefaults() {
if k == nil {
return
}
if k.Type == "" {
k.Type = KnowledgeTypeManual
}
if k.FileType == "" {
k.FileType = KnowledgeTypeManual
}
if k.Source == "" {
k.Source = KnowledgeTypeManual
}
}
// IsDraft returns whether the payload should be saved as draft.
func (p ManualKnowledgePayload) IsDraft() bool {
return p.Status == "" || p.Status == ManualKnowledgeStatusDraft
}
// KnowledgeCheckParams defines parameters used to check if knowledge already exists.
type KnowledgeCheckParams struct {
// File parameters