@@ -220,7 +220,7 @@ deps:
|
||||
|
||||
# Build for production
|
||||
build-prod:
|
||||
VERSION=$${VERSION:-unknown}; \
|
||||
VERSION=$${VERSION:-$$(git describe --tags --abbrev=0 2>/dev/null || echo "unknown")}; \
|
||||
COMMIT_ID=$${COMMIT_ID:-unknown}; \
|
||||
BUILD_TIME=$${BUILD_TIME:-unknown}; \
|
||||
GO_VERSION=$${GO_VERSION:-unknown}; \
|
||||
|
||||
+18
-6
@@ -12,14 +12,26 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// MentionedItem represents a mentioned item in the request
|
||||
type MentionedItem struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"` // "kb" for knowledge base, "file" for file
|
||||
KBType string `json:"kb_type"` // "document" or "faq" (only for kb type)
|
||||
}
|
||||
|
||||
// AgentQARequest agent Q&A request payload.
|
||||
type AgentQARequest struct {
|
||||
Query string `json:"query"` // Required query text
|
||||
KnowledgeBaseIDs []string `json:"knowledge_base_ids,omitempty"` // Optional KBs for this query
|
||||
AgentEnabled bool `json:"agent_enabled"` // Whether to run in agent mode
|
||||
WebSearchEnabled bool `json:"web_search_enabled"` // Whether to enable web search
|
||||
SummaryModelID string `json:"summary_model_id,omitempty"` // Optional summary model override
|
||||
MCPServiceIDs []string `json:"mcp_service_ids,omitempty"` // Optional MCP service allow list
|
||||
Query string `json:"query"` // Required query text
|
||||
KnowledgeBaseIDs []string `json:"knowledge_base_ids,omitempty"` // Optional KBs for this query
|
||||
KnowledgeIDs []string `json:"knowledge_ids,omitempty"` // Optional specific knowledge IDs for this query
|
||||
AgentEnabled bool `json:"agent_enabled"` // Whether to run in agent mode
|
||||
AgentID string `json:"agent_id,omitempty"` // Optional custom agent ID
|
||||
WebSearchEnabled bool `json:"web_search_enabled"` // Whether to enable web search
|
||||
SummaryModelID string `json:"summary_model_id,omitempty"` // Optional summary model override
|
||||
MentionedItems []MentionedItem `json:"mentioned_items,omitempty"` // @mentioned knowledge bases and files
|
||||
DisableTitle bool `json:"disable_title,omitempty"` // Whether to disable auto title generation
|
||||
MCPServiceIDs []string `json:"mcp_service_ids,omitempty"` // Optional MCP service allow list (deprecated)
|
||||
}
|
||||
|
||||
// AgentResponseType defines the type of agent response
|
||||
|
||||
@@ -15,6 +15,7 @@ import (
|
||||
// Chunks are the basic units of storage and indexing in the knowledge base
|
||||
type Chunk struct {
|
||||
ID string `json:"id"` // Unique identifier of the chunk
|
||||
SeqID int64 `json:"seq_id"` // Auto-increment integer ID for external API usage
|
||||
KnowledgeID string `json:"knowledge_id"` // Identifier of the parent knowledge
|
||||
KnowledgeBaseID string `json:"knowledge_base_id"` // ID of the knowledge base
|
||||
TenantID uint64 `json:"tenant_id"` // Tenant ID
|
||||
|
||||
+135
-48
@@ -12,11 +12,11 @@ import (
|
||||
|
||||
// FAQEntry represents a FAQ item stored under a knowledge base.
|
||||
type FAQEntry struct {
|
||||
ID string `json:"id"`
|
||||
ID int64 `json:"id"`
|
||||
ChunkID string `json:"chunk_id"`
|
||||
KnowledgeID string `json:"knowledge_id"`
|
||||
KnowledgeBaseID string `json:"knowledge_base_id"`
|
||||
TagID string `json:"tag_id"`
|
||||
TagID int64 `json:"tag_id"`
|
||||
TagName string `json:"tag_name"`
|
||||
IsEnabled bool `json:"is_enabled"`
|
||||
IsRecommended bool `json:"is_recommended"`
|
||||
@@ -35,12 +35,14 @@ type FAQEntry struct {
|
||||
|
||||
// FAQEntryPayload is used to create or update a FAQ entry.
|
||||
type FAQEntryPayload struct {
|
||||
// ID is optional, used for data migration to specify seq_id (must be less than auto-increment start value 100000000)
|
||||
ID *int64 `json:"id,omitempty"`
|
||||
StandardQuestion string `json:"standard_question"`
|
||||
SimilarQuestions []string `json:"similar_questions,omitempty"`
|
||||
NegativeQuestions []string `json:"negative_questions,omitempty"`
|
||||
Answers []string `json:"answers"`
|
||||
AnswerStrategy *string `json:"answer_strategy,omitempty"`
|
||||
TagID string `json:"tag_id,omitempty"`
|
||||
TagID int64 `json:"tag_id,omitempty"`
|
||||
TagName string `json:"tag_name,omitempty"`
|
||||
IsEnabled *bool `json:"is_enabled,omitempty"`
|
||||
IsRecommended *bool `json:"is_recommended,omitempty"`
|
||||
@@ -51,13 +53,15 @@ type FAQBatchUpsertPayload struct {
|
||||
Entries []FAQEntryPayload `json:"entries"`
|
||||
Mode string `json:"mode"`
|
||||
KnowledgeID string `json:"knowledge_id,omitempty"`
|
||||
TaskID string `json:"task_id,omitempty"` // Optional, if not provided, a UUID will be generated
|
||||
DryRun bool `json:"dry_run,omitempty"` // If true, only validate without importing
|
||||
}
|
||||
|
||||
// FAQEntryFieldsUpdate represents the fields that can be updated for a single FAQ entry.
|
||||
type FAQEntryFieldsUpdate struct {
|
||||
IsEnabled *bool `json:"is_enabled,omitempty"`
|
||||
IsRecommended *bool `json:"is_recommended,omitempty"`
|
||||
TagID *string `json:"tag_id,omitempty"`
|
||||
IsEnabled *bool `json:"is_enabled,omitempty"`
|
||||
IsRecommended *bool `json:"is_recommended,omitempty"`
|
||||
TagID *int64 `json:"tag_id,omitempty"`
|
||||
}
|
||||
|
||||
// FAQEntryFieldsBatchRequest updates multiple fields for FAQ entries in bulk.
|
||||
@@ -65,31 +69,33 @@ type FAQEntryFieldsUpdate struct {
|
||||
// 1. By entry ID: use ByID field
|
||||
// 2. By Tag: use ByTag field to apply the same update to all entries under a tag
|
||||
type FAQEntryFieldsBatchRequest struct {
|
||||
// ByID updates by entry ID, key is entry ID
|
||||
ByID map[string]FAQEntryFieldsUpdate `json:"by_id,omitempty"`
|
||||
// ByTag updates all entries under a tag, key is tag ID (empty string for uncategorized)
|
||||
ByTag map[string]FAQEntryFieldsUpdate `json:"by_tag,omitempty"`
|
||||
// ExcludeIDs IDs to exclude from the ByTag update
|
||||
ExcludeIDs []string `json:"exclude_ids,omitempty"`
|
||||
// ByID updates by entry ID (seq_id), key is entry seq_id
|
||||
ByID map[int64]FAQEntryFieldsUpdate `json:"by_id,omitempty"`
|
||||
// ByTag updates all entries under a tag, key is tag seq_id (0 for uncategorized)
|
||||
ByTag map[int64]FAQEntryFieldsUpdate `json:"by_tag,omitempty"`
|
||||
// ExcludeIDs IDs (seq_id) to exclude from the ByTag update
|
||||
ExcludeIDs []int64 `json:"exclude_ids,omitempty"`
|
||||
}
|
||||
|
||||
// FAQEntryTagBatchRequest updates tags in bulk.
|
||||
// key: entry seq_id, value: tag seq_id (nil to remove tag)
|
||||
type FAQEntryTagBatchRequest struct {
|
||||
Updates map[string]*string `json:"updates"`
|
||||
Updates map[int64]*int64 `json:"updates"`
|
||||
}
|
||||
|
||||
// FAQDeleteRequest deletes entries in bulk.
|
||||
type FAQDeleteRequest struct {
|
||||
IDs []string `json:"ids"`
|
||||
IDs []int64 `json:"ids"`
|
||||
}
|
||||
|
||||
// FAQSearchRequest represents the hybrid FAQ search request.
|
||||
type FAQSearchRequest struct {
|
||||
QueryText string `json:"query_text"`
|
||||
VectorThreshold float64 `json:"vector_threshold"`
|
||||
MatchCount int `json:"match_count"`
|
||||
FirstPriorityTagIDs []string `json:"first_priority_tag_ids"` // First priority tag IDs, highest priority
|
||||
SecondPriorityTagIDs []string `json:"second_priority_tag_ids"` // Second priority tag IDs, lower than first
|
||||
QueryText string `json:"query_text"`
|
||||
VectorThreshold float64 `json:"vector_threshold"`
|
||||
MatchCount int `json:"match_count"`
|
||||
FirstPriorityTagIDs []int64 `json:"first_priority_tag_ids"` // First priority tag seq_ids, highest priority
|
||||
SecondPriorityTagIDs []int64 `json:"second_priority_tag_ids"` // Second priority tag seq_ids, lower than first
|
||||
OnlyRecommended bool `json:"only_recommended"` // Only return recommended entries
|
||||
}
|
||||
|
||||
// FAQEntriesPage contains paginated FAQ results.
|
||||
@@ -144,10 +150,11 @@ type faqSimpleResponse struct {
|
||||
}
|
||||
|
||||
// ListFAQEntries returns paginated FAQ entries under a knowledge base.
|
||||
// tagSeqID: filter by tag seq_id (0 means no filter)
|
||||
// searchField: specifies which field to search in ("standard_question", "similar_questions", "answers", "" for all)
|
||||
// sortOrder: "asc" for time ascending (updated_at ASC), default is time descending (updated_at DESC)
|
||||
func (c *Client) ListFAQEntries(ctx context.Context,
|
||||
knowledgeBaseID string, page, pageSize int, tagID string, keyword string, searchField string, sortOrder string,
|
||||
knowledgeBaseID string, page, pageSize int, tagSeqID int64, keyword string, searchField string, sortOrder string,
|
||||
) (*FAQEntriesPage, error) {
|
||||
path := fmt.Sprintf("/api/v1/knowledge-bases/%s/faq/entries", knowledgeBaseID)
|
||||
query := url.Values{}
|
||||
@@ -157,8 +164,8 @@ func (c *Client) ListFAQEntries(ctx context.Context,
|
||||
if pageSize > 0 {
|
||||
query.Add("page_size", strconv.Itoa(pageSize))
|
||||
}
|
||||
if tagID != "" {
|
||||
query.Add("tag_id", tagID)
|
||||
if tagSeqID != 0 {
|
||||
query.Add("tag_id", strconv.FormatInt(tagSeqID, 10))
|
||||
}
|
||||
if keyword != "" {
|
||||
query.Add("keyword", keyword)
|
||||
@@ -222,11 +229,11 @@ func (c *Client) CreateFAQEntry(ctx context.Context,
|
||||
return response.Data, nil
|
||||
}
|
||||
|
||||
// GetFAQEntry retrieves a single FAQ entry by ID.
|
||||
// GetFAQEntry retrieves a single FAQ entry by seq_id.
|
||||
func (c *Client) GetFAQEntry(ctx context.Context,
|
||||
knowledgeBaseID, entryID string,
|
||||
knowledgeBaseID string, entrySeqID int64,
|
||||
) (*FAQEntry, error) {
|
||||
path := fmt.Sprintf("/api/v1/knowledge-bases/%s/faq/entries/%s", knowledgeBaseID, entryID)
|
||||
path := fmt.Sprintf("/api/v1/knowledge-bases/%s/faq/entries/%d", knowledgeBaseID, entrySeqID)
|
||||
resp, err := c.doRequest(ctx, http.MethodGet, path, nil, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -241,25 +248,51 @@ func (c *Client) GetFAQEntry(ctx context.Context,
|
||||
|
||||
// UpdateFAQEntry updates a single FAQ entry.
|
||||
func (c *Client) UpdateFAQEntry(ctx context.Context,
|
||||
knowledgeBaseID, entryID string, payload *FAQEntryPayload,
|
||||
) error {
|
||||
path := fmt.Sprintf("/api/v1/knowledge-bases/%s/faq/entries/%s", knowledgeBaseID, entryID)
|
||||
knowledgeBaseID string, entrySeqID int64, payload *FAQEntryPayload,
|
||||
) (*FAQEntry, error) {
|
||||
path := fmt.Sprintf("/api/v1/knowledge-bases/%s/faq/entries/%d", knowledgeBaseID, entrySeqID)
|
||||
resp, err := c.doRequest(ctx, http.MethodPut, path, payload, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var response faqSimpleResponse
|
||||
return parseResponse(resp, &response)
|
||||
var response FAQEntryResponse
|
||||
if err := parseResponse(resp, &response); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return response.Data, nil
|
||||
}
|
||||
|
||||
// AddSimilarQuestionsPayload is used to add similar questions to a FAQ entry.
|
||||
type AddSimilarQuestionsPayload struct {
|
||||
SimilarQuestions []string `json:"similar_questions"`
|
||||
}
|
||||
|
||||
// AddSimilarQuestions adds similar questions to a FAQ entry.
|
||||
// This will append the new questions to the existing similar questions list.
|
||||
func (c *Client) AddSimilarQuestions(ctx context.Context,
|
||||
knowledgeBaseID string, entrySeqID int64, payload *AddSimilarQuestionsPayload,
|
||||
) (*FAQEntry, error) {
|
||||
path := fmt.Sprintf("/api/v1/knowledge-bases/%s/faq/entries/%d/similar-questions", knowledgeBaseID, entrySeqID)
|
||||
resp, err := c.doRequest(ctx, http.MethodPost, path, payload, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var response FAQEntryResponse
|
||||
if err := parseResponse(resp, &response); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return response.Data, nil
|
||||
}
|
||||
|
||||
// UpdateFAQEntryFieldsBatch updates multiple fields for FAQ entries in bulk.
|
||||
// Supports updating is_enabled, is_recommended, tag_id in a single call.
|
||||
// Supports two modes:
|
||||
// - byID: update by entry ID, key is entry ID
|
||||
// - byTag: update all entries under a tag, key is tag ID (empty string for uncategorized)
|
||||
// - byID: update by entry seq_id, key is entry seq_id
|
||||
// - byTag: update all entries under a tag, key is tag seq_id (0 for uncategorized)
|
||||
func (c *Client) UpdateFAQEntryFieldsBatch(ctx context.Context,
|
||||
knowledgeBaseID string, byID map[string]FAQEntryFieldsUpdate, byTag map[string]FAQEntryFieldsUpdate, excludeIDs []string,
|
||||
knowledgeBaseID string, byID map[int64]FAQEntryFieldsUpdate, byTag map[int64]FAQEntryFieldsUpdate, excludeIDs []int64,
|
||||
) error {
|
||||
path := fmt.Sprintf("/api/v1/knowledge-bases/%s/faq/entries/fields", knowledgeBaseID)
|
||||
resp, err := c.doRequest(ctx, http.MethodPut, path, &FAQEntryFieldsBatchRequest{ByID: byID, ByTag: byTag, ExcludeIDs: excludeIDs}, nil)
|
||||
@@ -272,8 +305,9 @@ func (c *Client) UpdateFAQEntryFieldsBatch(ctx context.Context,
|
||||
}
|
||||
|
||||
// UpdateFAQEntryTagBatch updates FAQ entry tags in bulk.
|
||||
// key: entry seq_id, value: tag seq_id (nil to remove tag)
|
||||
func (c *Client) UpdateFAQEntryTagBatch(ctx context.Context,
|
||||
knowledgeBaseID string, updates map[string]*string,
|
||||
knowledgeBaseID string, updates map[int64]*int64,
|
||||
) error {
|
||||
path := fmt.Sprintf("/api/v1/knowledge-bases/%s/faq/entries/tags", knowledgeBaseID)
|
||||
resp, err := c.doRequest(ctx, http.MethodPut, path, &FAQEntryTagBatchRequest{Updates: updates}, nil)
|
||||
@@ -285,9 +319,9 @@ func (c *Client) UpdateFAQEntryTagBatch(ctx context.Context,
|
||||
return parseResponse(resp, &response)
|
||||
}
|
||||
|
||||
// DeleteFAQEntries deletes FAQ entries in bulk.
|
||||
// DeleteFAQEntries deletes FAQ entries in bulk by seq_id.
|
||||
func (c *Client) DeleteFAQEntries(ctx context.Context,
|
||||
knowledgeBaseID string, ids []string,
|
||||
knowledgeBaseID string, ids []int64,
|
||||
) error {
|
||||
path := fmt.Sprintf("/api/v1/knowledge-bases/%s/faq/entries", knowledgeBaseID)
|
||||
resp, err := c.doRequest(ctx, http.MethodDelete, path, &FAQDeleteRequest{IDs: ids}, nil)
|
||||
@@ -339,19 +373,55 @@ func (c *Client) ExportFAQEntries(ctx context.Context, knowledgeBaseID string) (
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// FAQFailedEntry represents a failed entry during FAQ import/validation.
|
||||
type FAQFailedEntry struct {
|
||||
Index int `json:"index"`
|
||||
Reason string `json:"reason"`
|
||||
TagName string `json:"tag_name,omitempty"`
|
||||
StandardQuestion string `json:"standard_question"`
|
||||
SimilarQuestions []string `json:"similar_questions,omitempty"`
|
||||
NegativeQuestions []string `json:"negative_questions,omitempty"`
|
||||
Answers []string `json:"answers,omitempty"`
|
||||
AnswerAll bool `json:"answer_all,omitempty"`
|
||||
IsDisabled bool `json:"is_disabled,omitempty"`
|
||||
}
|
||||
|
||||
// FAQSuccessEntry represents a successfully imported FAQ entry.
|
||||
type FAQSuccessEntry struct {
|
||||
Index int `json:"index"` // Entry index in the batch (0-based)
|
||||
SeqID int64 `json:"seq_id"` // Entry sequence ID after import
|
||||
TagID int64 `json:"tag_id,omitempty"` // Tag ID (seq_id)
|
||||
TagName string `json:"tag_name,omitempty"` // Tag name
|
||||
StandardQuestion string `json:"standard_question"` // Standard question
|
||||
}
|
||||
|
||||
// FAQImportProgress represents the progress of an async FAQ import task.
|
||||
// When Status is "completed", the result fields (SkippedCount, ImportMode, ImportedAt, DisplayStatus, ProcessingTime) are populated.
|
||||
type FAQImportProgress struct {
|
||||
TaskID string `json:"task_id"`
|
||||
KBID string `json:"kb_id"`
|
||||
KnowledgeID string `json:"knowledge_id"`
|
||||
Status string `json:"status"`
|
||||
Progress int `json:"progress"`
|
||||
Total int `json:"total"`
|
||||
Processed int `json:"processed"`
|
||||
Message string `json:"message"`
|
||||
Error string `json:"error,omitempty"`
|
||||
CreatedAt int64 `json:"created_at"`
|
||||
UpdatedAt int64 `json:"updated_at"`
|
||||
TaskID string `json:"task_id"`
|
||||
KBID string `json:"kb_id"`
|
||||
KnowledgeID string `json:"knowledge_id"`
|
||||
Status string `json:"status"`
|
||||
Progress int `json:"progress"`
|
||||
Total int `json:"total"`
|
||||
Processed int `json:"processed"`
|
||||
SuccessCount int `json:"success_count"`
|
||||
FailedCount int `json:"failed_count"`
|
||||
SkippedCount int `json:"skipped_count,omitempty"`
|
||||
FailedEntries []FAQFailedEntry `json:"failed_entries,omitempty"`
|
||||
SuccessEntries []FAQSuccessEntry `json:"success_entries,omitempty"` // Successfully imported entries (when count is small)
|
||||
FailedEntriesURL string `json:"failed_entries_url,omitempty"` // CSV download URL when too many failures
|
||||
Message string `json:"message"`
|
||||
Error string `json:"error,omitempty"`
|
||||
CreatedAt int64 `json:"created_at"`
|
||||
UpdatedAt int64 `json:"updated_at"`
|
||||
DryRun bool `json:"dry_run,omitempty"` // Whether this is a dry run validation
|
||||
|
||||
// Result fields (populated when Status == "completed")
|
||||
ImportMode string `json:"import_mode,omitempty"`
|
||||
ImportedAt time.Time `json:"imported_at,omitempty"`
|
||||
DisplayStatus string `json:"display_status,omitempty"`
|
||||
ProcessingTime int64 `json:"processing_time,omitempty"`
|
||||
}
|
||||
|
||||
// FAQImportProgressResponse wraps the FAQ import progress response.
|
||||
@@ -363,6 +433,7 @@ type FAQImportProgressResponse struct {
|
||||
}
|
||||
|
||||
// GetFAQImportProgress retrieves the progress of an async FAQ import task.
|
||||
// This works for both regular imports and dry run validations.
|
||||
func (c *Client) GetFAQImportProgress(ctx context.Context, taskID string) (*FAQImportProgress, error) {
|
||||
path := fmt.Sprintf("/api/v1/faq/import/progress/%s", taskID)
|
||||
resp, err := c.doRequest(ctx, http.MethodGet, path, nil, nil)
|
||||
@@ -376,3 +447,19 @@ func (c *Client) GetFAQImportProgress(ctx context.Context, taskID string) (*FAQI
|
||||
}
|
||||
return response.Data, nil
|
||||
}
|
||||
|
||||
type updateLastFAQImportResultDisplayStatusRequest struct {
|
||||
DisplayStatus string `json:"display_status"`
|
||||
}
|
||||
|
||||
// UpdateLastFAQImportResultDisplayStatus updates the display status (open/close) of the last FAQ import result.
|
||||
func (c *Client) UpdateLastFAQImportResultDisplayStatus(ctx context.Context, knowledgeBaseID string, displayStatus string) error {
|
||||
path := fmt.Sprintf("/api/v1/knowledge-bases/%s/faq/import/last-result/display", knowledgeBaseID)
|
||||
resp, err := c.doRequest(ctx, http.MethodPut, path, &updateLastFAQImportResultDisplayStatusRequest{DisplayStatus: displayStatus}, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var response faqSimpleResponse
|
||||
return parseResponse(resp, &response)
|
||||
}
|
||||
|
||||
+19
-2
@@ -12,6 +12,7 @@ import (
|
||||
// Tag represents a knowledge base tag.
|
||||
type Tag struct {
|
||||
ID string `json:"id"`
|
||||
SeqID int64 `json:"seq_id"`
|
||||
TenantID uint64 `json:"tenant_id"`
|
||||
KnowledgeBaseID string `json:"knowledge_base_id"`
|
||||
Name string `json:"name"`
|
||||
@@ -121,6 +122,7 @@ func (c *Client) CreateTag(ctx context.Context,
|
||||
}
|
||||
|
||||
// UpdateTag updates an existing tag.
|
||||
// tagID can be either UUID or seq_id (as string).
|
||||
func (c *Client) UpdateTag(ctx context.Context,
|
||||
knowledgeBaseID, tagID string, payload *UpdateTagPayload,
|
||||
) (*Tag, error) {
|
||||
@@ -137,12 +139,20 @@ func (c *Client) UpdateTag(ctx context.Context,
|
||||
return response.Data, nil
|
||||
}
|
||||
|
||||
// UpdateTagBySeqID updates an existing tag by seq_id.
|
||||
func (c *Client) UpdateTagBySeqID(ctx context.Context,
|
||||
knowledgeBaseID string, tagSeqID int64, payload *UpdateTagPayload,
|
||||
) (*Tag, error) {
|
||||
return c.UpdateTag(ctx, knowledgeBaseID, strconv.FormatInt(tagSeqID, 10), payload)
|
||||
}
|
||||
|
||||
// DeleteTag deletes a tag.
|
||||
// tagID can be either UUID or seq_id (as string).
|
||||
// Set force to true to delete even if the tag is referenced.
|
||||
// Set contentOnly to true to only delete the content under the tag but keep the tag itself.
|
||||
// excludeIDs: IDs of chunks to exclude from deletion.
|
||||
// excludeIDs: seq_ids of chunks to exclude from deletion.
|
||||
func (c *Client) DeleteTag(ctx context.Context,
|
||||
knowledgeBaseID, tagID string, force bool, contentOnly bool, excludeIDs []string,
|
||||
knowledgeBaseID, tagID string, force bool, contentOnly bool, excludeIDs []int64,
|
||||
) error {
|
||||
path := fmt.Sprintf("/api/v1/knowledge-bases/%s/tags/%s", knowledgeBaseID, tagID)
|
||||
query := url.Values{}
|
||||
@@ -168,3 +178,10 @@ func (c *Client) DeleteTag(ctx context.Context,
|
||||
var response tagSimpleResponse
|
||||
return parseResponse(resp, &response)
|
||||
}
|
||||
|
||||
// DeleteTagBySeqID deletes a tag by seq_id.
|
||||
func (c *Client) DeleteTagBySeqID(ctx context.Context,
|
||||
knowledgeBaseID string, tagSeqID int64, force bool, contentOnly bool, excludeIDs []int64,
|
||||
) error {
|
||||
return c.DeleteTag(ctx, knowledgeBaseID, strconv.FormatInt(tagSeqID, 10), force, contentOnly, excludeIDs)
|
||||
}
|
||||
|
||||
@@ -55,7 +55,6 @@ class BaseParser(ABC):
|
||||
if not hostname:
|
||||
logger.warning("No hostname found in URL")
|
||||
return False
|
||||
|
||||
# Try to parse as IP address
|
||||
try:
|
||||
ip = ipaddress.ip_address(hostname)
|
||||
@@ -743,7 +742,6 @@ class BaseParser(ABC):
|
||||
if not self._is_safe_url(img_url):
|
||||
logger.error(f"URL failed validation check: {img_url}")
|
||||
return img_url, img_url, None
|
||||
|
||||
# Still need to get image object for OCR processing
|
||||
# Get proxy settings from environment variables
|
||||
http_proxy = os.environ.get("EXTERNAL_HTTP_PROXY")
|
||||
@@ -794,7 +792,6 @@ class BaseParser(ABC):
|
||||
if not self._is_safe_url(img_url):
|
||||
logger.error(f"URL failed validation check: {img_url}")
|
||||
return img_url, img_url, None
|
||||
|
||||
# Get proxy settings from environment variables
|
||||
http_proxy = os.environ.get("EXTERNAL_HTTP_PROXY")
|
||||
https_proxy = os.environ.get("EXTERNAL_HTTPS_PROXY")
|
||||
|
||||
+15
-2
@@ -50,10 +50,14 @@ Agent 模式支持更智能的问答,包括工具调用、网络搜索、多
|
||||
**请求参数**:
|
||||
- `query`: 查询文本(必填)
|
||||
- `knowledge_base_ids`: 知识库 ID 数组,可动态指定本次查询使用的知识库(可选)
|
||||
- `knowledge_ids`: 知识文件 ID 数组,可动态指定本次查询使用的具体知识文件(可选)
|
||||
- `agent_enabled`: 是否启用 Agent 模式(可选,默认 false)
|
||||
- `agent_id`: 自定义 Agent ID,指定使用的自定义智能体(可选)
|
||||
- `web_search_enabled`: 是否启用网络搜索(可选,默认 false)
|
||||
- `summary_model_id`: 覆盖会话默认的摘要模型 ID(可选)
|
||||
- `mcp_service_ids`: MCP 服务白名单(可选)
|
||||
- `mentioned_items`: @提及的知识库和文件列表(可选)
|
||||
- `disable_title`: 是否禁用自动标题生成(可选,默认 false)
|
||||
- `mcp_service_ids`: MCP 服务白名单(可选,已废弃)
|
||||
|
||||
**请求**:
|
||||
|
||||
@@ -65,7 +69,16 @@ curl --location 'http://localhost:8080/api/v1/agent-chat/ceb9babb-1e30-41d7-817d
|
||||
"query": "帮我查询今天的天气",
|
||||
"agent_enabled": true,
|
||||
"web_search_enabled": true,
|
||||
"knowledge_base_ids": ["kb-00000001"]
|
||||
"knowledge_base_ids": ["kb-00000001"],
|
||||
"agent_id": "agent-001",
|
||||
"mentioned_items": [
|
||||
{
|
||||
"id": "kb-00000001",
|
||||
"name": "天气知识库",
|
||||
"type": "kb",
|
||||
"kb_type": "document"
|
||||
}
|
||||
]
|
||||
}'
|
||||
```
|
||||
|
||||
|
||||
@@ -138,16 +138,16 @@ export function updateKnowledgeBaseTag(
|
||||
return put(`/api/v1/knowledge-bases/${kbId}/tags/${tagId}`, data);
|
||||
}
|
||||
|
||||
export function deleteKnowledgeBaseTag(kbId: string, tagId: string, params?: { force?: boolean }) {
|
||||
export function deleteKnowledgeBaseTag(kbId: string, tagSeqId: number, params?: { force?: boolean }) {
|
||||
const forceQuery = params?.force ? '?force=true' : '';
|
||||
return del(`/api/v1/knowledge-bases/${kbId}/tags/${tagId}${forceQuery}`);
|
||||
return del(`/api/v1/knowledge-bases/${kbId}/tags/${tagSeqId}${forceQuery}`);
|
||||
}
|
||||
|
||||
export function updateKnowledgeTagBatch(data: { updates: Record<string, string | null> }) {
|
||||
return put(`/api/v1/knowledge/tags`, data);
|
||||
}
|
||||
|
||||
export function updateFAQEntryTagBatch(kbId: string, data: { updates: Record<string, string | null> }) {
|
||||
export function updateFAQEntryTagBatch(kbId: string, data: { updates: Record<number, number | null> }) {
|
||||
return put(`/api/v1/knowledge-bases/${kbId}/faq/entries/tags`, data);
|
||||
}
|
||||
|
||||
@@ -164,7 +164,7 @@ const buildQuery = (params?: Record<string, any>) => {
|
||||
|
||||
export function listFAQEntries(
|
||||
kbId: string,
|
||||
params?: { page?: number; page_size?: number; tag_id?: string; keyword?: string },
|
||||
params?: { page?: number; page_size?: number; tag_id?: number; keyword?: string },
|
||||
) {
|
||||
const query = buildQuery(params);
|
||||
return get(`/api/v1/knowledge-bases/${kbId}/faq/entries${query}`);
|
||||
@@ -178,7 +178,7 @@ export function createFAQEntry(kbId: string, data: any) {
|
||||
return post(`/api/v1/knowledge-bases/${kbId}/faq/entry`, data);
|
||||
}
|
||||
|
||||
export function updateFAQEntry(kbId: string, entryId: string, data: any) {
|
||||
export function updateFAQEntry(kbId: string, entryId: number, data: any) {
|
||||
return put(`/api/v1/knowledge-bases/${kbId}/faq/entries/${entryId}`, data);
|
||||
}
|
||||
|
||||
@@ -189,19 +189,20 @@ export function updateFAQEntry(kbId: string, entryId: string, data: any) {
|
||||
export interface FAQEntryFieldsUpdate {
|
||||
is_enabled?: boolean
|
||||
is_recommended?: boolean
|
||||
tag_id?: string | null
|
||||
tag_id?: number | null
|
||||
}
|
||||
|
||||
export interface FAQEntryFieldsBatchRequest {
|
||||
by_id?: Record<string, FAQEntryFieldsUpdate>
|
||||
by_tag?: Record<string, FAQEntryFieldsUpdate>
|
||||
by_id?: Record<number, FAQEntryFieldsUpdate>
|
||||
by_tag?: Record<number, FAQEntryFieldsUpdate>
|
||||
exclude_ids?: number[]
|
||||
}
|
||||
|
||||
export function updateFAQEntryFieldsBatch(kbId: string, data: FAQEntryFieldsBatchRequest) {
|
||||
return put(`/api/v1/knowledge-bases/${kbId}/faq/entries/fields`, data);
|
||||
}
|
||||
|
||||
export function deleteFAQEntries(kbId: string, ids: string[]) {
|
||||
export function deleteFAQEntries(kbId: string, ids: number[]) {
|
||||
return del(`/api/v1/knowledge-bases/${kbId}/faq/entries`, { ids });
|
||||
}
|
||||
|
||||
@@ -223,6 +224,20 @@ export async function exportFAQEntries(kbId: string): Promise<Blob> {
|
||||
}
|
||||
|
||||
// FAQ Import Progress API
|
||||
export interface FAQBlockedEntry {
|
||||
index: number
|
||||
standard_question: string
|
||||
reason: string
|
||||
}
|
||||
|
||||
export interface FAQSuccessEntry {
|
||||
index: number
|
||||
seq_id: number
|
||||
tag_id?: number
|
||||
tag_name?: string
|
||||
standard_question: string
|
||||
}
|
||||
|
||||
export interface FAQImportProgress {
|
||||
task_id: string
|
||||
kb_id: string
|
||||
@@ -231,6 +246,9 @@ export interface FAQImportProgress {
|
||||
progress: number
|
||||
total: number
|
||||
processed: number
|
||||
blocked: number
|
||||
blocked_entries?: FAQBlockedEntry[]
|
||||
success_entries?: FAQSuccessEntry[]
|
||||
message: string
|
||||
error: string
|
||||
created_at: number
|
||||
@@ -241,6 +259,12 @@ export function getFAQImportProgress(taskId: string) {
|
||||
return get(`/api/v1/faq/import/progress/${taskId}`);
|
||||
}
|
||||
|
||||
export function updateFAQImportResultDisplayStatus(knowledgeBaseId: string, displayStatus: 'open' | 'close') {
|
||||
return put(`/api/v1/knowledge-bases/${knowledgeBaseId}/faq/import/last-result/display`, {
|
||||
display_status: displayStatus
|
||||
});
|
||||
}
|
||||
|
||||
export function searchKnowledge(keyword: string, offset = 0, limit = 20, fileTypes?: string[]) {
|
||||
let url = `/api/v1/knowledge/search?keyword=${encodeURIComponent(keyword)}&offset=${offset}&limit=${limit}`;
|
||||
if (fileTypes && fileTypes.length > 0) {
|
||||
|
||||
@@ -179,12 +179,13 @@ const loadTenants = async (append = false) => {
|
||||
|
||||
loading.value = true
|
||||
try {
|
||||
let keyword = searchQuery.value.trim()
|
||||
const keyword = searchQuery.value.trim()
|
||||
let tenantID: number | undefined = undefined
|
||||
|
||||
// 如果是纯数字,同时作为 tenant_id 和 keyword 搜索
|
||||
// 这样既能精确匹配租户ID,也能模糊匹配名称中包含数字的租户
|
||||
if (keyword && /^\d+$/.test(keyword)) {
|
||||
tenantID = Number(keyword)
|
||||
keyword = ''
|
||||
}
|
||||
|
||||
const response = await searchTenants({
|
||||
|
||||
@@ -39,6 +39,7 @@ export default {
|
||||
tagEditSuccess: 'Tag updated',
|
||||
tagDeleteTitle: 'Delete tag',
|
||||
tagDeleteDesc: 'Delete tag "{name}"? All FAQ entries under this tag will also be deleted.',
|
||||
tagDeleteDescDoc: 'Delete tag "{name}"? All documents under this tag will also be deleted.',
|
||||
tagDeleteSuccess: 'Tag deleted',
|
||||
tagEditAction: 'Rename',
|
||||
tagDeleteAction: 'Delete',
|
||||
@@ -1114,6 +1115,7 @@ export default {
|
||||
questionIndexModeDescription: 'Combined: Standard and similar questions are indexed together. Separate: Each question is indexed independently for more precise retrieval but requires more storage.',
|
||||
entryGuide: 'Each FAQ entry contains a primary question, similar questions, negative examples, and multiple answers. Manage them in the FAQ knowledge base detail view.',
|
||||
tagDesc: 'Select category for FAQ entries',
|
||||
tagPlaceholder: 'Please select a category',
|
||||
modes: {
|
||||
questionOnly: 'Questions only',
|
||||
questionAnswer: 'Question + answer',
|
||||
|
||||
@@ -38,6 +38,7 @@ export default {
|
||||
tagEditSuccess: "태그 업데이트 성공",
|
||||
tagDeleteTitle: "태그 삭제",
|
||||
tagDeleteDesc: '"{name}" 태그를 삭제하시겠습니까? 해당 태그의 모든 FAQ 항목이 함께 삭제됩니다',
|
||||
tagDeleteDescDoc: '"{name}" 태그를 삭제하시겠습니까? 해당 태그의 모든 문서가 함께 삭제됩니다',
|
||||
tagDeleteSuccess: "태그가 삭제되었습니다",
|
||||
tagEditAction: "이름 변경",
|
||||
tagDeleteAction: "삭제",
|
||||
@@ -1319,6 +1320,7 @@ export default {
|
||||
questionIndexModeDescription: "병합 인덱스: 표준 질문과 유사 질문을 병합 인덱싱; 개별 인덱스: 표준 질문과 각 유사 질문을 독립적으로 인덱싱하여 더 정확하게 검색하지만 더 많은 저장 공간이 필요합니다",
|
||||
entryGuide: "FAQ 항목은 표준 질문, 유사 질문, 반례 및 여러 답변으로 구성됩니다. 지식베이스 세부 정보에서 일괄 가져오기 및 편집할 수 있습니다.",
|
||||
tagDesc: "FAQ 항목에 분류 선택",
|
||||
tagPlaceholder: "분류를 선택하세요",
|
||||
modes: {
|
||||
questionOnly: "표준 질문/유사 질문만",
|
||||
questionAnswer: "표준 질문 + 답변",
|
||||
|
||||
@@ -38,6 +38,7 @@ export default {
|
||||
tagEditSuccess: 'Тег обновлён',
|
||||
tagDeleteTitle: 'Удаление тега',
|
||||
tagDeleteDesc: 'Удалить тег «{name}»? Все записи FAQ под этим тегом также будут удалены.',
|
||||
tagDeleteDescDoc: 'Удалить тег «{name}»? Все документы под этим тегом также будут удалены.',
|
||||
tagDeleteSuccess: 'Тег удалён',
|
||||
tagEditAction: 'Переименовать',
|
||||
tagDeleteAction: 'Удалить',
|
||||
@@ -1080,6 +1081,7 @@ export default {
|
||||
questionIndexModeDescription: 'Объединенная: стандартные и похожие вопросы индексируются вместе. Раздельная: каждый вопрос индексируется независимо для более точного поиска, но требует больше места.',
|
||||
entryGuide: 'Каждый FAQ включает основной вопрос, похожие вопросы, негативные примеры и несколько ответов. Управляйте ими в деталях FAQ-базы.',
|
||||
tagDesc: 'Выберите категорию для записей FAQ',
|
||||
tagPlaceholder: 'Пожалуйста, выберите категорию',
|
||||
modes: {
|
||||
questionOnly: 'Только вопросы',
|
||||
questionAnswer: 'Вопрос + ответ',
|
||||
|
||||
@@ -39,6 +39,7 @@ export default {
|
||||
tagEditSuccess: "标签更新成功",
|
||||
tagDeleteTitle: "删除标签",
|
||||
tagDeleteDesc: '确定删除标签"{name}"?该标签下的所有 FAQ 条目将被一并删除',
|
||||
tagDeleteDescDoc: '确定删除标签"{name}"?该标签下的所有文档将被一并删除',
|
||||
tagDeleteSuccess: "标签已删除",
|
||||
tagEditAction: "重命名",
|
||||
tagDeleteAction: "删除",
|
||||
@@ -1511,6 +1512,7 @@ export default {
|
||||
questionIndexModeDescription: "合并索引:标准问和相似问合并索引;分别索引:标准问和每个相似问独立索引,检索更精确但需要更多存储",
|
||||
entryGuide: "FAQ 条目由标准问、相似问、反例和多个答案组成,可在知识库详情中批量导入、编辑。",
|
||||
tagDesc: "为 FAQ 条目选择分类",
|
||||
tagPlaceholder: "请选择分类",
|
||||
modes: {
|
||||
questionOnly: "仅标准问/相似问",
|
||||
questionAnswer: "标准问 + 答案",
|
||||
|
||||
@@ -46,7 +46,7 @@ let knowledgeScroll = ref()
|
||||
let page = 1;
|
||||
let pageSize = 35;
|
||||
|
||||
const selectedTagId = ref<string>("__untagged__");
|
||||
const selectedTagId = ref<string>('');
|
||||
const tagList = ref<any[]>([]);
|
||||
const tagLoading = ref(false);
|
||||
const tagSearchQuery = ref('');
|
||||
@@ -59,7 +59,6 @@ let tagSearchDebounce: ReturnType<typeof setTimeout> | null = null;
|
||||
let docSearchDebounce: ReturnType<typeof setTimeout> | null = null;
|
||||
const docSearchKeyword = ref('');
|
||||
const selectedFileType = ref('');
|
||||
const UNTAGGED_TAG_ID = '__untagged__';
|
||||
const fileTypeOptions = computed(() => [
|
||||
{ content: t('knowledgeBase.allFileTypes') || '全部类型', value: '' },
|
||||
{ content: 'PDF', value: 'pdf' },
|
||||
@@ -71,18 +70,12 @@ const fileTypeOptions = computed(() => [
|
||||
{ content: t('knowledgeBase.typeManual') || '手动创建', value: 'manual' },
|
||||
]);
|
||||
type TagInputInstance = ComponentPublicInstance<{ focus: () => void; select: () => void }>;
|
||||
const tagDropdownOptions = computed(() => {
|
||||
const options = [
|
||||
{ content: t('knowledgeBase.untagged') || '未分类', value: UNTAGGED_TAG_ID },
|
||||
...tagList.value
|
||||
.filter((tag: any) => tag.id !== UNTAGGED_TAG_ID)
|
||||
.map((tag: any) => ({
|
||||
content: tag.name,
|
||||
value: tag.id,
|
||||
})),
|
||||
];
|
||||
return options;
|
||||
});
|
||||
const tagDropdownOptions = computed(() =>
|
||||
tagList.value.map((tag: any) => ({
|
||||
content: tag.name,
|
||||
value: tag.id,
|
||||
})),
|
||||
);
|
||||
const tagMap = computed<Record<string, any>>(() => {
|
||||
const map: Record<string, any> = {};
|
||||
tagList.value.forEach((tag) => {
|
||||
@@ -124,10 +117,9 @@ const getPageSize = () => {
|
||||
getPageSize()
|
||||
// 直接调用 API 获取知识库文件列表
|
||||
const getTagName = (tagId?: string | number) => {
|
||||
if (!tagId && tagId !== 0) return t('knowledgeBase.untagged') || '未分类';
|
||||
if (tagId === UNTAGGED_TAG_ID) return t('knowledgeBase.untagged') || '未分类';
|
||||
if (!tagId && tagId !== 0) return '';
|
||||
const key = String(tagId);
|
||||
return tagMap.value[key]?.name || (t('knowledgeBase.untagged') || '未分类');
|
||||
return tagMap.value[key]?.name || '';
|
||||
};
|
||||
|
||||
const formatDocTime = (time?: string) => {
|
||||
@@ -227,22 +219,6 @@ const handleTagFilterChange = (value: string) => {
|
||||
};
|
||||
|
||||
const handleTagRowClick = (tagId: string) => {
|
||||
const normalizedId = String(tagId);
|
||||
if (editingTagId.value && editingTagId.value !== normalizedId) {
|
||||
editingTagId.value = null;
|
||||
editingTagName.value = '';
|
||||
}
|
||||
if (creatingTag.value) {
|
||||
creatingTag.value = false;
|
||||
newTagName.value = '';
|
||||
}
|
||||
if (selectedTagId.value === normalizedId) {
|
||||
return;
|
||||
}
|
||||
handleTagFilterChange(normalizedId);
|
||||
};
|
||||
|
||||
const handleUntaggedClick = () => {
|
||||
if (creatingTag.value) {
|
||||
creatingTag.value = false;
|
||||
newTagName.value = '';
|
||||
@@ -251,8 +227,8 @@ const handleUntaggedClick = () => {
|
||||
editingTagId.value = null;
|
||||
editingTagName.value = '';
|
||||
}
|
||||
if (selectedTagId.value === UNTAGGED_TAG_ID) return;
|
||||
handleTagFilterChange(UNTAGGED_TAG_ID);
|
||||
if (selectedTagId.value === tagId) return;
|
||||
handleTagFilterChange(tagId);
|
||||
};
|
||||
|
||||
const startCreateTag = () => {
|
||||
@@ -354,18 +330,24 @@ const confirmDeleteTag = (tag: any) => {
|
||||
if (editingTagId.value) {
|
||||
cancelEditTag();
|
||||
}
|
||||
const deleteDescKey = isFAQ.value ? 'knowledgeBase.tagDeleteDesc' : 'knowledgeBase.tagDeleteDescDoc';
|
||||
const confirm = window.confirm(
|
||||
t('knowledgeBase.tagDeleteDesc', { name: tag.name }) as string,
|
||||
t(deleteDescKey, { name: tag.name }) as string,
|
||||
);
|
||||
if (!confirm) return;
|
||||
deleteKnowledgeBaseTag(kbId.value, tag.id, { force: true })
|
||||
deleteKnowledgeBaseTag(kbId.value, tag.seq_id, { force: true })
|
||||
.then(() => {
|
||||
MessagePlugin.success(t('knowledgeBase.tagDeleteSuccess'));
|
||||
if (selectedTagId.value === tag.id) {
|
||||
handleTagFilterChange(UNTAGGED_TAG_ID);
|
||||
// Reset to show all entries when current tag is deleted
|
||||
selectedTagId.value = '';
|
||||
handleTagFilterChange('');
|
||||
}
|
||||
loadTags(kbId.value);
|
||||
loadKnowledgeFiles(kbId.value);
|
||||
// 由于后端是异步删除文档,延迟刷新以确保看到最新数据
|
||||
setTimeout(() => {
|
||||
loadKnowledgeFiles(kbId.value);
|
||||
}, 500);
|
||||
})
|
||||
.catch((error: any) => {
|
||||
MessagePlugin.error(error?.message || t('common.operationFailed'));
|
||||
@@ -374,8 +356,8 @@ const confirmDeleteTag = (tag: any) => {
|
||||
|
||||
const handleKnowledgeTagChange = async (knowledgeId: string, tagValue: string) => {
|
||||
try {
|
||||
// When selecting "untagged", pass null to clear the tag
|
||||
const tagIdToUpdate = tagValue === UNTAGGED_TAG_ID ? null : (tagValue || null);
|
||||
// Pass the tag value directly (empty string means no tag)
|
||||
const tagIdToUpdate = tagValue || null;
|
||||
await updateKnowledgeTagBatch({ updates: { [knowledgeId]: tagIdToUpdate } });
|
||||
MessagePlugin.success(t('knowledgeBase.tagUpdateSuccess') || '分类已更新');
|
||||
loadKnowledgeFiles(kbId.value);
|
||||
@@ -394,7 +376,7 @@ const loadKnowledgeBaseInfo = async (targetKbId: string) => {
|
||||
try {
|
||||
const res: any = await getKnowledgeBaseById(targetKbId);
|
||||
kbInfo.value = res?.data || null;
|
||||
selectedTagId.value = UNTAGGED_TAG_ID;
|
||||
selectedTagId.value = '';
|
||||
if (!isFAQ.value) {
|
||||
loadKnowledgeFiles(targetKbId);
|
||||
} else {
|
||||
@@ -1053,16 +1035,12 @@ async function createNewSession(value: string): Promise<void> {
|
||||
v-for="tag in filteredTags"
|
||||
:key="tag.id"
|
||||
class="tag-list-item"
|
||||
:class="{ active: selectedTagId === tag.id, editing: editingTagId === tag.id && tag.id !== UNTAGGED_TAG_ID }"
|
||||
@click="tag.id === UNTAGGED_TAG_ID ? handleUntaggedClick() : handleTagRowClick(tag.id)"
|
||||
:class="{ active: selectedTagId === tag.id, editing: editingTagId === tag.id }"
|
||||
@click="handleTagRowClick(tag.id)"
|
||||
>
|
||||
<div class="tag-list-left">
|
||||
<t-icon name="folder" size="18px" />
|
||||
<!-- Untagged pseudo-tag: show translated name, no editing -->
|
||||
<template v-if="tag.id === UNTAGGED_TAG_ID">
|
||||
<span class="tag-name">{{ $t('knowledgeBase.untagged') || '未分类' }}</span>
|
||||
</template>
|
||||
<template v-else-if="editingTagId === tag.id">
|
||||
<template v-if="editingTagId === tag.id">
|
||||
<div class="tag-edit-input" @click.stop>
|
||||
<t-input
|
||||
:ref="setEditingTagInputRefByTag(tag.id)"
|
||||
@@ -1080,11 +1058,7 @@ async function createNewSession(value: string): Promise<void> {
|
||||
</div>
|
||||
<div class="tag-list-right">
|
||||
<span class="tag-count">{{ tag.knowledge_count || 0 }}</span>
|
||||
<!-- Untagged pseudo-tag: no edit/delete actions -->
|
||||
<template v-if="tag.id === UNTAGGED_TAG_ID">
|
||||
<!-- Empty placeholder for alignment -->
|
||||
</template>
|
||||
<template v-else-if="editingTagId === tag.id">
|
||||
<template v-if="editingTagId === tag.id">
|
||||
<div class="tag-inline-actions" @click.stop>
|
||||
<t-button
|
||||
variant="text"
|
||||
|
||||
@@ -54,6 +54,66 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 导入结果统计(持久化显示) -->
|
||||
<div v-if="importResult && importResult.display_status === 'open' && !importState.taskId" class="faq-import-result-card">
|
||||
<div class="import-result-content">
|
||||
<div class="import-result-header">
|
||||
<div class="header-left">
|
||||
<t-icon name="check-circle-filled" size="20px" class="result-icon" />
|
||||
<span class="result-title">最近导入结果</span>
|
||||
</div>
|
||||
<div class="header-right">
|
||||
<span class="result-time">{{ formatImportTime(importResult.imported_at) }}</span>
|
||||
<t-button
|
||||
variant="text"
|
||||
theme="default"
|
||||
size="small"
|
||||
class="result-close-btn"
|
||||
@click="closeImportResult"
|
||||
>
|
||||
<t-icon name="close" size="16px" />
|
||||
</t-button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="import-result-body">
|
||||
<div class="import-result-stats">
|
||||
<div class="stat-item">
|
||||
<span class="stat-label">导入数据</span>
|
||||
<span class="stat-value">{{ importResult.total_entries }}条</span>
|
||||
</div>
|
||||
<div class="stat-item success">
|
||||
<span class="stat-label">成功</span>
|
||||
<span class="stat-value">{{ importResult.success_count }}条</span>
|
||||
</div>
|
||||
<div v-if="importResult.failed_count > 0" class="stat-item failed">
|
||||
<span class="stat-label">失败</span>
|
||||
<span class="stat-value">{{ importResult.failed_count }}条</span>
|
||||
<t-button
|
||||
v-if="importResult.failed_entries_url"
|
||||
variant="outline"
|
||||
theme="danger"
|
||||
size="small"
|
||||
class="download-failed-btn"
|
||||
@click="downloadFailedEntries"
|
||||
>
|
||||
<t-icon name="download" size="14px" />
|
||||
下载原因
|
||||
</t-button>
|
||||
</div>
|
||||
<div v-if="importResult.skipped_count > 0" class="stat-item skipped">
|
||||
<span class="stat-label">跳过</span>
|
||||
<span class="stat-value">{{ importResult.skipped_count }}条</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="import-mode-tag">
|
||||
<t-tag size="small" variant="light" theme="success">
|
||||
{{ importResult.import_mode === 'append' ? '追加模式' : '替换模式' }}
|
||||
</t-tag>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 导入进度条(显示在列表页面顶部) -->
|
||||
<div v-if="importState.taskId && importState.taskStatus" class="faq-import-progress-bar">
|
||||
<div class="progress-bar-content">
|
||||
@@ -183,16 +243,12 @@
|
||||
v-for="tag in filteredTags"
|
||||
:key="tag.id"
|
||||
class="faq-tag-item"
|
||||
:class="{ active: selectedTagId === tag.id, editing: editingTagId === tag.id && tag.id !== UNTAGGED_FILTER }"
|
||||
@click="tag.id === UNTAGGED_FILTER ? handleUntaggedClick() : handleTagRowClick(tag.id)"
|
||||
:class="{ active: selectedTagId === tag.seq_id, editing: editingTagId === tag.id }"
|
||||
@click="handleTagRowClick(tag.seq_id)"
|
||||
>
|
||||
<div class="faq-tag-left">
|
||||
<t-icon name="folder" size="18px" />
|
||||
<!-- Untagged pseudo-tag: show translated name, no editing -->
|
||||
<template v-if="tag.id === UNTAGGED_FILTER">
|
||||
<span class="tag-name">{{ $t('knowledgeBase.untagged') }}</span>
|
||||
</template>
|
||||
<template v-else-if="editingTagId === tag.id">
|
||||
<template v-if="editingTagId === tag.id">
|
||||
<div class="tag-edit-input" @click.stop>
|
||||
<t-input
|
||||
:ref="setEditingTagInputRefByTag(tag.id)"
|
||||
@@ -210,11 +266,7 @@
|
||||
</div>
|
||||
<div class="faq-tag-right">
|
||||
<span class="faq-tag-count">{{ tag.chunk_count || 0 }}</span>
|
||||
<!-- Untagged pseudo-tag: no edit/delete actions, just a placeholder -->
|
||||
<template v-if="tag.id === UNTAGGED_FILTER">
|
||||
<div class="tag-more-placeholder"></div>
|
||||
</template>
|
||||
<template v-else-if="editingTagId === tag.id">
|
||||
<template v-if="editingTagId === tag.id">
|
||||
<div class="tag-inline-actions" @click.stop>
|
||||
<t-button
|
||||
variant="text"
|
||||
@@ -1120,6 +1172,7 @@ import {
|
||||
getKnowledgeBaseById,
|
||||
listKnowledgeBases,
|
||||
getFAQImportProgress,
|
||||
updateFAQImportResultDisplayStatus,
|
||||
} from '@/api/knowledge-base'
|
||||
import * as XLSX from 'xlsx'
|
||||
import Papa from 'papaparse'
|
||||
@@ -1127,11 +1180,11 @@ import FAQTagTooltip from '@/components/FAQTagTooltip.vue'
|
||||
import { useUIStore } from '@/stores/ui'
|
||||
|
||||
interface FAQEntry {
|
||||
id: string
|
||||
id: number
|
||||
chunk_id: string
|
||||
knowledge_id: string
|
||||
knowledge_base_id: string
|
||||
tag_id?: string
|
||||
tag_id?: number
|
||||
is_enabled: boolean
|
||||
is_recommended: boolean
|
||||
standard_question: string
|
||||
@@ -1153,7 +1206,7 @@ interface FAQEntryPayload {
|
||||
similar_questions: string[]
|
||||
negative_questions: string[]
|
||||
answers: string[]
|
||||
tag_id?: string
|
||||
tag_id?: number
|
||||
tag_name?: string
|
||||
is_enabled?: boolean
|
||||
is_recommended?: boolean
|
||||
@@ -1170,9 +1223,9 @@ const uiStore = useUIStore()
|
||||
const loading = ref(false)
|
||||
const loadingMore = ref(false)
|
||||
const entries = ref<FAQEntry[]>([])
|
||||
const entryStatusLoading = reactive<Record<string, boolean>>({})
|
||||
const entryRecommendedLoading = reactive<Record<string, boolean>>({})
|
||||
const selectedRowKeys = ref<string[]>([])
|
||||
const entryStatusLoading = reactive<Record<number, boolean>>({})
|
||||
const entryRecommendedLoading = reactive<Record<number, boolean>>({})
|
||||
const selectedRowKeys = ref<number[]>([])
|
||||
const scrollContainer = ref<HTMLElement | null>(null)
|
||||
const cardListRef = ref<HTMLElement | null>(null)
|
||||
const hasMore = ref(true)
|
||||
@@ -1185,9 +1238,8 @@ type TagInputInstance = ComponentPublicInstance<{ focus: () => void; select: ()
|
||||
const tagList = ref<any[]>([])
|
||||
const tagLoading = ref(false)
|
||||
const tagListRef = ref<HTMLElement | null>(null)
|
||||
// Special value to represent "untagged" filter - must match backend constant
|
||||
const UNTAGGED_FILTER = '__untagged__'
|
||||
const selectedTagId = ref<string>(UNTAGGED_FILTER)
|
||||
// Selected tag seq_id for filtering (0 means show all)
|
||||
const selectedTagId = ref<number>(0)
|
||||
const overallFAQTotal = ref(0)
|
||||
const tagSearchQuery = ref('')
|
||||
const TAG_PAGE_SIZE = 20
|
||||
@@ -1214,23 +1266,30 @@ const newTagName = ref('')
|
||||
const editingTagId = ref<string | null>(null)
|
||||
const editingTagName = ref('')
|
||||
const editingTagSubmitting = ref(false)
|
||||
const tagMap = computed<Record<string, any>>(() => {
|
||||
// tagMap uses seq_id as key for looking up by entry.tag_id
|
||||
const tagMap = computed<Record<number, any>>(() => {
|
||||
const map: Record<number, any> = {}
|
||||
tagList.value.forEach((tag) => {
|
||||
map[tag.seq_id] = tag
|
||||
})
|
||||
return map
|
||||
})
|
||||
// tagMapById uses UUID as key for editing operations
|
||||
const tagMapById = computed<Record<string, any>>(() => {
|
||||
const map: Record<string, any> = {}
|
||||
tagList.value.forEach((tag) => {
|
||||
map[tag.id] = tag
|
||||
})
|
||||
return map
|
||||
})
|
||||
// Filter out the __untagged__ pseudo-tag for tag select options (when assigning tags)
|
||||
const regularTags = computed(() => tagList.value.filter((tag) => tag.id !== UNTAGGED_FILTER))
|
||||
const tagDropdownOptions = computed(() => [
|
||||
{ content: t('knowledgeBase.untagged') || '未分类', value: '' },
|
||||
...regularTags.value.map((tag: any) => ({ content: tag.name, value: tag.id })),
|
||||
])
|
||||
const tagSelectOptions = computed(() => [
|
||||
{ label: t('knowledgeBase.untagged') || '未分类', value: '' },
|
||||
...regularTags.value.map((tag: any) => ({ label: tag.name, value: tag.id })),
|
||||
])
|
||||
// All tags are now regular tags (no pseudo-tag)
|
||||
const regularTags = computed(() => tagList.value)
|
||||
const tagDropdownOptions = computed(() =>
|
||||
regularTags.value.map((tag: any) => ({ content: tag.name, value: String(tag.seq_id) })),
|
||||
)
|
||||
const tagSelectOptions = computed(() =>
|
||||
regularTags.value.map((tag: any) => ({ label: tag.name, value: tag.seq_id })),
|
||||
)
|
||||
const sidebarCategoryCount = computed(() => tagList.value.length)
|
||||
const filteredTags = computed(() => {
|
||||
const query = tagSearchQuery.value.trim().toLowerCase()
|
||||
@@ -1282,13 +1341,13 @@ const loadKnowledgeList = async () => {
|
||||
|
||||
const editorVisible = ref(false)
|
||||
const editorMode = ref<'create' | 'edit'>('create')
|
||||
const currentEntryId = ref<string | null>(null)
|
||||
const currentEntryId = ref<number | null>(null)
|
||||
const editorForm = reactive<FAQEntryPayload>({
|
||||
standard_question: '',
|
||||
similar_questions: [],
|
||||
negative_questions: [],
|
||||
answers: [],
|
||||
tag_id: '',
|
||||
tag_id: undefined,
|
||||
})
|
||||
const editorFormRef = ref<FormInstanceFunctions>()
|
||||
const savingEntry = ref(false)
|
||||
@@ -1316,6 +1375,27 @@ const importState = reactive({
|
||||
pollingInterval: null as ReturnType<typeof setInterval> | null,
|
||||
})
|
||||
|
||||
// FAQ导入结果状态(持久化的)
|
||||
const importResult = ref<{
|
||||
total_entries: number
|
||||
success_count: number
|
||||
failed_count: number
|
||||
skipped_count: number
|
||||
import_mode: string
|
||||
imported_at: string
|
||||
task_id: string
|
||||
processing_time: number
|
||||
failed_entries_url?: string
|
||||
success_entries?: Array<{
|
||||
index: number
|
||||
seq_id: number
|
||||
tag_id?: number
|
||||
tag_name?: string
|
||||
standard_question: string
|
||||
}>
|
||||
display_status: string
|
||||
} | null>(null)
|
||||
|
||||
// Search test state
|
||||
const searchDrawerVisible = ref(false)
|
||||
const searching = ref(false)
|
||||
@@ -1395,38 +1475,26 @@ const loadTags = async (reset = false) => {
|
||||
}
|
||||
}
|
||||
|
||||
const getTagName = (tagId?: string) => {
|
||||
const getTagName = (tagId?: number) => {
|
||||
if (!tagId) return t('knowledgeBase.untagged') || '未分类'
|
||||
return tagMap.value[tagId]?.name || (t('knowledgeBase.untagged') || '未分类')
|
||||
}
|
||||
|
||||
const handleTagFilterChange = (value: string) => {
|
||||
const handleTagFilterChange = (value: number) => {
|
||||
selectedTagId.value = value
|
||||
}
|
||||
|
||||
const handleTagRowClick = (tagId: string) => {
|
||||
const normalizedId = String(tagId)
|
||||
if (editingTagId.value && editingTagId.value !== normalizedId) {
|
||||
cancelEditTag()
|
||||
}
|
||||
if (creatingTag.value) {
|
||||
cancelCreateTag()
|
||||
}
|
||||
if (selectedTagId.value === normalizedId) {
|
||||
return
|
||||
}
|
||||
handleTagFilterChange(normalizedId)
|
||||
}
|
||||
|
||||
const handleUntaggedClick = () => {
|
||||
if (creatingTag.value) {
|
||||
cancelCreateTag()
|
||||
}
|
||||
const handleTagRowClick = (tagSeqId: number) => {
|
||||
if (editingTagId.value) {
|
||||
cancelEditTag()
|
||||
}
|
||||
if (selectedTagId.value === UNTAGGED_FILTER) return
|
||||
handleTagFilterChange(UNTAGGED_FILTER)
|
||||
if (creatingTag.value) {
|
||||
cancelCreateTag()
|
||||
}
|
||||
if (selectedTagId.value === tagSeqId) {
|
||||
return
|
||||
}
|
||||
handleTagFilterChange(tagSeqId)
|
||||
}
|
||||
|
||||
const startCreateTag = () => {
|
||||
@@ -1498,7 +1566,7 @@ const submitEditTag = async () => {
|
||||
MessagePlugin.warning(t('knowledgeBase.tagNameRequired'))
|
||||
return
|
||||
}
|
||||
if (name === tagMap.value[editingTagId.value]?.name) {
|
||||
if (name === tagMapById.value[editingTagId.value]?.name) {
|
||||
cancelEditTag()
|
||||
return
|
||||
}
|
||||
@@ -1533,10 +1601,12 @@ const confirmDeleteTag = (tag: any) => {
|
||||
cancelBtn: t('common.cancel'),
|
||||
onConfirm: async () => {
|
||||
try {
|
||||
await deleteKnowledgeBaseTag(props.kbId, tag.id, { force: true })
|
||||
await deleteKnowledgeBaseTag(props.kbId, tag.seq_id, { force: true })
|
||||
MessagePlugin.success(t('knowledgeBase.tagDeleteSuccess'))
|
||||
if (selectedTagId.value === tag.id) {
|
||||
handleTagFilterChange(UNTAGGED_FILTER)
|
||||
if (selectedTagId.value === tag.seq_id) {
|
||||
// Reset to show all entries when current tag is deleted
|
||||
selectedTagId.value = 0
|
||||
handleTagFilterChange(0)
|
||||
}
|
||||
await loadTags()
|
||||
await loadEntries()
|
||||
@@ -1548,16 +1618,16 @@ const confirmDeleteTag = (tag: any) => {
|
||||
})
|
||||
}
|
||||
|
||||
const handleEntryTagChange = async (entryId: string, value?: string) => {
|
||||
const handleEntryTagChange = async (entryId: number, value?: string) => {
|
||||
if (!props.kbId) return
|
||||
const targetEntry = entries.value.find((item) => item.id === entryId)
|
||||
const previousTagId = targetEntry ? targetEntry.tag_id : ''
|
||||
const normalizedValue = value ?? ''
|
||||
const previousTagId = targetEntry ? targetEntry.tag_id : undefined
|
||||
const normalizedValue = value ? Number(value) : null
|
||||
if (normalizedValue === previousTagId) {
|
||||
return
|
||||
}
|
||||
try {
|
||||
await updateFAQEntryTagBatch(props.kbId, { updates: { [entryId]: normalizedValue || null } })
|
||||
await updateFAQEntryTagBatch(props.kbId, { updates: { [entryId]: normalizedValue } })
|
||||
MessagePlugin.success(t('knowledgeEditor.messages.updateSuccess'))
|
||||
await loadEntries()
|
||||
await loadTags()
|
||||
@@ -1703,7 +1773,7 @@ const loadEntries = async (append = false) => {
|
||||
entries.value = []
|
||||
selectedRowKeys.value = []
|
||||
Object.keys(entryStatusLoading).forEach((key) => {
|
||||
delete entryStatusLoading[key]
|
||||
delete entryStatusLoading[Number(key)]
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1734,7 +1804,6 @@ const loadEntries = async (append = false) => {
|
||||
similarCollapsed: true, // 相似问默认折叠
|
||||
negativeCollapsed: true, // 反例默认折叠
|
||||
answersCollapsed: true, // 答案默认折叠
|
||||
tag_id: entry.tag_id ? String(entry.tag_id) : '',
|
||||
is_enabled: entry.is_enabled !== false,
|
||||
}))
|
||||
|
||||
@@ -1794,7 +1863,7 @@ const checkAndLoadMore = () => {
|
||||
}
|
||||
}
|
||||
|
||||
const handleCardSelect = (entryId: string, checked: boolean) => {
|
||||
const handleCardSelect = (entryId: number, checked: boolean) => {
|
||||
if (checked) {
|
||||
if (!selectedRowKeys.value.includes(entryId)) {
|
||||
selectedRowKeys.value.push(entryId)
|
||||
@@ -1812,7 +1881,7 @@ const resetEditorForm = () => {
|
||||
editorForm.similar_questions = []
|
||||
editorForm.negative_questions = []
|
||||
editorForm.answers = []
|
||||
editorForm.tag_id = ''
|
||||
editorForm.tag_id = undefined
|
||||
answerInput.value = ''
|
||||
similarInput.value = ''
|
||||
negativeInput.value = ''
|
||||
@@ -1826,7 +1895,7 @@ const openEditor = (entry?: FAQEntry) => {
|
||||
editorForm.similar_questions = [...(entry.similar_questions || [])]
|
||||
editorForm.negative_questions = [...(entry.negative_questions || [])]
|
||||
editorForm.answers = [...(entry.answers || [])]
|
||||
editorForm.tag_id = entry.tag_id || ''
|
||||
editorForm.tag_id = entry.tag_id || undefined
|
||||
} else {
|
||||
editorMode.value = 'create'
|
||||
currentEntryId.value = null
|
||||
@@ -1901,7 +1970,7 @@ const handleSubmitEntry = async () => {
|
||||
similar_questions: [...editorForm.similar_questions],
|
||||
negative_questions: [...editorForm.negative_questions],
|
||||
answers: [...editorForm.answers],
|
||||
tag_id: editorForm.tag_id || '',
|
||||
tag_id: editorForm.tag_id || undefined,
|
||||
}
|
||||
if (editorMode.value === 'create') {
|
||||
await createFAQEntry(props.kbId, payload)
|
||||
@@ -1944,9 +2013,9 @@ const openBatchTagDialog = () => {
|
||||
const handleBatchTag = async () => {
|
||||
if (!selectedRowKeys.value.length || !props.kbId) return
|
||||
try {
|
||||
const updates: Record<string, string | null> = {}
|
||||
const updates: Record<number, number | null> = {}
|
||||
selectedRowKeys.value.forEach(id => {
|
||||
updates[id] = batchTagValue.value || null
|
||||
updates[id] = batchTagValue.value ? Number(batchTagValue.value) : null
|
||||
})
|
||||
await updateFAQEntryTagBatch(props.kbId, { updates })
|
||||
MessagePlugin.success(t('knowledgeEditor.messages.updateSuccess'))
|
||||
@@ -1962,7 +2031,7 @@ const handleBatchTag = async () => {
|
||||
const handleBatchStatusChange = async (isEnabled: boolean) => {
|
||||
if (!selectedRowKeys.value.length || !props.kbId) return
|
||||
try {
|
||||
const by_id: Record<string, { is_enabled: boolean }> = {}
|
||||
const by_id: Record<number, { is_enabled: boolean }> = {}
|
||||
selectedRowKeys.value.forEach(id => {
|
||||
by_id[id] = { is_enabled: isEnabled }
|
||||
})
|
||||
@@ -1978,7 +2047,7 @@ const handleBatchStatusChange = async (isEnabled: boolean) => {
|
||||
const handleBatchRecommendedChange = async (isRecommended: boolean) => {
|
||||
if (!selectedRowKeys.value.length || !props.kbId) return
|
||||
try {
|
||||
const by_id: Record<string, { is_recommended: boolean }> = {}
|
||||
const by_id: Record<number, { is_recommended: boolean }> = {}
|
||||
selectedRowKeys.value.forEach(id => {
|
||||
by_id[id] = { is_recommended: isRecommended }
|
||||
})
|
||||
@@ -2103,7 +2172,7 @@ const parseCSVFile = async (file: File): Promise<FAQEntryPayload[]> => {
|
||||
answers: splitByDelimiter(record['机器人回答'] || record['answers']),
|
||||
similar_questions: splitByDelimiter(record['相似问题'] || record['similar_questions']),
|
||||
negative_questions: splitByDelimiter(record['反例问题'] || record['negative_questions']),
|
||||
tag_id: record['tag_id'] || '',
|
||||
tag_id: record['tag_id'] ? Number(record['tag_id']) : undefined,
|
||||
tag_name: record['分类'] || record['tag_name'] || '',
|
||||
is_enabled: isDisabled !== undefined ? !isDisabled : undefined, // 是否停用:FALSE表示启用,TRUE表示停用,所以取反
|
||||
}),
|
||||
@@ -2150,7 +2219,7 @@ const parseExcelFile = async (file: File): Promise<FAQEntryPayload[]> => {
|
||||
answers: splitByDelimiter(normalizedRow['机器人回答'] || normalizedRow['answers']),
|
||||
similar_questions: splitByDelimiter(normalizedRow['相似问题'] || normalizedRow['similar_questions']),
|
||||
negative_questions: splitByDelimiter(normalizedRow['反例问题'] || normalizedRow['negative_questions']),
|
||||
tag_id: normalizedRow['tag_id'] || '',
|
||||
tag_id: normalizedRow['tag_id'] ? Number(normalizedRow['tag_id']) : undefined,
|
||||
tag_name: normalizedRow['分类'] || normalizedRow['tag_name'] || '',
|
||||
is_enabled: isDisabled !== undefined ? !isDisabled : undefined, // 是否停用:FALSE表示启用,TRUE表示停用,所以取反
|
||||
})
|
||||
@@ -2193,7 +2262,7 @@ const normalizePayload = (payload: Partial<FAQEntryPayload>): FAQEntryPayload =>
|
||||
answers: payload.answers?.filter(Boolean) || [],
|
||||
similar_questions: payload.similar_questions?.filter(Boolean) || [],
|
||||
negative_questions: payload.negative_questions?.filter(Boolean) || [],
|
||||
tag_id: payload.tag_id || '',
|
||||
tag_id: payload.tag_id || undefined,
|
||||
tag_name: payload.tag_name || '',
|
||||
is_enabled: payload.is_enabled !== undefined ? payload.is_enabled : undefined,
|
||||
})
|
||||
@@ -2251,13 +2320,18 @@ const startPolling = (taskId: string) => {
|
||||
if (status === 'success' || status === 'failed') {
|
||||
stopPolling()
|
||||
if (status === 'success') {
|
||||
// 保存已完成的 taskId 用于后续加载结果
|
||||
if (importState.taskId) {
|
||||
saveLastCompletedTaskId(importState.taskId)
|
||||
}
|
||||
MessagePlugin.success(t('knowledgeEditor.faqImport.importSuccess'))
|
||||
// 清除筛选条件,确保用户能看到所有新导入的数据
|
||||
selectedTagId.value = UNTAGGED_FILTER
|
||||
selectedTagId.value = 0
|
||||
entrySearchKeyword.value = ''
|
||||
overallFAQTotal.value = 0 // Reset to trigger re-fetch
|
||||
await loadEntries()
|
||||
await loadTags()
|
||||
await loadImportResult() // 加载最新的导入结果统计
|
||||
// 任务完成后,3秒后自动关闭进度条
|
||||
setTimeout(() => {
|
||||
if (importState.taskStatus?.status === 'success') {
|
||||
@@ -2387,6 +2461,105 @@ const restoreImportTask = async () => {
|
||||
}
|
||||
}
|
||||
|
||||
// localStorage key for last completed task
|
||||
const getLastCompletedTaskKey = () => {
|
||||
return `faq_import_last_completed_${props.kbId}`
|
||||
}
|
||||
|
||||
const saveLastCompletedTaskId = (taskId: string) => {
|
||||
if (!props.kbId) return
|
||||
try {
|
||||
localStorage.setItem(getLastCompletedTaskKey(), taskId)
|
||||
} catch (error) {
|
||||
console.error('Failed to save last completed taskId:', error)
|
||||
}
|
||||
}
|
||||
|
||||
const getLastCompletedTaskId = (): string | null => {
|
||||
if (!props.kbId) return null
|
||||
try {
|
||||
return localStorage.getItem(getLastCompletedTaskKey())
|
||||
} catch (error) {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
// 加载持久化的导入结果统计
|
||||
const loadImportResult = async () => {
|
||||
if (!props.kbId) return
|
||||
|
||||
const lastTaskId = getLastCompletedTaskId()
|
||||
if (!lastTaskId) {
|
||||
importResult.value = null
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
const res: any = await getFAQImportProgress(lastTaskId)
|
||||
const data = res?.data
|
||||
if (data && data.status === 'completed') {
|
||||
// 检查后端返回的 display_status,如果是 close 则不显示
|
||||
if (data.display_status === 'close') {
|
||||
importResult.value = null
|
||||
return
|
||||
}
|
||||
// Map progress fields to importResult format
|
||||
importResult.value = {
|
||||
total_entries: data.total,
|
||||
success_count: data.success_count,
|
||||
failed_count: data.failed_count,
|
||||
skipped_count: data.skipped_count || 0,
|
||||
import_mode: data.import_mode || 'append',
|
||||
imported_at: data.imported_at,
|
||||
task_id: data.task_id,
|
||||
failed_entries_url: data.failed_entries_url,
|
||||
success_entries: data.success_entries,
|
||||
display_status: data.display_status || 'open',
|
||||
processing_time: data.processing_time || 0,
|
||||
}
|
||||
} else {
|
||||
importResult.value = null
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to load FAQ import result:', error)
|
||||
importResult.value = null
|
||||
}
|
||||
}
|
||||
|
||||
// 关闭导入结果统计卡片
|
||||
const closeImportResult = async () => {
|
||||
if (!props.kbId) return
|
||||
try {
|
||||
await updateFAQImportResultDisplayStatus(props.kbId, 'close')
|
||||
if (importResult.value) {
|
||||
importResult.value.display_status = 'close'
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to close import result:', error)
|
||||
}
|
||||
}
|
||||
|
||||
// 下载失败条目原因
|
||||
const downloadFailedEntries = () => {
|
||||
if (!importResult.value?.failed_entries_url) {
|
||||
MessagePlugin.warning('暂无失败记录可下载')
|
||||
return
|
||||
}
|
||||
// 直接打开下载链接
|
||||
window.open(importResult.value.failed_entries_url, '_blank')
|
||||
}
|
||||
|
||||
// 格式化导入时间
|
||||
const formatImportTime = (timeStr?: string) => {
|
||||
if (!timeStr) return ''
|
||||
try {
|
||||
const date = new Date(timeStr)
|
||||
return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')} ${String(date.getHours()).padStart(2, '0')}:${String(date.getMinutes()).padStart(2, '0')}`
|
||||
} catch (e) {
|
||||
return timeStr
|
||||
}
|
||||
}
|
||||
|
||||
const handleImport = async () => {
|
||||
if (!importState.file || !importState.preview.length) {
|
||||
MessagePlugin.warning(t('knowledgeEditor.faqImport.selectFile'))
|
||||
@@ -2610,7 +2783,7 @@ watch(
|
||||
async (newKbId) => {
|
||||
currentPage = 1
|
||||
hasMore.value = true
|
||||
selectedTagId.value = UNTAGGED_FILTER
|
||||
selectedTagId.value = 0
|
||||
overallFAQTotal.value = 0 // Reset to trigger re-fetch
|
||||
cancelCreateTag()
|
||||
cancelEditTag()
|
||||
@@ -2847,6 +3020,7 @@ onMounted(async () => {
|
||||
// 如果已有kbId,恢复导入任务状态
|
||||
if (props.kbId) {
|
||||
await restoreImportTask()
|
||||
await loadImportResult() // 加载导入结果
|
||||
}
|
||||
// 主动触发一次选中数量事件,确保左侧菜单能接收到初始状态
|
||||
nextTick(() => {
|
||||
@@ -3542,6 +3716,133 @@ watch(() => entries.value.map(e => ({
|
||||
}
|
||||
}
|
||||
|
||||
// 导入结果统计卡片样式
|
||||
.faq-import-result-card {
|
||||
margin-bottom: 16px;
|
||||
background: #ffffff;
|
||||
border: 1px solid #e7ebf0;
|
||||
border-radius: 8px;
|
||||
padding: 16px 20px;
|
||||
box-shadow: 0 1px 4px rgba(0, 0, 0, 0.04);
|
||||
|
||||
.import-result-content {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 12px;
|
||||
}
|
||||
|
||||
.import-result-header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
|
||||
.header-left {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
|
||||
.result-icon {
|
||||
color: #07C05F;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.result-title {
|
||||
font-family: "PingFang SC";
|
||||
font-weight: 600;
|
||||
font-size: 14px;
|
||||
color: #1d2129;
|
||||
}
|
||||
}
|
||||
|
||||
.header-right {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 12px;
|
||||
|
||||
.result-time {
|
||||
font-family: "PingFang SC";
|
||||
font-size: 13px;
|
||||
color: #86909c;
|
||||
}
|
||||
|
||||
.result-close-btn {
|
||||
padding: 4px;
|
||||
border-radius: 4px;
|
||||
color: #86909c;
|
||||
transition: all 0.2s ease;
|
||||
|
||||
&:hover {
|
||||
background: #f2f3f5;
|
||||
color: #4e5969;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.import-result-body {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
flex-wrap: wrap;
|
||||
gap: 12px;
|
||||
}
|
||||
|
||||
.import-result-stats {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
flex-wrap: wrap;
|
||||
gap: 24px;
|
||||
|
||||
.stat-item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 6px;
|
||||
font-family: "PingFang SC";
|
||||
font-size: 13px;
|
||||
|
||||
.stat-label {
|
||||
color: #86909c;
|
||||
}
|
||||
|
||||
.stat-value {
|
||||
font-weight: 600;
|
||||
color: #1d2129;
|
||||
}
|
||||
|
||||
&.success .stat-value {
|
||||
color: #07C05F;
|
||||
}
|
||||
|
||||
&.failed .stat-value {
|
||||
color: #E34D59;
|
||||
}
|
||||
|
||||
&.skipped .stat-value {
|
||||
color: #ED7B2F;
|
||||
}
|
||||
|
||||
.download-failed-btn {
|
||||
margin-left: 4px;
|
||||
padding: 0 8px;
|
||||
height: 24px;
|
||||
font-size: 12px;
|
||||
border-radius: 4px;
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 4px;
|
||||
|
||||
.t-icon {
|
||||
font-size: 12px;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.import-mode-tag {
|
||||
flex-shrink: 0;
|
||||
}
|
||||
}
|
||||
|
||||
.faq-header-title {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
|
||||
@@ -29,100 +29,19 @@
|
||||
<t-radio-group v-model="formData.transport_type">
|
||||
<t-radio value="sse">{{ t('mcpServiceDialog.transport.sse') }}</t-radio>
|
||||
<t-radio value="http-streamable">{{ t('mcpServiceDialog.transport.httpStreamable') }}</t-radio>
|
||||
<t-radio value="stdio">{{ t('mcpServiceDialog.transport.stdio') }}</t-radio>
|
||||
<!-- Stdio transport is disabled for security reasons -->
|
||||
</t-radio-group>
|
||||
</t-form-item>
|
||||
|
||||
<!-- URL for SSE/HTTP Streamable -->
|
||||
<t-form-item
|
||||
v-if="formData.transport_type !== 'stdio'"
|
||||
:label="t('mcpServiceDialog.serviceUrl')"
|
||||
name="url"
|
||||
>
|
||||
<t-input v-model="formData.url" :placeholder="t('mcpServiceDialog.serviceUrlPlaceholder')" />
|
||||
</t-form-item>
|
||||
|
||||
<!-- Stdio Config -->
|
||||
<template v-if="formData.transport_type === 'stdio'">
|
||||
<t-form-item :label="t('mcpServiceDialog.command')" name="stdio_config.command">
|
||||
<t-radio-group v-model="formData.stdio_config.command">
|
||||
<t-radio value="uvx">uvx</t-radio>
|
||||
<t-radio value="npx">npx</t-radio>
|
||||
</t-radio-group>
|
||||
</t-form-item>
|
||||
|
||||
<t-form-item :label="t('mcpServiceDialog.args')" name="stdio_config.args">
|
||||
<div class="args-input-container">
|
||||
<div
|
||||
v-for="(arg, index) in formData.stdio_config.args"
|
||||
:key="index"
|
||||
class="arg-item"
|
||||
>
|
||||
<t-input
|
||||
v-model="formData.stdio_config.args[index]"
|
||||
:placeholder="t('mcpServiceDialog.argPlaceholder', { index: index + 1 })"
|
||||
class="arg-input"
|
||||
/>
|
||||
<t-button
|
||||
variant="text"
|
||||
theme="danger"
|
||||
@click="removeArg(index)"
|
||||
:disabled="formData.stdio_config.args.length === 1"
|
||||
>
|
||||
<template #icon><t-icon name="delete" /></template>
|
||||
</t-button>
|
||||
</div>
|
||||
<t-button
|
||||
variant="outline"
|
||||
size="small"
|
||||
@click="addArg"
|
||||
class="add-arg-btn"
|
||||
>
|
||||
<template #icon><t-icon name="add" /></template>
|
||||
{{ t('mcpServiceDialog.addArg') }}
|
||||
</t-button>
|
||||
</div>
|
||||
</t-form-item>
|
||||
|
||||
<t-form-item :label="t('mcpServiceDialog.envVars')">
|
||||
<div class="env-vars-container">
|
||||
<div
|
||||
v-for="(value, key, index) in formData.env_vars"
|
||||
:key="index"
|
||||
class="env-var-item"
|
||||
>
|
||||
<t-input
|
||||
v-model="envVarKeys[index]"
|
||||
:placeholder="t('mcpServiceDialog.envKeyPlaceholder')"
|
||||
class="env-key-input"
|
||||
@blur="updateEnvVarKey(index, envVarKeys[index])"
|
||||
/>
|
||||
<t-input
|
||||
v-model="formData.env_vars[key]"
|
||||
:placeholder="t('mcpServiceDialog.envValuePlaceholder')"
|
||||
type="password"
|
||||
class="env-value-input"
|
||||
/>
|
||||
<t-button
|
||||
variant="text"
|
||||
theme="danger"
|
||||
@click="removeEnvVar(key)"
|
||||
>
|
||||
<template #icon><t-icon name="delete" /></template>
|
||||
</t-button>
|
||||
</div>
|
||||
<t-button
|
||||
variant="outline"
|
||||
size="small"
|
||||
@click="addEnvVar"
|
||||
class="add-env-var-btn"
|
||||
>
|
||||
<template #icon><t-icon name="add" /></template>
|
||||
{{ t('mcpServiceDialog.addEnvVar') }}
|
||||
</t-button>
|
||||
</div>
|
||||
</t-form-item>
|
||||
</template>
|
||||
<!-- Stdio Config removed for security reasons -->
|
||||
|
||||
<t-form-item :label="t('mcpServiceDialog.enableService')" name="enabled">
|
||||
<t-switch v-model="formData.enabled" />
|
||||
@@ -212,13 +131,8 @@ const formData = ref({
|
||||
name: '',
|
||||
description: '',
|
||||
enabled: true,
|
||||
transport_type: 'sse' as 'sse' | 'http-streamable' | 'stdio',
|
||||
transport_type: 'sse' as 'sse' | 'http-streamable',
|
||||
url: '',
|
||||
stdio_config: {
|
||||
command: 'uvx' as 'uvx' | 'npx',
|
||||
args: ['']
|
||||
},
|
||||
env_vars: {} as Record<string, string>,
|
||||
auth_config: {
|
||||
api_key: '',
|
||||
token: ''
|
||||
@@ -230,52 +144,22 @@ const formData = ref({
|
||||
}
|
||||
})
|
||||
|
||||
// Track env var keys separately for easier editing
|
||||
const envVarKeys = ref<string[]>([])
|
||||
|
||||
const rules: Record<string, FormRule[]> = {
|
||||
name: [{ required: true, message: t('mcpServiceDialog.rules.nameRequired') as string, type: 'error' }],
|
||||
transport_type: [{ required: true, message: t('mcpServiceDialog.rules.transportRequired') as string, type: 'error' }],
|
||||
url: [
|
||||
{
|
||||
validator: (val: string) => {
|
||||
if (formData.value.transport_type !== 'stdio') {
|
||||
if (!val || val.trim() === '') {
|
||||
return { result: false, message: t('mcpServiceDialog.rules.urlRequired') as string, type: 'error' }
|
||||
}
|
||||
// Basic URL validation
|
||||
try {
|
||||
new URL(val)
|
||||
return { result: true, message: '', type: 'success' }
|
||||
} catch {
|
||||
return { result: false, message: t('mcpServiceDialog.rules.urlInvalid') as string, type: 'error' }
|
||||
}
|
||||
if (!val || val.trim() === '') {
|
||||
return { result: false, message: t('mcpServiceDialog.rules.urlRequired') as string, type: 'error' }
|
||||
}
|
||||
return { result: true, message: '', type: 'success' }
|
||||
}
|
||||
}
|
||||
],
|
||||
'stdio_config.command': [
|
||||
{
|
||||
validator: (val: string) => {
|
||||
if (formData.value.transport_type === 'stdio') {
|
||||
if (!val || (val !== 'uvx' && val !== 'npx')) {
|
||||
return { result: false, message: t('mcpServiceDialog.rules.commandRequired') as string, type: 'error' }
|
||||
}
|
||||
// Basic URL validation
|
||||
try {
|
||||
new URL(val)
|
||||
return { result: true, message: '', type: 'success' }
|
||||
} catch {
|
||||
return { result: false, message: t('mcpServiceDialog.rules.urlInvalid') as string, type: 'error' }
|
||||
}
|
||||
return { result: true, message: '', type: 'success' }
|
||||
}
|
||||
}
|
||||
],
|
||||
'stdio_config.args': [
|
||||
{
|
||||
validator: (val: string[]) => {
|
||||
if (formData.value.transport_type === 'stdio') {
|
||||
if (!val || val.length === 0 || val.every(arg => !arg || arg.trim() === '')) {
|
||||
return { result: false, message: t('mcpServiceDialog.rules.argsRequired') as string, type: 'error' }
|
||||
}
|
||||
}
|
||||
return { result: true, message: '', type: 'success' }
|
||||
}
|
||||
}
|
||||
]
|
||||
@@ -294,11 +178,6 @@ const resetForm = () => {
|
||||
enabled: true,
|
||||
transport_type: 'sse',
|
||||
url: '',
|
||||
stdio_config: {
|
||||
command: 'uvx',
|
||||
args: ['']
|
||||
},
|
||||
env_vars: {},
|
||||
auth_config: {
|
||||
api_key: '',
|
||||
token: ''
|
||||
@@ -309,90 +188,22 @@ const resetForm = () => {
|
||||
retry_delay: 1
|
||||
}
|
||||
}
|
||||
envVarKeys.value = []
|
||||
formRef.value?.clearValidate()
|
||||
}
|
||||
|
||||
// Watch transport_type to reset related fields
|
||||
watch(
|
||||
() => formData.value.transport_type,
|
||||
(newType) => {
|
||||
if (newType === 'stdio') {
|
||||
formData.value.url = ''
|
||||
if (!formData.value.stdio_config || formData.value.stdio_config.args.length === 0) {
|
||||
formData.value.stdio_config = {
|
||||
command: 'uvx',
|
||||
args: ['']
|
||||
}
|
||||
}
|
||||
} else {
|
||||
formData.value.stdio_config = {
|
||||
command: 'uvx',
|
||||
args: ['']
|
||||
}
|
||||
formData.value.env_vars = {}
|
||||
envVarKeys.value = []
|
||||
}
|
||||
formRef.value?.clearValidate()
|
||||
}
|
||||
)
|
||||
|
||||
// Args management
|
||||
const addArg = () => {
|
||||
formData.value.stdio_config.args.push('')
|
||||
}
|
||||
|
||||
const removeArg = (index: number) => {
|
||||
if (formData.value.stdio_config.args.length > 1) {
|
||||
formData.value.stdio_config.args.splice(index, 1)
|
||||
}
|
||||
}
|
||||
|
||||
// Env vars management
|
||||
const addEnvVar = () => {
|
||||
const key = `VAR_${Date.now()}`
|
||||
formData.value.env_vars[key] = ''
|
||||
envVarKeys.value.push(key)
|
||||
}
|
||||
|
||||
const removeEnvVar = (key: string) => {
|
||||
delete formData.value.env_vars[key]
|
||||
const index = envVarKeys.value.indexOf(key)
|
||||
if (index > -1) {
|
||||
envVarKeys.value.splice(index, 1)
|
||||
}
|
||||
}
|
||||
|
||||
const updateEnvVarKey = (index: number, newKey: string) => {
|
||||
const oldKey = envVarKeys.value[index]
|
||||
if (oldKey && oldKey !== newKey && formData.value.env_vars[oldKey] !== undefined) {
|
||||
const value = formData.value.env_vars[oldKey]
|
||||
delete formData.value.env_vars[oldKey]
|
||||
if (newKey && newKey.trim() !== '') {
|
||||
formData.value.env_vars[newKey] = value
|
||||
envVarKeys.value[index] = newKey
|
||||
} else {
|
||||
envVarKeys.value.splice(index, 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Watch service prop to initialize form
|
||||
watch(
|
||||
() => props.service,
|
||||
(service) => {
|
||||
if (service) {
|
||||
// Note: stdio transport_type will fall back to 'sse' as stdio is disabled
|
||||
const transportType = service.transport_type === 'stdio' ? 'sse' : (service.transport_type || 'sse')
|
||||
formData.value = {
|
||||
name: service.name || '',
|
||||
description: service.description || '',
|
||||
enabled: service.enabled ?? true,
|
||||
transport_type: service.transport_type || 'sse',
|
||||
transport_type: transportType as 'sse' | 'http-streamable',
|
||||
url: service.url || '',
|
||||
stdio_config: service.stdio_config || {
|
||||
command: 'uvx',
|
||||
args: ['']
|
||||
},
|
||||
env_vars: service.env_vars || {},
|
||||
auth_config: {
|
||||
api_key: service.auth_config?.api_key || '',
|
||||
token: service.auth_config?.token || ''
|
||||
@@ -403,8 +214,6 @@ watch(
|
||||
retry_delay: service.advanced_config?.retry_delay || 1
|
||||
}
|
||||
}
|
||||
// Initialize env var keys
|
||||
envVarKeys.value = Object.keys(formData.value.env_vars)
|
||||
} else {
|
||||
resetForm()
|
||||
}
|
||||
@@ -428,32 +237,8 @@ const handleSubmit = async () => {
|
||||
api_key: formData.value.auth_config.api_key || undefined,
|
||||
token: formData.value.auth_config.token || undefined
|
||||
},
|
||||
advanced_config: formData.value.advanced_config
|
||||
}
|
||||
|
||||
// Add URL or stdio_config based on transport type
|
||||
if (formData.value.transport_type === 'stdio') {
|
||||
// Filter out empty args
|
||||
const args = formData.value.stdio_config.args.filter(arg => arg && arg.trim() !== '')
|
||||
data.stdio_config = {
|
||||
command: formData.value.stdio_config.command,
|
||||
args
|
||||
}
|
||||
// Build env vars using envVarKeys to get the correct key names
|
||||
// This fixes the issue where user-entered keys weren't being saved
|
||||
const envVars: Record<string, string> = {}
|
||||
const formEnvVarsEntries = Object.entries(formData.value.env_vars)
|
||||
for (let i = 0; i < formEnvVarsEntries.length; i++) {
|
||||
const [, value] = formEnvVarsEntries[i]
|
||||
// Use the key from envVarKeys (which reflects user input) instead of formData key
|
||||
const actualKey = envVarKeys.value[i]
|
||||
if (actualKey && actualKey.trim() !== '' && value && value.trim() !== '') {
|
||||
envVars[actualKey.trim()] = value.trim()
|
||||
}
|
||||
}
|
||||
data.env_vars = Object.keys(envVars).length > 0 ? envVars : undefined
|
||||
} else {
|
||||
data.url = formData.value.url || undefined
|
||||
advanced_config: formData.value.advanced_config,
|
||||
url: formData.value.url || undefined
|
||||
}
|
||||
|
||||
if (props.mode === 'add') {
|
||||
@@ -482,48 +267,6 @@ const handleClose = () => {
|
||||
</script>
|
||||
|
||||
<style scoped lang="less">
|
||||
.args-input-container {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 8px;
|
||||
|
||||
.arg-item {
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
align-items: center;
|
||||
|
||||
.arg-input {
|
||||
flex: 1;
|
||||
}
|
||||
}
|
||||
|
||||
.add-arg-btn {
|
||||
align-self: flex-start;
|
||||
}
|
||||
}
|
||||
|
||||
.env-vars-container {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 8px;
|
||||
|
||||
.env-var-item {
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
align-items: center;
|
||||
|
||||
.env-key-input {
|
||||
width: 150px;
|
||||
}
|
||||
|
||||
.env-value-input {
|
||||
flex: 1;
|
||||
}
|
||||
}
|
||||
|
||||
.add-env-var-btn {
|
||||
align-self: flex-start;
|
||||
}
|
||||
}
|
||||
/* Stdio-related styles removed as stdio transport is disabled for security reasons */
|
||||
</style>
|
||||
|
||||
|
||||
@@ -44,6 +44,18 @@ func (r *chunkRepository) GetChunkByID(ctx context.Context, tenantID uint64, id
|
||||
return &chunk, nil
|
||||
}
|
||||
|
||||
// GetChunkBySeqID retrieves a chunk by its seq_id and tenant ID
|
||||
func (r *chunkRepository) GetChunkBySeqID(ctx context.Context, tenantID uint64, seqID int64) (*types.Chunk, error) {
|
||||
var chunk types.Chunk
|
||||
if err := r.db.WithContext(ctx).Where("tenant_id = ? AND seq_id = ?", tenantID, seqID).First(&chunk).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, errors.New("chunk not found")
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &chunk, nil
|
||||
}
|
||||
|
||||
// ListChunksByID retrieves multiple chunks by their IDs
|
||||
func (r *chunkRepository) ListChunksByID(
|
||||
ctx context.Context, tenantID uint64, ids []string,
|
||||
@@ -57,6 +69,22 @@ func (r *chunkRepository) ListChunksByID(
|
||||
return chunks, nil
|
||||
}
|
||||
|
||||
// ListChunksBySeqID retrieves multiple chunks by their seq_ids
|
||||
func (r *chunkRepository) ListChunksBySeqID(
|
||||
ctx context.Context, tenantID uint64, seqIDs []int64,
|
||||
) ([]*types.Chunk, error) {
|
||||
if len(seqIDs) == 0 {
|
||||
return []*types.Chunk{}, nil
|
||||
}
|
||||
var chunks []*types.Chunk
|
||||
if err := r.db.WithContext(ctx).
|
||||
Where("tenant_id = ? AND seq_id IN ?", tenantID, seqIDs).
|
||||
Find(&chunks).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return chunks, nil
|
||||
}
|
||||
|
||||
// ListChunksByKnowledgeID lists all chunks for a knowledge ID
|
||||
func (r *chunkRepository) ListChunksByKnowledgeID(
|
||||
ctx context.Context, tenantID uint64, knowledgeID string,
|
||||
@@ -91,10 +119,7 @@ func (r *chunkRepository) ListPagedChunksByKnowledgeID(
|
||||
baseFilter := func(db *gorm.DB) *gorm.DB {
|
||||
db = db.Where("tenant_id = ? AND knowledge_id = ? AND chunk_type IN (?) AND status in (?)",
|
||||
tenantID, knowledgeID, chunkType, []int{int(types.ChunkStatusIndexed), int(types.ChunkStatusDefault)})
|
||||
if tagID == types.UntaggedTagID {
|
||||
// Special value to filter entries without a tag
|
||||
db = db.Where("tag_id = ''")
|
||||
} else if tagID != "" {
|
||||
if tagID != "" {
|
||||
db = db.Where("tag_id = ?", tagID)
|
||||
}
|
||||
if keyword != "" {
|
||||
@@ -640,9 +665,7 @@ func (r *chunkRepository) UpdateChunkFieldsByTagID(
|
||||
Select("id").
|
||||
Where("tenant_id = ? AND knowledge_base_id = ? AND chunk_type = ?",
|
||||
tenantID, kbID, types.ChunkTypeFAQ)
|
||||
if tagID == "" || tagID == types.UntaggedTagID {
|
||||
query = query.Where("(tag_id = '' OR tag_id IS NULL)")
|
||||
} else {
|
||||
if tagID != "" {
|
||||
query = query.Where("tag_id = ?", tagID)
|
||||
}
|
||||
|
||||
@@ -669,22 +692,16 @@ func (r *chunkRepository) UpdateChunkFieldsByTagID(
|
||||
updates["is_enabled"] = *isEnabled
|
||||
}
|
||||
|
||||
// Handle newTagID update (__untagged__ is stored as empty string)
|
||||
// Handle newTagID update
|
||||
if newTagID != nil {
|
||||
if *newTagID == types.UntaggedTagID || *newTagID == "" {
|
||||
updates["tag_id"] = ""
|
||||
} else {
|
||||
updates["tag_id"] = *newTagID
|
||||
}
|
||||
updates["tag_id"] = *newTagID
|
||||
}
|
||||
|
||||
query := r.db.WithContext(ctx).Model(&types.Chunk{}).
|
||||
Where("tenant_id = ? AND knowledge_base_id = ? AND chunk_type = ?",
|
||||
tenantID, kbID, types.ChunkTypeFAQ)
|
||||
|
||||
if tagID == "" || tagID == types.UntaggedTagID {
|
||||
query = query.Where("(tag_id = '' OR tag_id IS NULL)")
|
||||
} else {
|
||||
if tagID != "" {
|
||||
query = query.Where("tag_id = ?", tagID)
|
||||
}
|
||||
|
||||
|
||||
@@ -75,12 +75,7 @@ func (r *knowledgeRepository) ListPagedKnowledgeByKnowledgeBaseID(
|
||||
query := r.db.WithContext(ctx).Model(&types.Knowledge{}).
|
||||
Where("tenant_id = ? AND knowledge_base_id = ?", tenantID, kbID)
|
||||
if tagID != "" {
|
||||
if tagID == types.UntaggedTagID {
|
||||
// Special value to filter entries without a tag
|
||||
query = query.Where("tag_id = '' OR tag_id IS NULL")
|
||||
} else {
|
||||
query = query.Where("tag_id = ?", tagID)
|
||||
}
|
||||
query = query.Where("tag_id = ?", tagID)
|
||||
}
|
||||
if keyword != "" {
|
||||
query = query.Where("file_name LIKE ?", "%"+keyword+"%")
|
||||
@@ -104,12 +99,7 @@ func (r *knowledgeRepository) ListPagedKnowledgeByKnowledgeBaseID(
|
||||
dataQuery := r.db.WithContext(ctx).
|
||||
Where("tenant_id = ? AND knowledge_base_id = ?", tenantID, kbID)
|
||||
if tagID != "" {
|
||||
if tagID == types.UntaggedTagID {
|
||||
// Special value to filter entries without a tag
|
||||
dataQuery = dataQuery.Where("tag_id = '' OR tag_id IS NULL")
|
||||
} else {
|
||||
dataQuery = dataQuery.Where("tag_id = ?", tagID)
|
||||
}
|
||||
dataQuery = dataQuery.Where("tag_id = ?", tagID)
|
||||
}
|
||||
if keyword != "" {
|
||||
dataQuery = dataQuery.Where("file_name LIKE ?", "%"+keyword+"%")
|
||||
@@ -411,3 +401,16 @@ func (r *knowledgeRepository) SearchKnowledge(
|
||||
}
|
||||
return knowledges, hasMore, nil
|
||||
}
|
||||
|
||||
// ListIDsByTagID returns all knowledge IDs that have the specified tag ID
|
||||
func (r *knowledgeRepository) ListIDsByTagID(
|
||||
ctx context.Context,
|
||||
tenantID uint64,
|
||||
kbID, tagID string,
|
||||
) ([]string, error) {
|
||||
var ids []string
|
||||
err := r.db.WithContext(ctx).Model(&types.Knowledge{}).
|
||||
Where("tenant_id = ? AND knowledge_base_id = ? AND tag_id = ?", tenantID, kbID, tagID).
|
||||
Pluck("id", &ids).Error
|
||||
return ids, err
|
||||
}
|
||||
|
||||
@@ -54,6 +54,31 @@ func (r *knowledgeTagRepository) GetByIDs(ctx context.Context, tenantID uint64,
|
||||
return tags, nil
|
||||
}
|
||||
|
||||
// GetBySeqID retrieves a tag by its seq_id
|
||||
func (r *knowledgeTagRepository) GetBySeqID(ctx context.Context, tenantID uint64, seqID int64) (*types.KnowledgeTag, error) {
|
||||
var tag types.KnowledgeTag
|
||||
if err := r.db.WithContext(ctx).
|
||||
Where("tenant_id = ? AND seq_id = ?", tenantID, seqID).
|
||||
First(&tag).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &tag, nil
|
||||
}
|
||||
|
||||
// GetBySeqIDs retrieves multiple tags by their seq_ids in a single query
|
||||
func (r *knowledgeTagRepository) GetBySeqIDs(ctx context.Context, tenantID uint64, seqIDs []int64) ([]*types.KnowledgeTag, error) {
|
||||
if len(seqIDs) == 0 {
|
||||
return []*types.KnowledgeTag{}, nil
|
||||
}
|
||||
var tags []*types.KnowledgeTag
|
||||
if err := r.db.WithContext(ctx).
|
||||
Where("tenant_id = ? AND seq_id IN (?)", tenantID, seqIDs).
|
||||
Find(&tags).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return tags, nil
|
||||
}
|
||||
|
||||
// GetByName gets a knowledge tag by name
|
||||
func (r *knowledgeTagRepository) GetByName(ctx context.Context, tenantID uint64, kbID string, name string) (*types.KnowledgeTag, error) {
|
||||
var tag types.KnowledgeTag
|
||||
@@ -97,7 +122,7 @@ func (r *knowledgeTagRepository) ListByKB(
|
||||
|
||||
var tags []*types.KnowledgeTag
|
||||
if err := dataQuery.
|
||||
Order("sort_order ASC, created_at ASC").
|
||||
Order("sort_order ASC, created_at DESC").
|
||||
Offset(page.Offset()).
|
||||
Limit(page.Limit()).
|
||||
Find(&tags).Error; err != nil {
|
||||
@@ -136,35 +161,14 @@ func (r *knowledgeTagRepository) CountReferences(
|
||||
return
|
||||
}
|
||||
|
||||
// CountUntaggedReferences returns the number of knowledges and chunks without a tag
|
||||
func (r *knowledgeTagRepository) CountUntaggedReferences(
|
||||
ctx context.Context,
|
||||
tenantID uint64,
|
||||
kbID string,
|
||||
) (knowledgeCount int64, chunkCount int64, err error) {
|
||||
if err = r.db.WithContext(ctx).
|
||||
Model(&types.Knowledge{}).
|
||||
Where("tenant_id = ? AND knowledge_base_id = ? AND (tag_id = '' OR tag_id IS NULL)", tenantID, kbID).
|
||||
Count(&knowledgeCount).Error; err != nil {
|
||||
return
|
||||
}
|
||||
if err = r.db.WithContext(ctx).
|
||||
Model(&types.Chunk{}).
|
||||
Where("tenant_id = ? AND knowledge_base_id = ? AND (tag_id = '' OR tag_id IS NULL)", tenantID, kbID).
|
||||
Count(&chunkCount).Error; err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// DeleteUnusedTags deletes tags that are not referenced by any knowledge or chunk.
|
||||
// Returns the number of deleted tags.
|
||||
func (r *knowledgeTagRepository) DeleteUnusedTags(ctx context.Context, tenantID uint64, kbID string) (int64, error) {
|
||||
// Delete tags that have no references in both knowledges and chunks tables
|
||||
// Delete tags that have no references in both knowledges and chunks tables (excluding soft-deleted records)
|
||||
result := r.db.WithContext(ctx).
|
||||
Where("tenant_id = ? AND knowledge_base_id = ?", tenantID, kbID).
|
||||
Where("id NOT IN (SELECT DISTINCT tag_id FROM knowledges WHERE tenant_id = ? AND knowledge_base_id = ? AND tag_id IS NOT NULL AND tag_id != '')", tenantID, kbID).
|
||||
Where("id NOT IN (SELECT DISTINCT tag_id FROM chunks WHERE tenant_id = ? AND knowledge_base_id = ? AND tag_id IS NOT NULL AND tag_id != '')", tenantID, kbID).
|
||||
Where("id NOT IN (SELECT DISTINCT tag_id FROM knowledges WHERE tenant_id = ? AND knowledge_base_id = ? AND tag_id IS NOT NULL AND tag_id != '' AND deleted_at IS NULL)", tenantID, kbID).
|
||||
Where("id NOT IN (SELECT DISTINCT tag_id FROM chunks WHERE tenant_id = ? AND knowledge_base_id = ? AND tag_id IS NOT NULL AND tag_id != '' AND deleted_at IS NULL)", tenantID, kbID).
|
||||
Delete(&types.KnowledgeTag{})
|
||||
return result.RowsAffected, result.Error
|
||||
}
|
||||
|
||||
@@ -59,13 +59,15 @@ func (r *tenantRepository) SearchTenants(ctx context.Context, keyword string, te
|
||||
|
||||
query := r.db.WithContext(ctx).Model(&types.Tenant{})
|
||||
|
||||
// Filter by tenant ID if provided
|
||||
if tenantID > 0 {
|
||||
// Build search conditions
|
||||
if tenantID > 0 && keyword != "" {
|
||||
// When both tenantID and keyword are provided, use OR to match either
|
||||
query = query.Where("id = ? OR name LIKE ? OR description LIKE ?", tenantID, "%"+keyword+"%", "%"+keyword+"%")
|
||||
} else if tenantID > 0 {
|
||||
// Filter by tenant ID only
|
||||
query = query.Where("id = ?", tenantID)
|
||||
}
|
||||
|
||||
// Filter by keyword if provided (search in name and description)
|
||||
if keyword != "" {
|
||||
} else if keyword != "" {
|
||||
// Filter by keyword only (search in name and description)
|
||||
query = query.Where("name LIKE ? OR description LIKE ?", "%"+keyword+"%", "%"+keyword+"%")
|
||||
}
|
||||
|
||||
|
||||
@@ -375,7 +375,7 @@ func (s *agentService) getKnowledgeBaseInfos(ctx context.Context, kbIDs []string
|
||||
pageResult, err := s.knowledgeService.ListFAQEntries(ctx, kbID, &types.Pagination{
|
||||
Page: 1,
|
||||
PageSize: 10,
|
||||
}, "", "", "", "")
|
||||
}, 0, "", "", "")
|
||||
if err == nil && pageResult != nil {
|
||||
docCount = int(pageResult.Total)
|
||||
if entries, ok := pageResult.Data.([]*types.FAQEntry); ok {
|
||||
|
||||
@@ -12,7 +12,7 @@ import (
|
||||
"github.com/Tencent/WeKnora/internal/logger"
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
||||
"github.com/google/uuid"
|
||||
"github.com/Tencent/WeKnora/internal/utils"
|
||||
"golang.org/x/sync/errgroup"
|
||||
)
|
||||
|
||||
@@ -253,7 +253,7 @@ func (e *EvaluationService) Evaluation(ctx context.Context,
|
||||
|
||||
// Create evaluation task with unique ID
|
||||
logger.Info(ctx, "Creating evaluation task")
|
||||
taskID := uuid.New().String()
|
||||
taskID := utils.GenerateTaskID("evaluation", tenantID, datasetID)
|
||||
logger.Infof(ctx, "Generated task ID: %s", taskID)
|
||||
|
||||
// Prepare evaluation detail with all parameters
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -9,6 +10,7 @@ import (
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
||||
"github.com/google/uuid"
|
||||
@@ -20,11 +22,19 @@ type cosFileService struct {
|
||||
client *cos.Client
|
||||
bucketURL string
|
||||
cosPathPrefix string
|
||||
// 临时桶配置(用于存放自动过期的临时文件)
|
||||
tempClient *cos.Client
|
||||
tempBucketURL string
|
||||
}
|
||||
|
||||
// NewCosFileService creates a new COS file service instance
|
||||
func NewCosFileService(bucketName, region, secretId, secretKey, cosPathPrefix string) (interfaces.FileService, error) {
|
||||
bucketURL := fmt.Sprintf("https://%s.cos.%s.myqcloud.com/", bucketName, region)
|
||||
return NewCosFileServiceWithTempBucket(bucketName, region, secretId, secretKey, cosPathPrefix, "", "")
|
||||
}
|
||||
|
||||
// NewCosFileServiceWithTempBucket creates a new COS file service instance with optional temp bucket
|
||||
func NewCosFileServiceWithTempBucket(bucketName, region, secretId, secretKey, cosPathPrefix, tempBucketName, tempRegion string) (interfaces.FileService, error) {
|
||||
bucketURL := fmt.Sprintf("https://%s.cos.%s.tencentcos.cn/", bucketName, region)
|
||||
u, err := url.Parse(bucketURL)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse bucketURL: %w", err)
|
||||
@@ -36,11 +46,35 @@ func NewCosFileService(bucketName, region, secretId, secretKey, cosPathPrefix st
|
||||
SecretKey: secretKey,
|
||||
},
|
||||
})
|
||||
return &cosFileService{
|
||||
|
||||
svc := &cosFileService{
|
||||
client: client,
|
||||
bucketURL: bucketURL,
|
||||
cosPathPrefix: cosPathPrefix,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 如果配置了临时桶,初始化临时桶客户端
|
||||
if tempBucketName != "" {
|
||||
if tempRegion == "" {
|
||||
tempRegion = region // 默认使用相同的 region
|
||||
}
|
||||
tempBucketURL := fmt.Sprintf("https://%s.cos.%s.tencentcos.cn/", tempBucketName, tempRegion)
|
||||
tempU, err := url.Parse(tempBucketURL)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse temp bucketURL: %w", err)
|
||||
}
|
||||
tempB := &cos.BaseURL{BucketURL: tempU}
|
||||
tempClient := cos.NewClient(tempB, &http.Client{
|
||||
Transport: &cos.AuthorizationTransport{
|
||||
SecretID: secretId,
|
||||
SecretKey: secretKey,
|
||||
},
|
||||
})
|
||||
svc.tempClient = tempClient
|
||||
svc.tempBucketURL = tempBucketURL
|
||||
}
|
||||
|
||||
return svc, nil
|
||||
}
|
||||
|
||||
// SaveFile saves a file to COS storage
|
||||
@@ -81,3 +115,53 @@ func (s *cosFileService) DeleteFile(ctx context.Context, filePath string) error
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SaveBytes saves bytes data to COS
|
||||
// If temp is true and temp bucket is configured, saves to temp bucket (with lifecycle auto-expiration)
|
||||
// Otherwise saves to main bucket
|
||||
func (s *cosFileService) SaveBytes(ctx context.Context, data []byte, tenantID uint64, fileName string, temp bool) (string, error) {
|
||||
ext := filepath.Ext(fileName)
|
||||
reader := bytes.NewReader(data)
|
||||
|
||||
// 如果请求写入临时桶且临时桶已配置
|
||||
if temp && s.tempClient != nil {
|
||||
objectName := fmt.Sprintf("exports/%d/%s%s", tenantID, uuid.New().String(), ext)
|
||||
_, err := s.tempClient.Object.Put(ctx, objectName, reader, nil)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to upload bytes to COS temp bucket: %w", err)
|
||||
}
|
||||
return fmt.Sprintf("%s%s", s.tempBucketURL, objectName), nil
|
||||
}
|
||||
|
||||
// 写入主桶
|
||||
objectName := fmt.Sprintf("%s/%d/exports/%s%s", s.cosPathPrefix, tenantID, uuid.New().String(), ext)
|
||||
_, err := s.client.Object.Put(ctx, objectName, reader, nil)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to upload bytes to COS: %w", err)
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%s%s", s.bucketURL, objectName), nil
|
||||
}
|
||||
|
||||
// GetFileURL returns a presigned download URL for the file
|
||||
func (s *cosFileService) GetFileURL(ctx context.Context, filePath string) (string, error) {
|
||||
// 判断文件属于哪个桶
|
||||
if s.tempClient != nil && strings.HasPrefix(filePath, s.tempBucketURL) {
|
||||
objectName := strings.TrimPrefix(filePath, s.tempBucketURL)
|
||||
// Generate presigned URL (valid for 24 hours)
|
||||
presignedURL, err := s.tempClient.Object.GetPresignedURL(ctx, http.MethodGet, objectName, s.tempClient.GetCredential().SecretID, s.tempClient.GetCredential().SecretKey, 24*time.Hour, nil)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to generate presigned URL for temp bucket: %w", err)
|
||||
}
|
||||
return presignedURL.String(), nil
|
||||
}
|
||||
|
||||
objectName := strings.TrimPrefix(filePath, s.bucketURL)
|
||||
// Generate presigned URL (valid for 24 hours)
|
||||
presignedURL, err := s.client.Object.GetPresignedURL(ctx, http.MethodGet, objectName, s.client.GetCredential().SecretID, s.client.GetCredential().SecretKey, 24*time.Hour, nil)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to generate presigned URL: %w", err)
|
||||
}
|
||||
|
||||
return presignedURL.String(), nil
|
||||
}
|
||||
|
||||
@@ -36,3 +36,13 @@ func (s *DummyFileService) GetFile(ctx context.Context, filePath string) (io.Rea
|
||||
func (s *DummyFileService) DeleteFile(ctx context.Context, filePath string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// SaveBytes pretends to save bytes but just returns a random UUID
|
||||
func (s *DummyFileService) SaveBytes(ctx context.Context, data []byte, tenantID uint64, fileName string, temp bool) (string, error) {
|
||||
return uuid.New().String(), nil
|
||||
}
|
||||
|
||||
// GetFileURL returns the file path as URL (dummy implementation)
|
||||
func (s *DummyFileService) GetFileURL(ctx context.Context, filePath string) (string, error) {
|
||||
return filePath, nil
|
||||
}
|
||||
|
||||
@@ -109,3 +109,38 @@ func (s *localFileService) DeleteFile(ctx context.Context, filePath string) erro
|
||||
logger.Info(ctx, "File deleted successfully")
|
||||
return nil
|
||||
}
|
||||
|
||||
// SaveBytes saves bytes data to a file and returns the file path
|
||||
// temp parameter is ignored for local storage (no auto-expiration support)
|
||||
func (s *localFileService) SaveBytes(ctx context.Context, data []byte, tenantID uint64, fileName string, temp bool) (string, error) {
|
||||
logger.Infof(ctx, "Saving bytes data: fileName=%s, size=%d, tenantID=%d, temp=%v", fileName, len(data), tenantID, temp)
|
||||
|
||||
// Create storage directory with tenant ID
|
||||
dir := filepath.Join(s.baseDir, fmt.Sprintf("%d", tenantID), "exports")
|
||||
if err := os.MkdirAll(dir, 0o755); err != nil {
|
||||
logger.Errorf(ctx, "Failed to create directory: %v", err)
|
||||
return "", fmt.Errorf("failed to create directory: %w", err)
|
||||
}
|
||||
|
||||
// Generate unique filename using timestamp
|
||||
ext := filepath.Ext(fileName)
|
||||
baseName := fileName[:len(fileName)-len(ext)]
|
||||
uniqueFileName := fmt.Sprintf("%s_%d%s", baseName, time.Now().UnixNano(), ext)
|
||||
filePath := filepath.Join(dir, uniqueFileName)
|
||||
|
||||
// Write data to file
|
||||
if err := os.WriteFile(filePath, data, 0o644); err != nil {
|
||||
logger.Errorf(ctx, "Failed to write file: %v", err)
|
||||
return "", fmt.Errorf("failed to write file: %w", err)
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "Bytes data saved successfully: %s", filePath)
|
||||
return filePath, nil
|
||||
}
|
||||
|
||||
// GetFileURL returns a download URL for the file
|
||||
// For local storage, returns the file path itself (no URL support)
|
||||
func (s *localFileService) GetFileURL(ctx context.Context, filePath string) (string, error) {
|
||||
// Local storage doesn't support URLs, return the path
|
||||
return filePath, nil
|
||||
}
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
||||
"github.com/google/uuid"
|
||||
@@ -125,3 +127,43 @@ func (s *minioFileService) DeleteFile(ctx context.Context, filePath string) erro
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SaveBytes saves bytes data to MinIO and returns the file path
|
||||
// temp parameter is ignored for MinIO (no auto-expiration support in this implementation)
|
||||
func (s *minioFileService) SaveBytes(ctx context.Context, data []byte, tenantID uint64, fileName string, temp bool) (string, error) {
|
||||
ext := filepath.Ext(fileName)
|
||||
objectName := fmt.Sprintf("%d/exports/%s%s", tenantID, uuid.New().String(), ext)
|
||||
|
||||
// Upload bytes to MinIO
|
||||
reader := bytes.NewReader(data)
|
||||
_, err := s.client.PutObject(ctx, s.bucketName, objectName, reader, int64(len(data)), minio.PutObjectOptions{
|
||||
ContentType: "text/csv; charset=utf-8",
|
||||
})
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to upload bytes to MinIO: %w", err)
|
||||
}
|
||||
|
||||
return fmt.Sprintf("minio://%s/%s", s.bucketName, objectName), nil
|
||||
}
|
||||
|
||||
// GetFileURL returns a presigned download URL for the file
|
||||
func (s *minioFileService) GetFileURL(ctx context.Context, filePath string) (string, error) {
|
||||
// Parse MinIO path
|
||||
if len(filePath) < 9 || filePath[:8] != "minio://" {
|
||||
return "", fmt.Errorf("invalid MinIO file path: %s", filePath)
|
||||
}
|
||||
|
||||
// Extract object name
|
||||
objectName := filePath[9+len(s.bucketName):]
|
||||
if objectName[0] == '/' {
|
||||
objectName = objectName[1:]
|
||||
}
|
||||
|
||||
// Generate presigned URL (valid for 24 hours)
|
||||
presignedURL, err := s.client.PresignedGetObject(ctx, s.bucketName, objectName, 24*time.Hour, nil)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to generate presigned URL: %w", err)
|
||||
}
|
||||
|
||||
return presignedURL.String(), nil
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -31,20 +31,9 @@ func NewMCPServiceService(
|
||||
|
||||
// CreateMCPService creates a new MCP service
|
||||
func (s *mcpServiceService) CreateMCPService(ctx context.Context, service *types.MCPService) error {
|
||||
// Security validation for stdio transport type
|
||||
// Stdio transport is disabled for security reasons
|
||||
if service.TransportType == types.MCPTransportStdio {
|
||||
if service.StdioConfig == nil {
|
||||
return fmt.Errorf("stdio_config is required for stdio transport")
|
||||
}
|
||||
// Validate stdio configuration to prevent command injection (CWE-78)
|
||||
if err := secutils.ValidateStdioConfig(
|
||||
service.StdioConfig.Command,
|
||||
service.StdioConfig.Args,
|
||||
service.EnvVars,
|
||||
); err != nil {
|
||||
logger.GetLogger(ctx).Warnf("MCP service creation blocked due to security validation: %v", err)
|
||||
return fmt.Errorf("security validation failed: %w", err)
|
||||
}
|
||||
return fmt.Errorf("stdio transport is disabled for security reasons; please use SSE or HTTP Streamable transport instead")
|
||||
}
|
||||
|
||||
// Set default advanced config if not provided
|
||||
@@ -129,31 +118,15 @@ func (s *mcpServiceService) UpdateMCPService(ctx context.Context, service *types
|
||||
return fmt.Errorf("MCP service not found")
|
||||
}
|
||||
|
||||
// Security validation for stdio transport type when updating stdio config
|
||||
// Determine the final transport type and stdio config after merge
|
||||
// Determine the final transport type after merge
|
||||
finalTransportType := existing.TransportType
|
||||
if service.TransportType != "" {
|
||||
finalTransportType = service.TransportType
|
||||
}
|
||||
finalStdioConfig := existing.StdioConfig
|
||||
if service.StdioConfig != nil {
|
||||
finalStdioConfig = service.StdioConfig
|
||||
}
|
||||
finalEnvVars := existing.EnvVars
|
||||
if service.EnvVars != nil {
|
||||
finalEnvVars = service.EnvVars
|
||||
}
|
||||
|
||||
// Validate if the final configuration uses stdio transport
|
||||
if finalTransportType == types.MCPTransportStdio && finalStdioConfig != nil {
|
||||
if err := secutils.ValidateStdioConfig(
|
||||
finalStdioConfig.Command,
|
||||
finalStdioConfig.Args,
|
||||
finalEnvVars,
|
||||
); err != nil {
|
||||
logger.GetLogger(ctx).Warnf("MCP service update blocked due to security validation: %v", err)
|
||||
return fmt.Errorf("security validation failed: %w", err)
|
||||
}
|
||||
// Stdio transport is disabled for security reasons
|
||||
if finalTransportType == types.MCPTransportStdio {
|
||||
return fmt.Errorf("stdio transport is disabled for security reasons; please use SSE or HTTP Streamable transport instead")
|
||||
}
|
||||
|
||||
// Store old enabled state BEFORE any updates
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"github.com/Tencent/WeKnora/internal/models/utils"
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
||||
"golang.org/x/sync/errgroup"
|
||||
)
|
||||
|
||||
// KeywordsVectorHybridRetrieveEngineService implements a hybrid retrieval engine
|
||||
@@ -58,7 +59,7 @@ func (v *KeywordsVectorHybridRetrieveEngineService) Index(ctx context.Context,
|
||||
}
|
||||
|
||||
// BatchIndex creates embeddings for multiple content items and saves them to the repository
|
||||
// in batches for efficiency
|
||||
// in batches for efficiency. Uses concurrent batch saving to improve performance.
|
||||
func (v *KeywordsVectorHybridRetrieveEngineService) BatchIndex(ctx context.Context,
|
||||
embedder embedding.Embedder, indexInfoList []*types.IndexInfo, retrieverTypes []types.RetrieverType,
|
||||
) error {
|
||||
@@ -85,30 +86,123 @@ func (v *KeywordsVectorHybridRetrieveEngineService) BatchIndex(ctx context.Conte
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
batchSize := 40
|
||||
for i, indexChunk := range utils.ChunkSlice(indexInfoList, batchSize) {
|
||||
chunks := utils.ChunkSlice(indexInfoList, batchSize)
|
||||
|
||||
// Use concurrent batch saving for better performance
|
||||
// Limit concurrency to avoid overwhelming the backend
|
||||
const maxConcurrency = 5
|
||||
if len(chunks) <= maxConcurrency {
|
||||
// For small number of batches, use simple concurrency
|
||||
return v.concurrentBatchSave(ctx, chunks, embeddings, batchSize)
|
||||
}
|
||||
|
||||
// For large number of batches, use bounded concurrency
|
||||
return v.boundedConcurrentBatchSave(ctx, chunks, embeddings, batchSize, maxConcurrency)
|
||||
}
|
||||
|
||||
// For non-vector retrieval, use concurrent batch saving as well
|
||||
chunks := utils.ChunkSlice(indexInfoList, 10)
|
||||
const maxConcurrency = 5
|
||||
if len(chunks) <= maxConcurrency {
|
||||
return v.concurrentBatchSaveNoEmbedding(ctx, chunks)
|
||||
}
|
||||
return v.boundedConcurrentBatchSaveNoEmbedding(ctx, chunks, maxConcurrency)
|
||||
}
|
||||
|
||||
// concurrentBatchSave saves all batches concurrently without concurrency limit
|
||||
func (v *KeywordsVectorHybridRetrieveEngineService) concurrentBatchSave(
|
||||
ctx context.Context,
|
||||
chunks [][]*types.IndexInfo,
|
||||
embeddings [][]float32,
|
||||
batchSize int,
|
||||
) error {
|
||||
g, ctx := errgroup.WithContext(ctx)
|
||||
for i, indexChunk := range chunks {
|
||||
g.Go(func() error {
|
||||
params := make(map[string]any)
|
||||
embeddingMap := make(map[string][]float32)
|
||||
for j, indexInfo := range indexChunk {
|
||||
embeddingMap[indexInfo.SourceID] = embeddings[i*batchSize+j]
|
||||
}
|
||||
params["embedding"] = embeddingMap
|
||||
err = v.indexRepository.BatchSave(ctx, indexChunk, params)
|
||||
if err != nil {
|
||||
return err
|
||||
return v.indexRepository.BatchSave(ctx, indexChunk, params)
|
||||
})
|
||||
}
|
||||
return g.Wait()
|
||||
}
|
||||
|
||||
// boundedConcurrentBatchSave saves batches with bounded concurrency using semaphore pattern
|
||||
func (v *KeywordsVectorHybridRetrieveEngineService) boundedConcurrentBatchSave(
|
||||
ctx context.Context,
|
||||
chunks [][]*types.IndexInfo,
|
||||
embeddings [][]float32,
|
||||
batchSize int,
|
||||
maxConcurrency int,
|
||||
) error {
|
||||
g, ctx := errgroup.WithContext(ctx)
|
||||
sem := make(chan struct{}, maxConcurrency)
|
||||
|
||||
for i, indexChunk := range chunks {
|
||||
g.Go(func() error {
|
||||
select {
|
||||
case sem <- struct{}{}:
|
||||
defer func() { <-sem }()
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
||||
params := make(map[string]any)
|
||||
embeddingMap := make(map[string][]float32)
|
||||
for j, indexInfo := range indexChunk {
|
||||
embeddingMap[indexInfo.SourceID] = embeddings[i*batchSize+j]
|
||||
}
|
||||
params["embedding"] = embeddingMap
|
||||
return v.indexRepository.BatchSave(ctx, indexChunk, params)
|
||||
})
|
||||
}
|
||||
var err error
|
||||
for _, indexChunk := range utils.ChunkSlice(indexInfoList, 10) {
|
||||
params := make(map[string]any)
|
||||
err = v.indexRepository.BatchSave(ctx, indexChunk, params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return g.Wait()
|
||||
}
|
||||
|
||||
// concurrentBatchSaveNoEmbedding saves all batches concurrently without embeddings
|
||||
func (v *KeywordsVectorHybridRetrieveEngineService) concurrentBatchSaveNoEmbedding(
|
||||
ctx context.Context,
|
||||
chunks [][]*types.IndexInfo,
|
||||
) error {
|
||||
g, ctx := errgroup.WithContext(ctx)
|
||||
for _, indexChunk := range chunks {
|
||||
g.Go(func() error {
|
||||
params := make(map[string]any)
|
||||
return v.indexRepository.BatchSave(ctx, indexChunk, params)
|
||||
})
|
||||
}
|
||||
return nil
|
||||
return g.Wait()
|
||||
}
|
||||
|
||||
// boundedConcurrentBatchSaveNoEmbedding saves batches with bounded concurrency without embeddings
|
||||
func (v *KeywordsVectorHybridRetrieveEngineService) boundedConcurrentBatchSaveNoEmbedding(
|
||||
ctx context.Context,
|
||||
chunks [][]*types.IndexInfo,
|
||||
maxConcurrency int,
|
||||
) error {
|
||||
g, ctx := errgroup.WithContext(ctx)
|
||||
sem := make(chan struct{}, maxConcurrency)
|
||||
|
||||
for _, indexChunk := range chunks {
|
||||
g.Go(func() error {
|
||||
select {
|
||||
case sem <- struct{}{}:
|
||||
defer func() { <-sem }()
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
}
|
||||
|
||||
params := make(map[string]any)
|
||||
return v.indexRepository.BatchSave(ctx, indexChunk, params)
|
||||
})
|
||||
}
|
||||
return g.Wait()
|
||||
}
|
||||
|
||||
// DeleteByChunkIDList deletes vectors by their chunk IDs
|
||||
|
||||
@@ -21,6 +21,7 @@ import (
|
||||
type knowledgeTagService struct {
|
||||
kbService interfaces.KnowledgeBaseService
|
||||
repo interfaces.KnowledgeTagRepository
|
||||
knowledgeRepo interfaces.KnowledgeRepository
|
||||
chunkRepo interfaces.ChunkRepository
|
||||
retrieveEngine interfaces.RetrieveEngineRegistry
|
||||
modelService interfaces.ModelService
|
||||
@@ -31,6 +32,7 @@ type knowledgeTagService struct {
|
||||
func NewKnowledgeTagService(
|
||||
kbService interfaces.KnowledgeBaseService,
|
||||
repo interfaces.KnowledgeTagRepository,
|
||||
knowledgeRepo interfaces.KnowledgeRepository,
|
||||
chunkRepo interfaces.ChunkRepository,
|
||||
retrieveEngine interfaces.RetrieveEngineRegistry,
|
||||
modelService interfaces.ModelService,
|
||||
@@ -39,6 +41,7 @@ func NewKnowledgeTagService(
|
||||
return &knowledgeTagService{
|
||||
kbService: kbService,
|
||||
repo: repo,
|
||||
knowledgeRepo: knowledgeRepo,
|
||||
chunkRepo: chunkRepo,
|
||||
retrieveEngine: retrieveEngine,
|
||||
modelService: modelService,
|
||||
@@ -72,28 +75,7 @@ func (s *knowledgeTagService) ListTags(
|
||||
return nil, err
|
||||
}
|
||||
|
||||
results := make([]*types.KnowledgeTagWithStats, 0, len(tags)+1)
|
||||
|
||||
// Add untagged pseudo-tag at the beginning (only on first page and when no keyword filter)
|
||||
if page.Page <= 1 && keyword == "" {
|
||||
untaggedKCount, untaggedCCount, err := s.repo.CountUntaggedReferences(ctx, tenantID, kbID)
|
||||
if err != nil {
|
||||
logger.ErrorWithFields(ctx, err, map[string]interface{}{
|
||||
"kb_id": kbID,
|
||||
})
|
||||
return nil, err
|
||||
}
|
||||
results = append(results, &types.KnowledgeTagWithStats{
|
||||
KnowledgeTag: types.KnowledgeTag{
|
||||
ID: types.UntaggedTagID,
|
||||
TenantID: tenantID,
|
||||
KnowledgeBaseID: kbID,
|
||||
Name: "未分类",
|
||||
},
|
||||
KnowledgeCount: untaggedKCount,
|
||||
ChunkCount: untaggedCCount,
|
||||
})
|
||||
}
|
||||
results := make([]*types.KnowledgeTagWithStats, 0, len(tags))
|
||||
|
||||
for _, tag := range tags {
|
||||
if tag == nil {
|
||||
@@ -114,11 +96,6 @@ func (s *knowledgeTagService) ListTags(
|
||||
})
|
||||
}
|
||||
|
||||
// Adjust total to include the untagged pseudo-tag
|
||||
if keyword == "" {
|
||||
total++
|
||||
}
|
||||
|
||||
return types.NewPageResult(total, page, results), nil
|
||||
}
|
||||
|
||||
@@ -149,6 +126,10 @@ func (s *knowledgeTagService) CreateTag(
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
// "未分类" tag should have the lowest sort order to appear first
|
||||
if name == types.UntaggedTagName {
|
||||
sortOrder = -1
|
||||
}
|
||||
tag := &types.KnowledgeTag{
|
||||
ID: uuid.New().String(),
|
||||
TenantID: kb.TenantID,
|
||||
@@ -203,6 +184,7 @@ func (s *knowledgeTagService) UpdateTag(
|
||||
}
|
||||
|
||||
// DeleteTag deletes a tag. When force=true, also deletes all chunks under this tag.
|
||||
// For document-type knowledge bases, also deletes all knowledge files under this tag.
|
||||
// When contentOnly=true, only deletes the content under the tag but keeps the tag itself.
|
||||
func (s *knowledgeTagService) DeleteTag(ctx context.Context, id string, force bool, contentOnly bool, excludeIDs []string) error {
|
||||
if id == "" {
|
||||
@@ -246,9 +228,49 @@ func (s *knowledgeTagService) DeleteTag(ctx context.Context, id string, force bo
|
||||
return nil
|
||||
}
|
||||
|
||||
// Helper function to enqueue knowledge list delete task for document-type knowledge bases
|
||||
enqueueKnowledgeDeleteTask := func() error {
|
||||
if kb.Type != types.KnowledgeBaseTypeDocument {
|
||||
return nil
|
||||
}
|
||||
// Get all knowledge IDs under this tag
|
||||
knowledgeIDs, err := s.knowledgeRepo.ListIDsByTagID(ctx, tenantID, kb.ID, tag.ID)
|
||||
if err != nil {
|
||||
logger.Errorf(ctx, "Failed to list knowledge IDs by tag ID %s: %v", tag.ID, err)
|
||||
return werrors.NewInternalServerError("获取标签下的文档失败")
|
||||
}
|
||||
if len(knowledgeIDs) == 0 {
|
||||
return nil
|
||||
}
|
||||
// Enqueue async task to delete knowledge files
|
||||
payload := types.KnowledgeListDeletePayload{
|
||||
TenantID: tenantID,
|
||||
KnowledgeIDs: knowledgeIDs,
|
||||
}
|
||||
payloadBytes, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
logger.Errorf(ctx, "Failed to marshal knowledge list delete payload: %v", err)
|
||||
return werrors.NewInternalServerError("删除标签下的文档失败")
|
||||
}
|
||||
task := asynq.NewTask(types.TypeKnowledgeListDelete, payloadBytes, asynq.Queue("low"), asynq.MaxRetry(3))
|
||||
info, err := s.task.Enqueue(task)
|
||||
if err != nil {
|
||||
logger.Errorf(ctx, "Failed to enqueue knowledge list delete task: %v", err)
|
||||
return werrors.NewInternalServerError("删除标签下的文档失败")
|
||||
}
|
||||
logger.Infof(ctx, "Enqueued knowledge list delete task %s for %d knowledge files under tag %s", info.ID, len(knowledgeIDs), tag.ID)
|
||||
return nil
|
||||
}
|
||||
|
||||
// contentOnly mode: only delete content, keep the tag
|
||||
if contentOnly {
|
||||
if cCount > 0 {
|
||||
// For document-type KB, delete knowledge files first (which will also delete chunks)
|
||||
if kb.Type == types.KnowledgeBaseTypeDocument && kCount > 0 {
|
||||
if err := enqueueKnowledgeDeleteTask(); err != nil {
|
||||
return err
|
||||
}
|
||||
} else if cCount > 0 {
|
||||
// For FAQ-type KB, only delete chunks
|
||||
if err := deleteChunksAndEnqueueIndexDelete(); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -259,12 +281,22 @@ func (s *knowledgeTagService) DeleteTag(ctx context.Context, id string, force bo
|
||||
if !force && (kCount > 0 || cCount > 0) {
|
||||
return werrors.NewBadRequestError("标签仍有知识或FAQ条目引用,无法删除")
|
||||
}
|
||||
// When force=true, delete all chunks under this tag first
|
||||
if force && cCount > 0 {
|
||||
if err := deleteChunksAndEnqueueIndexDelete(); err != nil {
|
||||
return err
|
||||
|
||||
// When force=true, delete all content under this tag first
|
||||
if force {
|
||||
// For document-type KB, delete knowledge files first (which will also delete chunks)
|
||||
if kb.Type == types.KnowledgeBaseTypeDocument && kCount > 0 {
|
||||
if err := enqueueKnowledgeDeleteTask(); err != nil {
|
||||
return err
|
||||
}
|
||||
} else if cCount > 0 {
|
||||
// For FAQ-type KB, only delete chunks
|
||||
if err := deleteChunksAndEnqueueIndexDelete(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If there are excludeIDs, we cannot delete the tag itself as it still has content
|
||||
if len(excludeIDs) > 0 {
|
||||
return nil
|
||||
|
||||
@@ -391,12 +391,14 @@ func initFileService(cfg *config.Config) (interfaces.FileService, error) {
|
||||
os.Getenv("COS_PATH_PREFIX") == "" {
|
||||
return nil, fmt.Errorf("missing COS configuration")
|
||||
}
|
||||
return file.NewCosFileService(
|
||||
return file.NewCosFileServiceWithTempBucket(
|
||||
os.Getenv("COS_BUCKET_NAME"),
|
||||
os.Getenv("COS_REGION"),
|
||||
os.Getenv("COS_SECRET_ID"),
|
||||
os.Getenv("COS_SECRET_KEY"),
|
||||
os.Getenv("COS_PATH_PREFIX"),
|
||||
os.Getenv("COS_TEMP_BUCKET_NAME"), // 可选:临时桶名称(桶需配置生命周期规则自动过期)
|
||||
os.Getenv("COS_TEMP_REGION"), // 可选:临时桶 region,默认与主桶相同
|
||||
)
|
||||
case "local":
|
||||
return file.NewLocalFileService(os.Getenv("LOCAL_STORAGE_BASE_DIR")), nil
|
||||
|
||||
+134
-15
@@ -2,6 +2,7 @@ package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
@@ -31,7 +32,7 @@ func NewFAQHandler(knowledgeService interfaces.KnowledgeService) *FAQHandler {
|
||||
// @Param id path string true "知识库ID"
|
||||
// @Param page query int false "页码"
|
||||
// @Param page_size query int false "每页数量"
|
||||
// @Param tag_id query string false "标签ID筛选"
|
||||
// @Param tag_id query int false "标签ID筛选(seq_id)"
|
||||
// @Param keyword query string false "关键词搜索"
|
||||
// @Param search_field query string false "搜索字段: standard_question(标准问题), similar_questions(相似问法), answers(答案), 默认搜索全部"
|
||||
// @Param sort_order query string false "排序方式: asc(按更新时间正序), 默认按更新时间倒序"
|
||||
@@ -49,12 +50,21 @@ func (h *FAQHandler) ListEntries(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
tagID := secutils.SanitizeForLog(c.Query("tag_id"))
|
||||
var tagSeqID int64
|
||||
tagIDStr := c.Query("tag_id")
|
||||
if tagIDStr != "" {
|
||||
var err error
|
||||
tagSeqID, err = strconv.ParseInt(tagIDStr, 10, 64)
|
||||
if err != nil {
|
||||
c.Error(errors.NewBadRequestError("tag_id 必须是整数"))
|
||||
return
|
||||
}
|
||||
}
|
||||
keyword := secutils.SanitizeForLog(c.Query("keyword"))
|
||||
searchField := secutils.SanitizeForLog(c.Query("search_field"))
|
||||
sortOrder := secutils.SanitizeForLog(c.Query("sort_order"))
|
||||
|
||||
result, err := h.knowledgeService.ListFAQEntries(ctx, secutils.SanitizeForLog(c.Param("id")), &page, tagID, keyword, searchField, sortOrder)
|
||||
result, err := h.knowledgeService.ListFAQEntries(ctx, secutils.SanitizeForLog(c.Param("id")), &page, tagSeqID, keyword, searchField, sortOrder)
|
||||
if err != nil {
|
||||
logger.ErrorWithFields(ctx, err, nil)
|
||||
c.Error(err)
|
||||
@@ -69,7 +79,9 @@ func (h *FAQHandler) ListEntries(c *gin.Context) {
|
||||
|
||||
// UpsertEntries godoc
|
||||
// @Summary 批量更新/插入FAQ条目
|
||||
// @Description 异步批量更新或插入FAQ条目
|
||||
// @Description 异步批量更新或插入FAQ条目。支持 dry_run 模式(设置 dry_run=true),异步验证不实际导入。
|
||||
// @Description dry_run 模式是异步操作,返回 task_id,通过 /faq/import/progress/{task_id} 查询进度和结果。
|
||||
// @Description 验证内容包括:1) 条目基本格式 2) 重复问题(批次内和知识库已有) 3) 内容安全检查。
|
||||
// @Tags FAQ管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
@@ -89,7 +101,10 @@ func (h *FAQHandler) UpsertEntries(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
taskID, err := h.knowledgeService.UpsertFAQEntries(ctx, secutils.SanitizeForLog(c.Param("id")), &req)
|
||||
kbID := secutils.SanitizeForLog(c.Param("id"))
|
||||
|
||||
// 统一使用 UpsertFAQEntries,通过 DryRun 字段区分模式
|
||||
taskID, err := h.knowledgeService.UpsertFAQEntries(ctx, kbID, &req)
|
||||
if err != nil {
|
||||
logger.ErrorWithFields(ctx, err, nil)
|
||||
c.Error(err)
|
||||
@@ -146,7 +161,7 @@ func (h *FAQHandler) CreateEntry(c *gin.Context) {
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path string true "知识库ID"
|
||||
// @Param entry_id path string true "FAQ条目ID"
|
||||
// @Param entry_id path int true "FAQ条目ID(seq_id)"
|
||||
// @Param request body types.FAQEntryPayload true "FAQ条目"
|
||||
// @Success 200 {object} map[string]interface{} "更新成功"
|
||||
// @Failure 400 {object} errors.AppError "请求参数错误"
|
||||
@@ -162,8 +177,15 @@ func (h *FAQHandler) UpdateEntry(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.knowledgeService.UpdateFAQEntry(ctx,
|
||||
secutils.SanitizeForLog(c.Param("id")), secutils.SanitizeForLog(c.Param("entry_id")), &req); err != nil {
|
||||
entrySeqID, err := strconv.ParseInt(c.Param("entry_id"), 10, 64)
|
||||
if err != nil {
|
||||
c.Error(errors.NewBadRequestError("entry_id 必须是整数"))
|
||||
return
|
||||
}
|
||||
|
||||
entry, err := h.knowledgeService.UpdateFAQEntry(ctx,
|
||||
secutils.SanitizeForLog(c.Param("id")), entrySeqID, &req)
|
||||
if err != nil {
|
||||
logger.ErrorWithFields(ctx, err, nil)
|
||||
c.Error(err)
|
||||
return
|
||||
@@ -171,6 +193,7 @@ func (h *FAQHandler) UpdateEntry(c *gin.Context) {
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"data": entry,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -240,12 +263,18 @@ func (h *FAQHandler) UpdateEntryFieldsBatch(c *gin.Context) {
|
||||
|
||||
// faqDeleteRequest is a request for deleting FAQ entries in batch
|
||||
type faqDeleteRequest struct {
|
||||
IDs []string `json:"ids" binding:"required,min=1,dive,required"`
|
||||
IDs []int64 `json:"ids" binding:"required,min=1"`
|
||||
}
|
||||
|
||||
// faqEntryTagBatchRequest is a request for updating tags for FAQ entries in batch
|
||||
// key: entry seq_id, value: tag seq_id (nil to remove tag)
|
||||
type faqEntryTagBatchRequest struct {
|
||||
Updates map[string]*string `json:"updates" binding:"required,min=1"`
|
||||
Updates map[int64]*int64 `json:"updates" binding:"required,min=1"`
|
||||
}
|
||||
|
||||
// addSimilarQuestionsRequest is a request for adding similar questions to a FAQ entry
|
||||
type addSimilarQuestionsRequest struct {
|
||||
SimilarQuestions []string `json:"similar_questions" binding:"required,min=1"`
|
||||
}
|
||||
|
||||
// DeleteEntries godoc
|
||||
@@ -255,7 +284,7 @@ type faqEntryTagBatchRequest struct {
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path string true "知识库ID"
|
||||
// @Param request body object{ids=[]string} true "要删除的FAQ ID列表"
|
||||
// @Param request body object{ids=[]int} true "要删除的FAQ ID列表(seq_id)"
|
||||
// @Success 200 {object} map[string]interface{} "删除成功"
|
||||
// @Failure 400 {object} errors.AppError "请求参数错误"
|
||||
// @Security Bearer
|
||||
@@ -272,7 +301,7 @@ func (h *FAQHandler) DeleteEntries(c *gin.Context) {
|
||||
|
||||
if err := h.knowledgeService.DeleteFAQEntries(ctx,
|
||||
secutils.SanitizeForLog(c.Param("id")),
|
||||
secutils.SanitizeForLogArray(req.IDs)); err != nil {
|
||||
req.IDs); err != nil {
|
||||
logger.ErrorWithFields(ctx, err, nil)
|
||||
c.Error(err)
|
||||
return
|
||||
@@ -362,7 +391,7 @@ func (h *FAQHandler) ExportEntries(c *gin.Context) {
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path string true "知识库ID"
|
||||
// @Param entry_id path string true "FAQ条目ID"
|
||||
// @Param entry_id path int true "FAQ条目ID(seq_id)"
|
||||
// @Success 200 {object} map[string]interface{} "FAQ条目详情"
|
||||
// @Failure 400 {object} errors.AppError "请求参数错误"
|
||||
// @Failure 404 {object} errors.AppError "条目不存在"
|
||||
@@ -372,9 +401,13 @@ func (h *FAQHandler) ExportEntries(c *gin.Context) {
|
||||
func (h *FAQHandler) GetEntry(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
kbID := secutils.SanitizeForLog(c.Param("id"))
|
||||
entryID := secutils.SanitizeForLog(c.Param("entry_id"))
|
||||
entrySeqID, err := strconv.ParseInt(c.Param("entry_id"), 10, 64)
|
||||
if err != nil {
|
||||
c.Error(errors.NewBadRequestError("entry_id 必须是整数"))
|
||||
return
|
||||
}
|
||||
|
||||
entry, err := h.knowledgeService.GetFAQEntry(ctx, kbID, entryID)
|
||||
entry, err := h.knowledgeService.GetFAQEntry(ctx, kbID, entrySeqID)
|
||||
if err != nil {
|
||||
logger.ErrorWithFields(ctx, err, nil)
|
||||
c.Error(err)
|
||||
@@ -415,3 +448,89 @@ func (h *FAQHandler) GetImportProgress(c *gin.Context) {
|
||||
"data": progress,
|
||||
})
|
||||
}
|
||||
|
||||
// updateLastFAQImportResultDisplayStatusRequest is the request payload for UpdateLastImportResultDisplayStatus
|
||||
type updateLastFAQImportResultDisplayStatusRequest struct {
|
||||
DisplayStatus string `json:"display_status" binding:"required,oneof=open close"`
|
||||
}
|
||||
|
||||
// UpdateLastImportResultDisplayStatus godoc
|
||||
// @Summary 更新FAQ最后一次导入结果显示状态
|
||||
// @Description 更新FAQ知识库导入结果统计卡片的显示或隐藏状态
|
||||
// @Tags FAQ管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path string true "知识库ID"
|
||||
// @Param request body updateLastFAQImportResultDisplayStatusRequest true "状态更新请求"
|
||||
// @Success 200 {object} map[string]interface{} "更新成功"
|
||||
// @Failure 400 {object} errors.AppError "请求参数错误"
|
||||
// @Failure 404 {object} errors.AppError "知识库不存在或无导入记录"
|
||||
// @Security Bearer
|
||||
// @Security ApiKeyAuth
|
||||
// @Router /knowledge-bases/{id}/faq/import/last-result/display [put]
|
||||
func (h *FAQHandler) UpdateLastImportResultDisplayStatus(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
kbID := secutils.SanitizeForLog(c.Param("id"))
|
||||
|
||||
var req updateLastFAQImportResultDisplayStatusRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
logger.Error(ctx, "Failed to bind display status update payload", err)
|
||||
c.Error(errors.NewBadRequestError("请求参数不合法").WithDetails(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.knowledgeService.UpdateLastFAQImportResultDisplayStatus(ctx, kbID, req.DisplayStatus); err != nil {
|
||||
logger.ErrorWithFields(ctx, err, nil)
|
||||
c.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
})
|
||||
}
|
||||
|
||||
// AddSimilarQuestions godoc
|
||||
// @Summary 添加相似问
|
||||
// @Description 向指定的FAQ条目添加相似问题
|
||||
// @Tags FAQ管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path string true "知识库ID"
|
||||
// @Param entry_id path int true "FAQ条目ID(seq_id)"
|
||||
// @Param request body addSimilarQuestionsRequest true "相似问列表"
|
||||
// @Success 200 {object} map[string]interface{} "更新后的FAQ条目"
|
||||
// @Failure 400 {object} errors.AppError "请求参数错误"
|
||||
// @Failure 404 {object} errors.AppError "条目不存在"
|
||||
// @Security Bearer
|
||||
// @Security ApiKeyAuth
|
||||
// @Router /knowledge-bases/{id}/faq/entries/{entry_id}/similar-questions [post]
|
||||
func (h *FAQHandler) AddSimilarQuestions(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
kbID := secutils.SanitizeForLog(c.Param("id"))
|
||||
|
||||
entrySeqID, err := strconv.ParseInt(c.Param("entry_id"), 10, 64)
|
||||
if err != nil {
|
||||
c.Error(errors.NewBadRequestError("entry_id 必须是整数"))
|
||||
return
|
||||
}
|
||||
|
||||
var req addSimilarQuestionsRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
logger.Error(ctx, "Failed to bind add similar questions payload", err)
|
||||
c.Error(errors.NewBadRequestError("请求参数不合法").WithDetails(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
entry, err := h.knowledgeService.AddSimilarQuestions(ctx, kbID, entrySeqID, req.SimilarQuestions)
|
||||
if err != nil {
|
||||
logger.ErrorWithFields(ctx, err, nil)
|
||||
c.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"data": entry,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -10,9 +10,9 @@ import (
|
||||
"github.com/Tencent/WeKnora/internal/logger"
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
||||
"github.com/Tencent/WeKnora/internal/utils"
|
||||
secutils "github.com/Tencent/WeKnora/internal/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
"github.com/hibiken/asynq"
|
||||
)
|
||||
|
||||
@@ -375,7 +375,7 @@ func (h *KnowledgeBaseHandler) CopyKnowledgeBase(c *gin.Context) {
|
||||
// Generate task ID if not provided
|
||||
taskID := req.TaskID
|
||||
if taskID == "" {
|
||||
taskID = uuid.New().String()
|
||||
taskID = utils.GenerateTaskID("kb_clone", tenantID.(uint64), req.SourceID)
|
||||
}
|
||||
|
||||
// Create KB clone payload
|
||||
@@ -394,7 +394,8 @@ func (h *KnowledgeBaseHandler) CopyKnowledgeBase(c *gin.Context) {
|
||||
}
|
||||
|
||||
// Enqueue KB clone task to Asynq
|
||||
task := asynq.NewTask(types.TypeKBClone, payloadBytes, asynq.Queue("default"), asynq.MaxRetry(3))
|
||||
task := asynq.NewTask(types.TypeKBClone, payloadBytes,
|
||||
asynq.TaskID(taskID), asynq.Queue("default"), asynq.MaxRetry(3))
|
||||
info, err := h.asynqClient.Enqueue(task)
|
||||
if err != nil {
|
||||
logger.Errorf(ctx, "Failed to enqueue KB clone task: %v", err)
|
||||
|
||||
@@ -1,179 +0,0 @@
|
||||
# Session Handler 重构总结
|
||||
|
||||
## 📋 优化概述
|
||||
|
||||
本次重构主要通过提取公共辅助函数来简化代码,消除重复逻辑,提高代码的可维护性和可读性。
|
||||
|
||||
## 🆕 新增文件
|
||||
|
||||
### `helpers.go` - 辅助函数集合
|
||||
|
||||
创建了一个专门的辅助函数文件,包含以下功能:
|
||||
|
||||
#### SSE 相关
|
||||
- **`setSSEHeaders(c *gin.Context)`** - 设置 SSE 标准头部
|
||||
- **`sendCompletionEvent(c, requestID)`** - 发送完成事件
|
||||
- **`buildStreamResponse(evt, requestID)`** - 从 StreamEvent 构建 StreamResponse
|
||||
|
||||
#### 事件和流处理
|
||||
- **`createAgentQueryEvent(sessionID, assistantMessageID)`** - 创建 agent query 事件
|
||||
- **`writeAgentQueryEvent(ctx, sessionID, assistantMessageID)`** - 写入 agent query 事件到流管理器
|
||||
|
||||
#### 消息处理
|
||||
- **`createUserMessage(ctx, sessionID, query, requestID)`** - 创建用户消息
|
||||
- **`createAssistantMessage(ctx, assistantMessage)`** - 创建助手消息
|
||||
|
||||
#### StreamHandler 设置
|
||||
- **`setupStreamHandler(...)`** - 创建并订阅流处理器
|
||||
- **`setupStopEventHandler(...)`** - 注册停止事件处理器
|
||||
|
||||
#### 配置相关
|
||||
- **`createDefaultSummaryConfig()`** - 创建默认摘要配置
|
||||
- **`fillSummaryConfigDefaults(config)`** - 填充摘要配置默认值
|
||||
|
||||
#### 工具函数
|
||||
- **`validateSessionID(c)`** - 验证并提取 session ID
|
||||
- **`getRequestID(c)`** - 获取 request ID
|
||||
- **`getString(m, key)`** - 安全获取字符串值
|
||||
- **`getFloat64(m, key)`** - 安全获取浮点数值
|
||||
|
||||
## 🔄 优化的文件
|
||||
|
||||
### 1. `agent_stream_handler.go`
|
||||
**减少行数**: 428 → 410 行 (-18 行)
|
||||
|
||||
**优化内容**:
|
||||
- 移除了重复的辅助函数 `getString` 和 `getFloat64`(现在在 `helpers.go` 中)
|
||||
|
||||
### 2. `stream.go`
|
||||
**减少行数**: 440 → 364 行 (-76 行, **-17.3%**)
|
||||
|
||||
**优化内容**:
|
||||
- 使用 `setSSEHeaders()` 替代重复的 4 行头部设置代码
|
||||
- 使用 `buildStreamResponse()` 替代 10+ 行的响应构建逻辑(3 处)
|
||||
- 使用 `sendCompletionEvent()` 替代重复的完成事件发送代码(3 处)
|
||||
|
||||
**优化示例**:
|
||||
```go
|
||||
// Before (10+ lines)
|
||||
response := &types.StreamResponse{
|
||||
ID: message.RequestID,
|
||||
ResponseType: evt.Type,
|
||||
Content: evt.Content,
|
||||
Done: evt.Done,
|
||||
Data: evt.Data,
|
||||
}
|
||||
if evt.Type == types.ResponseTypeReferences {
|
||||
if refs, ok := evt.Data["references"].(types.References); ok {
|
||||
response.KnowledgeReferences = refs
|
||||
}
|
||||
}
|
||||
|
||||
// After (1 line)
|
||||
response := buildStreamResponse(evt, message.RequestID)
|
||||
```
|
||||
|
||||
### 3. `qa.go`
|
||||
**减少行数**: 536 → 485 行 (-51 行, **-9.5%**)
|
||||
|
||||
**优化内容**:
|
||||
- 使用 `setSSEHeaders()` 替代重复的头部设置(2 处)
|
||||
- 使用 `createUserMessage()` 替代 9 行的用户消息创建(3 处)
|
||||
- 使用 `createAssistantMessage()` 替代 3 行的助手消息创建(3 处)
|
||||
- 使用 `writeAgentQueryEvent()` 替代 15+ 行的事件写入代码(2 处)
|
||||
- 使用 `setupStreamHandler()` 替代 7 行的处理器设置(2 处)
|
||||
- 使用 `setupStopEventHandler()` 替代 7 行的停止事件处理器设置(2 处)
|
||||
- 使用 `getRequestID()` 简化请求 ID 获取(1 处)
|
||||
|
||||
### 4. `handler.go`
|
||||
**减少行数**: 354 → 312 行 (-42 行, **-11.9%**)
|
||||
|
||||
**优化内容**:
|
||||
- 使用 `createDefaultSummaryConfig()` 替代 12 行的配置创建(2 处)
|
||||
- 使用 `fillSummaryConfigDefaults()` 替代 9 行的默认值填充(1 处)
|
||||
|
||||
**优化示例**:
|
||||
```go
|
||||
// Before (21 lines)
|
||||
if request.SessionStrategy.SummaryParameters != nil {
|
||||
createdSession.SummaryParameters = request.SessionStrategy.SummaryParameters
|
||||
} else {
|
||||
createdSession.SummaryParameters = &types.SummaryConfig{
|
||||
MaxTokens: h.config.Conversation.Summary.MaxTokens,
|
||||
TopP: h.config.Conversation.Summary.TopP,
|
||||
// ... 8 more fields
|
||||
}
|
||||
}
|
||||
if createdSession.SummaryParameters.Prompt == "" {
|
||||
createdSession.SummaryParameters.Prompt = h.config.Conversation.Summary.Prompt
|
||||
}
|
||||
// ... 2 more field checks
|
||||
|
||||
// After (5 lines)
|
||||
if request.SessionStrategy.SummaryParameters != nil {
|
||||
createdSession.SummaryParameters = request.SessionStrategy.SummaryParameters
|
||||
} else {
|
||||
createdSession.SummaryParameters = h.createDefaultSummaryConfig()
|
||||
}
|
||||
h.fillSummaryConfigDefaults(createdSession.SummaryParameters)
|
||||
```
|
||||
|
||||
## 📊 总体统计
|
||||
|
||||
| 文件 | 优化前 | 优化后 | 减少 | 比例 |
|
||||
|------|-------|-------|------|------|
|
||||
| agent_stream_handler.go | 428 | 410 | -18 | -4.2% |
|
||||
| stream.go | 440 | 364 | -76 | -17.3% |
|
||||
| qa.go | 536 | 485 | -51 | -9.5% |
|
||||
| handler.go | 354 | 312 | -42 | -11.9% |
|
||||
| **总计** | **1,758** | **1,571** | **-187** | **-10.6%** |
|
||||
| helpers.go (新增) | 0 | 204 | +204 | - |
|
||||
| **净变化** | **1,758** | **1,775** | **+17** | **+1.0%** |
|
||||
|
||||
虽然总行数略有增加(+17 行),但代码质量显著提升:
|
||||
- ✅ 消除了大量重复代码
|
||||
- ✅ 提高了代码复用性
|
||||
- ✅ 增强了可维护性
|
||||
- ✅ 统一了代码风格
|
||||
- ✅ 便于未来扩展
|
||||
|
||||
## 🎯 关键改进
|
||||
|
||||
### 1. **代码复用性**
|
||||
通过提取公共函数,同样的逻辑只需维护一处,修改时更新一个地方即可。
|
||||
|
||||
### 2. **可读性提升**
|
||||
```go
|
||||
// Before: 需要阅读 10+ 行才能理解
|
||||
response := &types.StreamResponse{ /* 10 lines */ }
|
||||
|
||||
// After: 一行就能明白意图
|
||||
response := buildStreamResponse(evt, requestID)
|
||||
```
|
||||
|
||||
### 3. **一致性**
|
||||
所有 SSE 头部设置、消息创建、事件处理都使用统一的方法,降低出错风险。
|
||||
|
||||
### 4. **易于测试**
|
||||
辅助函数可以独立测试,提高单元测试的覆盖率。
|
||||
|
||||
### 5. **便于维护**
|
||||
如果需要修改 SSE 头部或事件格式,只需修改辅助函数,不需要搜索整个代码库。
|
||||
|
||||
## ✅ 验证结果
|
||||
|
||||
- ✅ 无 linter 错误
|
||||
- ✅ 编译成功
|
||||
- ✅ 保持原有功能不变
|
||||
- ✅ 代码结构更清晰
|
||||
|
||||
## 🔮 未来建议
|
||||
|
||||
1. **测试覆盖**: 为 `helpers.go` 中的辅助函数添加单元测试
|
||||
2. **文档完善**: 为复杂的辅助函数添加使用示例
|
||||
3. **持续优化**: 定期审查是否有新的重复代码可以提取
|
||||
|
||||
## 📝 总结
|
||||
|
||||
本次重构成功地消除了代码重复,提高了代码质量。虽然增加了一个新文件,但整体代码结构更加清晰,维护成本大幅降低。重构遵循了 DRY(Don't Repeat Yourself)原则,为未来的开发和维护打下了良好的基础。
|
||||
|
||||
+63
-8
@@ -1,7 +1,9 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
@@ -15,16 +17,44 @@ import (
|
||||
// TagHandler handles knowledge base tag operations.
|
||||
type TagHandler struct {
|
||||
tagService interfaces.KnowledgeTagService
|
||||
tagRepo interfaces.KnowledgeTagRepository
|
||||
chunkRepo interfaces.ChunkRepository
|
||||
}
|
||||
|
||||
// DeleteTagRequest represents the request body for deleting a tag
|
||||
type DeleteTagRequest struct {
|
||||
ExcludeIDs []string `json:"exclude_ids"` // Chunk IDs to exclude from deletion
|
||||
ExcludeIDs []int64 `json:"exclude_ids"` // Chunk seq_ids to exclude from deletion
|
||||
}
|
||||
|
||||
// NewTagHandler creates a new TagHandler.
|
||||
func NewTagHandler(tagService interfaces.KnowledgeTagService) *TagHandler {
|
||||
return &TagHandler{tagService: tagService}
|
||||
func NewTagHandler(tagService interfaces.KnowledgeTagService, tagRepo interfaces.KnowledgeTagRepository, chunkRepo interfaces.ChunkRepository) *TagHandler {
|
||||
return &TagHandler{tagService: tagService, tagRepo: tagRepo, chunkRepo: chunkRepo}
|
||||
}
|
||||
|
||||
// resolveTagID resolves tag_id parameter which can be either UUID or seq_id (integer).
|
||||
// Returns the UUID of the tag.
|
||||
func (h *TagHandler) resolveTagID(c *gin.Context) (string, error) {
|
||||
ctx := c.Request.Context()
|
||||
tagIDParam := secutils.SanitizeForLog(c.Param("tag_id"))
|
||||
|
||||
// Try to parse as integer (seq_id)
|
||||
if seqID, err := strconv.ParseInt(tagIDParam, 10, 64); err == nil {
|
||||
// It's an integer, look up by seq_id
|
||||
tenantID := ctx.Value(types.TenantIDContextKey).(uint64)
|
||||
tag, err := h.tagRepo.GetBySeqID(ctx, tenantID, seqID)
|
||||
if err != nil {
|
||||
return "", errors.NewNotFoundError("标签不存在")
|
||||
}
|
||||
return tag.ID, nil
|
||||
}
|
||||
|
||||
// It's a UUID string
|
||||
return tagIDParam, nil
|
||||
}
|
||||
|
||||
// getChunksBySeqIDs retrieves chunks by their seq_ids.
|
||||
func (h *TagHandler) getChunksBySeqIDs(ctx context.Context, tenantID uint64, seqIDs []int64) ([]*types.Chunk, error) {
|
||||
return h.chunkRepo.ListChunksBySeqID(ctx, tenantID, seqIDs)
|
||||
}
|
||||
|
||||
// ListTags godoc
|
||||
@@ -127,7 +157,7 @@ type updateTagRequest struct {
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path string true "知识库ID"
|
||||
// @Param tag_id path string true "标签ID"
|
||||
// @Param tag_id path string true "标签ID (UUID或seq_id)"
|
||||
// @Param request body object true "标签更新信息"
|
||||
// @Success 200 {object} map[string]interface{} "更新后的标签"
|
||||
// @Failure 400 {object} errors.AppError "请求参数错误"
|
||||
@@ -137,7 +167,12 @@ type updateTagRequest struct {
|
||||
func (h *TagHandler) UpdateTag(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
|
||||
tagID := secutils.SanitizeForLog(c.Param("tag_id"))
|
||||
tagID, err := h.resolveTagID(c)
|
||||
if err != nil {
|
||||
c.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
var req updateTagRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
logger.Error(ctx, "Failed to bind update tag payload", err)
|
||||
@@ -167,7 +202,7 @@ func (h *TagHandler) UpdateTag(c *gin.Context) {
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path string true "知识库ID"
|
||||
// @Param tag_id path string true "标签ID"
|
||||
// @Param tag_id path string true "标签ID (UUID或seq_id)"
|
||||
// @Param force query bool false "强制删除"
|
||||
// @Param content_only query bool false "仅删除内容,保留标签"
|
||||
// @Param body body DeleteTagRequest false "删除选项"
|
||||
@@ -178,7 +213,12 @@ func (h *TagHandler) UpdateTag(c *gin.Context) {
|
||||
// @Router /knowledge-bases/{id}/tags/{tag_id} [delete]
|
||||
func (h *TagHandler) DeleteTag(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
tagID := secutils.SanitizeForLog(c.Param("tag_id"))
|
||||
|
||||
tagID, err := h.resolveTagID(c)
|
||||
if err != nil {
|
||||
c.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
force := c.Query("force") == "true"
|
||||
contentOnly := c.Query("content_only") == "true"
|
||||
@@ -187,7 +227,22 @@ func (h *TagHandler) DeleteTag(c *gin.Context) {
|
||||
// Ignore bind error since body is optional
|
||||
_ = c.ShouldBindJSON(&req)
|
||||
|
||||
if err := h.tagService.DeleteTag(ctx, tagID, force, contentOnly, req.ExcludeIDs); err != nil {
|
||||
// Convert seq_ids to UUIDs for excludeIDs
|
||||
var excludeUUIDs []string
|
||||
if len(req.ExcludeIDs) > 0 {
|
||||
tenantID := ctx.Value(types.TenantIDContextKey).(uint64)
|
||||
chunks, err := h.getChunksBySeqIDs(ctx, tenantID, req.ExcludeIDs)
|
||||
if err != nil {
|
||||
logger.Warnf(ctx, "Failed to resolve exclude_ids: %v", err)
|
||||
} else {
|
||||
excludeUUIDs = make([]string, len(chunks))
|
||||
for i, chunk := range chunks {
|
||||
excludeUUIDs[i] = chunk.ID
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if err := h.tagService.DeleteTag(ctx, tagID, force, contentOnly, excludeUUIDs); err != nil {
|
||||
logger.ErrorWithFields(ctx, err, map[string]interface{}{
|
||||
"tag_id": tagID,
|
||||
})
|
||||
|
||||
+2
-31
@@ -9,7 +9,6 @@ import (
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/logger"
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
secutils "github.com/Tencent/WeKnora/internal/utils"
|
||||
"github.com/mark3labs/mcp-go/client"
|
||||
"github.com/mark3labs/mcp-go/client/transport"
|
||||
"github.com/mark3labs/mcp-go/mcp"
|
||||
@@ -119,36 +118,8 @@ func NewMCPClient(config *ClientConfig) (MCPClient, error) {
|
||||
return nil, fmt.Errorf("failed to create HTTP streamable client: %w", err)
|
||||
}
|
||||
case types.MCPTransportStdio:
|
||||
if config.Service.StdioConfig == nil {
|
||||
return nil, fmt.Errorf("stdio_config is required for stdio transport")
|
||||
}
|
||||
|
||||
// Security validation: validate command, args, and env vars before execution
|
||||
// This prevents command injection attacks (CWE-78)
|
||||
if err := secutils.ValidateStdioConfig(
|
||||
config.Service.StdioConfig.Command,
|
||||
config.Service.StdioConfig.Args,
|
||||
config.Service.EnvVars,
|
||||
); err != nil {
|
||||
return nil, fmt.Errorf("stdio configuration validation failed: %w", err)
|
||||
}
|
||||
|
||||
// Convert env vars map to []string format (KEY=value)
|
||||
envVars := make([]string, 0, len(config.Service.EnvVars))
|
||||
for key, value := range config.Service.EnvVars {
|
||||
envVars = append(envVars, fmt.Sprintf("%s=%s", key, value))
|
||||
}
|
||||
|
||||
// Create stdio client with options
|
||||
// NewStdioMCPClientWithOptions(command string, env []string, args []string, opts ...transport.StdioOption)
|
||||
mcpClient, err = client.NewStdioMCPClientWithOptions(
|
||||
config.Service.StdioConfig.Command,
|
||||
envVars,
|
||||
config.Service.StdioConfig.Args,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create stdio client: %w", err)
|
||||
}
|
||||
// Stdio transport is disabled for security reasons (potential command injection vulnerabilities)
|
||||
return nil, fmt.Errorf("stdio transport is disabled for security reasons; please use SSE or HTTP Streamable transport instead")
|
||||
default:
|
||||
return nil, ErrUnsupportedTransport
|
||||
}
|
||||
|
||||
+4
-30
@@ -35,17 +35,17 @@ func NewMCPManager() *MCPManager {
|
||||
}
|
||||
|
||||
// GetOrCreateClient gets an existing client or creates a new one
|
||||
// For stdio transport, always creates a new client (not cached)
|
||||
// For SSE/HTTP Streamable, caches and reuses existing connections
|
||||
// Caches and reuses existing connections for SSE/HTTP Streamable
|
||||
// Note: Stdio transport is disabled for security reasons
|
||||
func (m *MCPManager) GetOrCreateClient(service *types.MCPService) (MCPClient, error) {
|
||||
// Check if service is enabled
|
||||
if !service.Enabled {
|
||||
return nil, fmt.Errorf("MCP service %s is not enabled", service.Name)
|
||||
}
|
||||
|
||||
// For stdio transport, always create a new client (don't cache)
|
||||
// Stdio transport is disabled for security reasons
|
||||
if service.TransportType == types.MCPTransportStdio {
|
||||
return m.createStdioClient(service)
|
||||
return nil, fmt.Errorf("stdio transport is disabled for security reasons; please use SSE or HTTP Streamable transport instead")
|
||||
}
|
||||
|
||||
// For SSE/HTTP Streamable, check if client already exists and reuse
|
||||
@@ -95,32 +95,6 @@ func (m *MCPManager) GetOrCreateClient(service *types.MCPService) (MCPClient, er
|
||||
return client, nil
|
||||
}
|
||||
|
||||
// createStdioClient creates a new stdio client (not cached)
|
||||
func (m *MCPManager) createStdioClient(service *types.MCPService) (MCPClient, error) {
|
||||
// Create new client
|
||||
config := &ClientConfig{
|
||||
Service: service,
|
||||
}
|
||||
|
||||
client, err := NewMCPClient(config)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create stdio MCP client: %w", err)
|
||||
}
|
||||
|
||||
// For stdio, Connect() starts the subprocess
|
||||
// Use manager's context for the connection lifecycle
|
||||
if err := client.Connect(m.ctx); err != nil {
|
||||
return nil, fmt.Errorf("failed to connect to stdio MCP service: %w", err)
|
||||
}
|
||||
|
||||
if err := m.initializeClient(service, client, "failed to initialize stdio MCP client"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
logger.GetLogger(m.ctx).Infof("MCP stdio client created and initialized for service: %s", service.Name)
|
||||
return client, nil
|
||||
}
|
||||
|
||||
// initializeClient handles the shared initialization flow with timeout enforcement.
|
||||
func (m *MCPManager) initializeClient(service *types.MCPService, client MCPClient, errPrefix string) error {
|
||||
initTimeout := 30 * time.Second
|
||||
|
||||
@@ -189,11 +189,14 @@ func RegisterFAQRoutes(r *gin.RouterGroup, handler *handler.FAQHandler) {
|
||||
faq.POST("/entries", handler.UpsertEntries)
|
||||
faq.POST("/entry", handler.CreateEntry)
|
||||
faq.PUT("/entries/:entry_id", handler.UpdateEntry)
|
||||
faq.POST("/entries/:entry_id/similar-questions", handler.AddSimilarQuestions)
|
||||
// Unified batch update API - supports is_enabled, is_recommended, tag_id
|
||||
faq.PUT("/entries/fields", handler.UpdateEntryFieldsBatch)
|
||||
faq.PUT("/entries/tags", handler.UpdateEntryTagBatch)
|
||||
faq.DELETE("/entries", handler.DeleteEntries)
|
||||
faq.POST("/search", handler.SearchFAQ)
|
||||
// FAQ import result display status
|
||||
faq.PUT("/import/last-result/display", handler.UpdateLastImportResultDisplayStatus)
|
||||
}
|
||||
// FAQ import progress route (outside of knowledge-base scope)
|
||||
faqImport := r.Group("/faq/import")
|
||||
|
||||
@@ -76,7 +76,7 @@ func RunAsynqServer(params AsynqTaskParams) *asynq.ServeMux {
|
||||
// Register document processing handler
|
||||
mux.HandleFunc(types.TypeDocumentProcess, params.KnowledgeService.ProcessDocument)
|
||||
|
||||
// Register FAQ import handler
|
||||
// Register FAQ import handler (includes dry run mode)
|
||||
mux.HandleFunc(types.TypeFAQImport, params.KnowledgeService.ProcessFAQImport)
|
||||
|
||||
// Register question generation handler
|
||||
@@ -88,6 +88,9 @@ func RunAsynqServer(params AsynqTaskParams) *asynq.ServeMux {
|
||||
// Register KB clone handler
|
||||
mux.HandleFunc(types.TypeKBClone, params.KnowledgeService.ProcessKBClone)
|
||||
|
||||
// Register knowledge list delete handler
|
||||
mux.HandleFunc(types.TypeKnowledgeListDelete, params.KnowledgeService.ProcessKnowledgeListDelete)
|
||||
|
||||
// Register index delete handler
|
||||
mux.HandleFunc(types.TypeIndexDelete, params.TagService.ProcessIndexDelete)
|
||||
|
||||
|
||||
@@ -102,6 +102,8 @@ type ImageInfo struct {
|
||||
type Chunk struct {
|
||||
// Unique identifier of the chunk, using UUID format
|
||||
ID string `json:"id" gorm:"type:varchar(36);primaryKey"`
|
||||
// SeqID is an auto-increment integer ID for external API usage (FAQ entries)
|
||||
SeqID int64 `json:"seq_id" gorm:"type:bigint;uniqueIndex;autoIncrement"`
|
||||
// Tenant ID, used for multi-tenant isolation
|
||||
TenantID uint64 `json:"tenant_id"`
|
||||
// ID of the parent knowledge, associated with the Knowledge model
|
||||
|
||||
@@ -37,4 +37,5 @@ type IndexInfo struct {
|
||||
KnowledgeType string // Type of the knowledge (e.g., "faq", "manual")
|
||||
TagID string // Tag ID for categorization (used for FAQ priority filtering)
|
||||
IsEnabled bool // Whether the chunk is enabled for retrieval
|
||||
IsRecommended bool // Whether the chunk is recommended
|
||||
}
|
||||
|
||||
@@ -1,15 +1,16 @@
|
||||
package types
|
||||
|
||||
const (
|
||||
TypeChunkExtract = "chunk:extract"
|
||||
TypeDocumentProcess = "document:process" // 文档处理任务
|
||||
TypeFAQImport = "faq:import" // FAQ导入任务
|
||||
TypeQuestionGeneration = "question:generation" // 问题生成任务
|
||||
TypeSummaryGeneration = "summary:generation" // 摘要生成任务
|
||||
TypeKBClone = "kb:clone" // 知识库复制任务
|
||||
TypeIndexDelete = "index:delete" // 索引删除任务
|
||||
TypeKBDelete = "kb:delete" // 知识库删除任务
|
||||
TypeDataTableSummary = "datatable:summary" // 表格摘要任务
|
||||
TypeChunkExtract = "chunk:extract"
|
||||
TypeDocumentProcess = "document:process" // 文档处理任务
|
||||
TypeFAQImport = "faq:import" // FAQ导入任务(包含dry run模式)
|
||||
TypeQuestionGeneration = "question:generation" // 问题生成任务
|
||||
TypeSummaryGeneration = "summary:generation" // 摘要生成任务
|
||||
TypeKBClone = "kb:clone" // 知识库复制任务
|
||||
TypeIndexDelete = "index:delete" // 索引删除任务
|
||||
TypeKBDelete = "kb:delete" // 知识库删除任务
|
||||
TypeKnowledgeListDelete = "knowledge:list_delete" // 批量删除知识任务
|
||||
TypeDataTableSummary = "datatable:summary" // 表格摘要任务
|
||||
)
|
||||
|
||||
// ExtractChunkPayload represents the extract chunk task payload
|
||||
@@ -35,14 +36,15 @@ type DocumentProcessPayload struct {
|
||||
QuestionCount int `json:"question_count,omitempty"` // 每个chunk生成的问题数量
|
||||
}
|
||||
|
||||
// FAQImportPayload represents the FAQ import task payload
|
||||
// FAQImportPayload represents the FAQ import task payload (including dry run mode)
|
||||
type FAQImportPayload struct {
|
||||
TenantID uint64 `json:"tenant_id"`
|
||||
TaskID string `json:"task_id"`
|
||||
KBID string `json:"kb_id"`
|
||||
KnowledgeID string `json:"knowledge_id"`
|
||||
KnowledgeID string `json:"knowledge_id,omitempty"` // 仅非 dry run 模式需要
|
||||
Entries []FAQEntryPayload `json:"entries"`
|
||||
Mode string `json:"mode"`
|
||||
DryRun bool `json:"dry_run"` // dry run 模式只验证不导入
|
||||
}
|
||||
|
||||
// QuestionGenerationPayload represents the question generation task payload
|
||||
@@ -85,6 +87,12 @@ type KBDeletePayload struct {
|
||||
EffectiveEngines []RetrieverEngineParams `json:"effective_engines"`
|
||||
}
|
||||
|
||||
// KnowledgeListDeletePayload represents the batch knowledge delete task payload
|
||||
type KnowledgeListDeletePayload struct {
|
||||
TenantID uint64 `json:"tenant_id"`
|
||||
KnowledgeIDs []string `json:"knowledge_ids"`
|
||||
}
|
||||
|
||||
// KBCloneTaskStatus represents the status of a knowledge base clone task
|
||||
type KBCloneTaskStatus string
|
||||
|
||||
|
||||
+120
-30
@@ -175,11 +175,11 @@ const (
|
||||
|
||||
// FAQEntry 表示返回给前端的 FAQ 条目
|
||||
type FAQEntry struct {
|
||||
ID string `json:"id"`
|
||||
ID int64 `json:"id"`
|
||||
ChunkID string `json:"chunk_id"`
|
||||
KnowledgeID string `json:"knowledge_id"`
|
||||
KnowledgeBaseID string `json:"knowledge_base_id"`
|
||||
TagID string `json:"tag_id"`
|
||||
TagID int64 `json:"tag_id"`
|
||||
TagName string `json:"tag_name"`
|
||||
IsEnabled bool `json:"is_enabled"`
|
||||
IsRecommended bool `json:"is_recommended"`
|
||||
@@ -198,12 +198,14 @@ type FAQEntry struct {
|
||||
|
||||
// FAQEntryPayload 用于创建/更新 FAQ 条目的 payload
|
||||
type FAQEntryPayload struct {
|
||||
// ID 可选,用于数据迁移时指定 seq_id(必须小于自增起始值 100000000)
|
||||
ID *int64 `json:"id,omitempty"`
|
||||
StandardQuestion string `json:"standard_question" binding:"required"`
|
||||
SimilarQuestions []string `json:"similar_questions"`
|
||||
NegativeQuestions []string `json:"negative_questions"`
|
||||
Answers []string `json:"answers" binding:"required"`
|
||||
AnswerStrategy *AnswerStrategy `json:"answer_strategy,omitempty"`
|
||||
TagID string `json:"tag_id"`
|
||||
TagID int64 `json:"tag_id"`
|
||||
TagName string `json:"tag_name"`
|
||||
IsEnabled *bool `json:"is_enabled,omitempty"`
|
||||
IsRecommended *bool `json:"is_recommended,omitempty"`
|
||||
@@ -219,25 +221,59 @@ type FAQBatchUpsertPayload struct {
|
||||
Entries []FAQEntryPayload `json:"entries" binding:"required"`
|
||||
Mode string `json:"mode" binding:"oneof=append replace"`
|
||||
KnowledgeID string `json:"knowledge_id"`
|
||||
TaskID string `json:"task_id"` // 可选,如果不传则自动生成UUID
|
||||
DryRun bool `json:"dry_run"` // 仅验证,不实际导入
|
||||
}
|
||||
|
||||
// FAQFailedEntry 表示导入/验证失败的条目
|
||||
type FAQFailedEntry struct {
|
||||
Index int `json:"index"` // 条目在批次中的索引(从0开始)
|
||||
Reason string `json:"reason"` // 失败原因
|
||||
TagName string `json:"tag_name,omitempty"` // 分类
|
||||
StandardQuestion string `json:"standard_question"` // 标准问题
|
||||
SimilarQuestions []string `json:"similar_questions,omitempty"` // 相似问题
|
||||
NegativeQuestions []string `json:"negative_questions,omitempty"` // 反例问题
|
||||
Answers []string `json:"answers,omitempty"` // 答案
|
||||
AnswerAll bool `json:"answer_all,omitempty"` // 是否全部回复
|
||||
IsDisabled bool `json:"is_disabled,omitempty"` // 是否停用
|
||||
}
|
||||
|
||||
// FAQSuccessEntry 表示导入成功的条目简单信息
|
||||
type FAQSuccessEntry struct {
|
||||
Index int `json:"index"` // 条目在批次中的索引(从0开始)
|
||||
SeqID int64 `json:"seq_id"` // 导入后的条目序列ID
|
||||
TagID int64 `json:"tag_id,omitempty"` // 分类ID(seq_id)
|
||||
TagName string `json:"tag_name,omitempty"` // 分类名称
|
||||
StandardQuestion string `json:"standard_question"` // 标准问题
|
||||
}
|
||||
|
||||
// FAQDryRunResult 表示 dry_run 模式的验证结果
|
||||
type FAQDryRunResult struct {
|
||||
TaskID string `json:"task_id,omitempty"` // 异步任务ID(异步模式时返回)
|
||||
Total int `json:"total"` // 总条目数
|
||||
SuccessCount int `json:"success_count"` // 验证通过的条目数
|
||||
FailedCount int `json:"failed_count"` // 验证失败的条目数
|
||||
FailedEntries []FAQFailedEntry `json:"failed_entries"` // 失败条目详情
|
||||
}
|
||||
|
||||
// FAQSearchRequest FAQ检索请求参数
|
||||
type FAQSearchRequest struct {
|
||||
QueryText string `json:"query_text" binding:"required"`
|
||||
VectorThreshold float64 `json:"vector_threshold"`
|
||||
MatchCount int `json:"match_count"`
|
||||
FirstPriorityTagIDs []string `json:"first_priority_tag_ids"` // 第一优先级标签ID列表,限定命中范围,优先级最高
|
||||
SecondPriorityTagIDs []string `json:"second_priority_tag_ids"` // 第二优先级标签ID列表,限定命中范围,优先级低于第一优先级
|
||||
QueryText string `json:"query_text" binding:"required"`
|
||||
VectorThreshold float64 `json:"vector_threshold"`
|
||||
MatchCount int `json:"match_count"`
|
||||
FirstPriorityTagIDs []int64 `json:"first_priority_tag_ids"` // 第一优先级标签ID列表,限定命中范围,优先级最高
|
||||
SecondPriorityTagIDs []int64 `json:"second_priority_tag_ids"` // 第二优先级标签ID列表,限定命中范围,优先级低于第一优先级
|
||||
OnlyRecommended bool `json:"only_recommended"` // 是否仅返回推荐的条目
|
||||
}
|
||||
|
||||
// UntaggedTagID is the special tag ID representing uncategorized entries
|
||||
const UntaggedTagID = "__untagged__"
|
||||
// UntaggedTagName is the default tag name for entries without a tag
|
||||
const UntaggedTagName = "未分类"
|
||||
|
||||
// FAQEntryFieldsUpdate 单个FAQ条目的字段更新
|
||||
type FAQEntryFieldsUpdate struct {
|
||||
IsEnabled *bool `json:"is_enabled,omitempty"`
|
||||
IsRecommended *bool `json:"is_recommended,omitempty"`
|
||||
TagID *string `json:"tag_id,omitempty"`
|
||||
IsEnabled *bool `json:"is_enabled,omitempty"`
|
||||
IsRecommended *bool `json:"is_recommended,omitempty"`
|
||||
TagID *int64 `json:"tag_id,omitempty"`
|
||||
// 后续可扩展更多字段
|
||||
}
|
||||
|
||||
@@ -246,12 +282,12 @@ type FAQEntryFieldsUpdate struct {
|
||||
// 1. 按条目ID更新:使用 ByID 字段
|
||||
// 2. 按Tag更新:使用 ByTag 字段,将该Tag下所有条目应用相同的更新
|
||||
type FAQEntryFieldsBatchUpdate struct {
|
||||
// ByID 按条目ID更新,key为条目ID
|
||||
ByID map[string]FAQEntryFieldsUpdate `json:"by_id,omitempty"`
|
||||
// ByTag 按Tag批量更新,key为TagID(__untagged__表示未分类)
|
||||
ByTag map[string]FAQEntryFieldsUpdate `json:"by_tag,omitempty"`
|
||||
// ExcludeIDs 在ByTag操作中需要排除的ID列表
|
||||
ExcludeIDs []string `json:"exclude_ids,omitempty"`
|
||||
// ByID 按条目ID更新,key为条目ID (seq_id)
|
||||
ByID map[int64]FAQEntryFieldsUpdate `json:"by_id,omitempty"`
|
||||
// ByTag 按Tag批量更新,key为TagID (seq_id)
|
||||
ByTag map[int64]FAQEntryFieldsUpdate `json:"by_tag,omitempty"`
|
||||
// ExcludeIDs 在ByTag操作中需要排除的ID列表 (seq_id)
|
||||
ExcludeIDs []int64 `json:"exclude_ids,omitempty"`
|
||||
}
|
||||
|
||||
// FAQImportTaskStatus 导入任务状态
|
||||
@@ -269,18 +305,33 @@ const (
|
||||
)
|
||||
|
||||
// FAQImportProgress represents the progress of an FAQ import task stored in Redis
|
||||
// When Status is "completed", the result fields (SkippedCount, ImportMode, ImportedAt, DisplayStatus, ProcessingTime) are populated.
|
||||
type FAQImportProgress struct {
|
||||
TaskID string `json:"task_id"` // UUID for the import task
|
||||
KBID string `json:"kb_id"` // Knowledge Base ID
|
||||
KnowledgeID string `json:"knowledge_id"` // FAQ Knowledge ID
|
||||
Status FAQImportTaskStatus `json:"status"` // Task status
|
||||
Progress int `json:"progress"` // 0-100 percentage
|
||||
Total int `json:"total"` // Total entries to import
|
||||
Processed int `json:"processed"` // Entries processed so far
|
||||
Message string `json:"message"` // Status message
|
||||
Error string `json:"error"` // Error message if failed
|
||||
CreatedAt int64 `json:"created_at"` // Task creation timestamp
|
||||
UpdatedAt int64 `json:"updated_at"` // Last update timestamp
|
||||
TaskID string `json:"task_id"` // UUID for the import task
|
||||
KBID string `json:"kb_id"` // Knowledge Base ID
|
||||
KnowledgeID string `json:"knowledge_id"` // FAQ Knowledge ID
|
||||
Status FAQImportTaskStatus `json:"status"` // Task status
|
||||
Progress int `json:"progress"` // 0-100 percentage
|
||||
Total int `json:"total"` // Total entries to import
|
||||
Processed int `json:"processed"` // Entries processed so far
|
||||
SuccessCount int `json:"success_count"` // 成功导入/验证通过的条目数
|
||||
FailedCount int `json:"failed_count"` // 失败的条目数
|
||||
SkippedCount int `json:"skipped_count,omitempty"` // 跳过的条目数(如重复等)
|
||||
FailedEntries []FAQFailedEntry `json:"failed_entries,omitempty"` // 失败条目详情(少量时直接返回)
|
||||
FailedEntriesURL string `json:"failed_entries_url,omitempty"` // 失败条目CSV下载URL(大量时返回URL)
|
||||
SuccessEntries []FAQSuccessEntry `json:"success_entries,omitempty"` // 成功条目简单信息(少量时直接返回)
|
||||
ValidEntryIndices []int `json:"valid_entry_indices,omitempty"` // 验证通过的条目索引(用于重试时跳过验证)
|
||||
Message string `json:"message"` // Status message
|
||||
Error string `json:"error"` // Error message if failed
|
||||
CreatedAt int64 `json:"created_at"` // Task creation timestamp
|
||||
UpdatedAt int64 `json:"updated_at"` // Last update timestamp
|
||||
DryRun bool `json:"dry_run,omitempty"` // 是否为 dry run 模式
|
||||
|
||||
// Result fields (populated when Status == "completed")
|
||||
ImportMode string `json:"import_mode,omitempty"` // 导入模式:append 或 replace
|
||||
ImportedAt time.Time `json:"imported_at,omitempty"` // 导入完成时间
|
||||
DisplayStatus string `json:"display_status,omitempty"` // 显示状态:open 或 close
|
||||
ProcessingTime int64 `json:"processing_time,omitempty"` // 处理耗时(毫秒)
|
||||
}
|
||||
|
||||
// FAQImportMetadata 存储在Knowledge.Metadata中的FAQ导入任务信息
|
||||
@@ -291,6 +342,33 @@ type FAQImportMetadata struct {
|
||||
ImportProcessed int `json:"import_processed"`
|
||||
}
|
||||
|
||||
// FAQImportResult 存储FAQ导入完成后的统计结果
|
||||
// 这个信息是持久化的,不跟随进度状态,直到下次导入时被替换
|
||||
type FAQImportResult struct {
|
||||
// 导入统计信息
|
||||
TotalEntries int `json:"total_entries"` // 总条目数
|
||||
SuccessCount int `json:"success_count"` // 成功导入的条目数
|
||||
FailedCount int `json:"failed_count"` // 失败的条目数
|
||||
SkippedCount int `json:"skipped_count"` // 跳过的条目数(如重复等)
|
||||
|
||||
// 导入模式和时间信息
|
||||
ImportMode string `json:"import_mode"` // 导入模式:append 或 replace
|
||||
ImportedAt time.Time `json:"imported_at"` // 导入完成时间
|
||||
TaskID string `json:"task_id"` // 导入任务ID
|
||||
|
||||
// 失败详情URL(失败条目较多时提供下载链接)
|
||||
FailedEntriesURL string `json:"failed_entries_url,omitempty"` // 失败条目CSV下载URL
|
||||
|
||||
// 成功条目简单信息
|
||||
SuccessEntries []FAQSuccessEntry `json:"success_entries,omitempty"` // 成功导入的条目简单信息
|
||||
|
||||
// 显示控制
|
||||
DisplayStatus string `json:"display_status"` // 显示状态:open 或 close
|
||||
|
||||
// 额外统计信息
|
||||
ProcessingTime int64 `json:"processing_time"` // 处理耗时(毫秒)
|
||||
}
|
||||
|
||||
// ToJSON converts the metadata to JSON type.
|
||||
func (m *FAQImportMetadata) ToJSON() (JSON, error) {
|
||||
if m == nil {
|
||||
@@ -303,6 +381,18 @@ func (m *FAQImportMetadata) ToJSON() (JSON, error) {
|
||||
return JSON(bytes), nil
|
||||
}
|
||||
|
||||
// ToJSON converts the import result to JSON type.
|
||||
func (r *FAQImportResult) ToJSON() (JSON, error) {
|
||||
if r == nil {
|
||||
return nil, nil
|
||||
}
|
||||
bytes, err := json.Marshal(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return JSON(bytes), nil
|
||||
}
|
||||
|
||||
// ParseFAQImportMetadata parses FAQ import metadata from Knowledge.
|
||||
func ParseFAQImportMetadata(k *Knowledge) (*FAQImportMetadata, error) {
|
||||
if k == nil || len(k.Metadata) == 0 {
|
||||
|
||||
@@ -12,8 +12,12 @@ type ChunkRepository interface {
|
||||
CreateChunks(ctx context.Context, chunks []*types.Chunk) error
|
||||
// GetChunkByID gets a chunk by id
|
||||
GetChunkByID(ctx context.Context, tenantID uint64, id string) (*types.Chunk, error)
|
||||
// GetChunkBySeqID gets a chunk by seq_id
|
||||
GetChunkBySeqID(ctx context.Context, tenantID uint64, seqID int64) (*types.Chunk, error)
|
||||
// ListChunksByID lists chunks by ids
|
||||
ListChunksByID(ctx context.Context, tenantID uint64, ids []string) ([]*types.Chunk, error)
|
||||
// ListChunksBySeqID lists chunks by seq_ids
|
||||
ListChunksBySeqID(ctx context.Context, tenantID uint64, seqIDs []int64) ([]*types.Chunk, error)
|
||||
// ListChunksByKnowledgeID lists chunks by knowledge id
|
||||
ListChunksByKnowledgeID(ctx context.Context, tenantID uint64, knowledgeID string) ([]*types.Chunk, error)
|
||||
// ListPagedChunksByKnowledgeID lists paged chunks by knowledge id.
|
||||
|
||||
@@ -11,8 +11,13 @@ import (
|
||||
type FileService interface {
|
||||
// SaveFile saves a file.
|
||||
SaveFile(ctx context.Context, file *multipart.FileHeader, tenantID uint64, knowledgeID string) (string, error)
|
||||
// SaveBytes saves bytes data to a file and returns the file path.
|
||||
// If temp is true, the file will be saved to a temporary storage that may auto-expire.
|
||||
SaveBytes(ctx context.Context, data []byte, tenantID uint64, fileName string, temp bool) (string, error)
|
||||
// GetFile retrieves a file.
|
||||
GetFile(ctx context.Context, filePath string) (io.ReadCloser, error)
|
||||
// GetFileURL returns a download URL for the file (if supported by the storage backend).
|
||||
GetFileURL(ctx context.Context, filePath string) (string, error)
|
||||
// DeleteFile deletes a file.
|
||||
DeleteFile(ctx context.Context, filePath string) error
|
||||
}
|
||||
|
||||
@@ -62,6 +62,8 @@ type KnowledgeService interface {
|
||||
) (*types.PageResult, error)
|
||||
// DeleteKnowledge deletes knowledge by ID.
|
||||
DeleteKnowledge(ctx context.Context, id string) error
|
||||
// DeleteKnowledgeList deletes multiple knowledge entries by IDs.
|
||||
DeleteKnowledgeList(ctx context.Context, ids []string) error
|
||||
// GetKnowledgeFile retrieves the file associated with the knowledge.
|
||||
GetKnowledgeFile(ctx context.Context, id string) (io.ReadCloser, string, error)
|
||||
// UpdateKnowledge updates knowledge information.
|
||||
@@ -77,32 +79,35 @@ type KnowledgeService interface {
|
||||
// UpdateImageInfo updates image information for a knowledge chunk.
|
||||
UpdateImageInfo(ctx context.Context, knowledgeID string, chunkID string, imageInfo string) error
|
||||
// ListFAQEntries lists FAQ entries under a FAQ knowledge base.
|
||||
// When tagID is non-empty, results are filtered by tag_id on FAQ chunks.
|
||||
// When tagSeqID is non-zero, results are filtered by tag seq_id on FAQ chunks.
|
||||
// searchField: specifies which field to search in ("standard_question", "similar_questions", "answers", "" for all)
|
||||
// sortOrder: "asc" for time ascending (updated_at ASC), default is time descending (updated_at DESC)
|
||||
ListFAQEntries(
|
||||
ctx context.Context,
|
||||
kbID string,
|
||||
page *types.Pagination,
|
||||
tagID string,
|
||||
tagSeqID int64,
|
||||
keyword string,
|
||||
searchField string,
|
||||
sortOrder string,
|
||||
) (*types.PageResult, error)
|
||||
// UpsertFAQEntries imports or appends FAQ entries asynchronously.
|
||||
// When DryRun is true, only validates entries without actually importing.
|
||||
// Returns task ID (Knowledge ID) for tracking import progress.
|
||||
UpsertFAQEntries(ctx context.Context, kbID string, payload *types.FAQBatchUpsertPayload) (string, error)
|
||||
// CreateFAQEntry creates a single FAQ entry synchronously.
|
||||
CreateFAQEntry(ctx context.Context, kbID string, payload *types.FAQEntryPayload) (*types.FAQEntry, error)
|
||||
// GetFAQEntry retrieves a single FAQ entry by ID.
|
||||
GetFAQEntry(ctx context.Context, kbID string, entryID string) (*types.FAQEntry, error)
|
||||
// GetFAQEntry retrieves a single FAQ entry by seq_id.
|
||||
GetFAQEntry(ctx context.Context, kbID string, entrySeqID int64) (*types.FAQEntry, error)
|
||||
// UpdateFAQEntry updates a single FAQ entry.
|
||||
UpdateFAQEntry(ctx context.Context, kbID string, entryID string, payload *types.FAQEntryPayload) error
|
||||
UpdateFAQEntry(ctx context.Context, kbID string, entrySeqID int64, payload *types.FAQEntryPayload) (*types.FAQEntry, error)
|
||||
// AddSimilarQuestions adds similar questions to a FAQ entry.
|
||||
AddSimilarQuestions(ctx context.Context, kbID string, entrySeqID int64, questions []string) (*types.FAQEntry, error)
|
||||
// UpdateFAQEntryFieldsBatch updates multiple fields for FAQ entries in batch.
|
||||
// Supports updating is_enabled, is_recommended, tag_id, and other fields in a single call.
|
||||
UpdateFAQEntryFieldsBatch(ctx context.Context, kbID string, req *types.FAQEntryFieldsBatchUpdate) error
|
||||
// DeleteFAQEntries deletes FAQ entries in batch.
|
||||
DeleteFAQEntries(ctx context.Context, kbID string, entryIDs []string) error
|
||||
// DeleteFAQEntries deletes FAQ entries in batch by seq_id.
|
||||
DeleteFAQEntries(ctx context.Context, kbID string, entrySeqIDs []int64) error
|
||||
// SearchFAQEntries searches FAQ entries using hybrid search.
|
||||
SearchFAQEntries(ctx context.Context, kbID string, req *types.FAQSearchRequest) ([]*types.FAQEntry, error)
|
||||
// ExportFAQEntries exports all FAQ entries for a knowledge base as CSV data.
|
||||
@@ -110,7 +115,8 @@ type KnowledgeService interface {
|
||||
// UpdateKnowledgeTagBatch updates tag for document knowledge items in batch.
|
||||
UpdateKnowledgeTagBatch(ctx context.Context, updates map[string]*string) error
|
||||
// UpdateFAQEntryTagBatch updates tag for FAQ entries in batch.
|
||||
UpdateFAQEntryTagBatch(ctx context.Context, kbID string, updates map[string]*string) error
|
||||
// Key: entry seq_id, Value: tag seq_id (nil to remove tag)
|
||||
UpdateFAQEntryTagBatch(ctx context.Context, kbID string, updates map[int64]*int64) error
|
||||
// GetRepository gets the knowledge repository
|
||||
GetRepository() KnowledgeRepository
|
||||
// ProcessDocument handles Asynq document processing tasks
|
||||
@@ -123,12 +129,16 @@ type KnowledgeService interface {
|
||||
ProcessSummaryGeneration(ctx context.Context, t *asynq.Task) error
|
||||
// ProcessKBClone handles Asynq knowledge base clone tasks
|
||||
ProcessKBClone(ctx context.Context, t *asynq.Task) error
|
||||
// ProcessKnowledgeListDelete handles Asynq knowledge list delete tasks
|
||||
ProcessKnowledgeListDelete(ctx context.Context, t *asynq.Task) error
|
||||
// GetKBCloneProgress retrieves the progress of a knowledge base clone task
|
||||
GetKBCloneProgress(ctx context.Context, taskID string) (*types.KBCloneProgress, error)
|
||||
// SaveKBCloneProgress saves the progress of a knowledge base clone task
|
||||
SaveKBCloneProgress(ctx context.Context, progress *types.KBCloneProgress) error
|
||||
// GetFAQImportProgress retrieves the progress of an FAQ import task
|
||||
GetFAQImportProgress(ctx context.Context, taskID string) (*types.FAQImportProgress, error)
|
||||
// UpdateLastFAQImportResultDisplayStatus updates the display status of FAQ import result
|
||||
UpdateLastFAQImportResultDisplayStatus(ctx context.Context, kbID string, displayStatus string) error
|
||||
// SearchKnowledge searches knowledge items by keyword across the tenant.
|
||||
// fileTypes: optional list of file extensions to filter by (e.g., ["csv", "xlsx"])
|
||||
SearchKnowledge(ctx context.Context, keyword string, offset, limit int, fileTypes []string) ([]*types.Knowledge, bool, error)
|
||||
@@ -172,4 +182,6 @@ type KnowledgeRepository interface {
|
||||
// SearchKnowledge searches knowledge items by keyword across the tenant.
|
||||
// fileTypes: optional list of file extensions to filter by (e.g., ["csv", "xlsx"])
|
||||
SearchKnowledge(ctx context.Context, tenantID uint64, keyword string, offset, limit int, fileTypes []string) ([]*types.Knowledge, bool, error)
|
||||
// ListIDsByTagID returns all knowledge IDs that have the specified tag ID.
|
||||
ListIDsByTagID(ctx context.Context, tenantID uint64, kbID, tagID string) ([]string, error)
|
||||
}
|
||||
|
||||
@@ -30,8 +30,12 @@ type KnowledgeTagRepository interface {
|
||||
Create(ctx context.Context, tag *types.KnowledgeTag) error
|
||||
Update(ctx context.Context, tag *types.KnowledgeTag) error
|
||||
GetByID(ctx context.Context, tenantID uint64, id string) (*types.KnowledgeTag, error)
|
||||
// GetBySeqID retrieves a tag by its seq_id.
|
||||
GetBySeqID(ctx context.Context, tenantID uint64, seqID int64) (*types.KnowledgeTag, error)
|
||||
// GetByIDs retrieves multiple tags by their IDs in a single query.
|
||||
GetByIDs(ctx context.Context, tenantID uint64, ids []string) ([]*types.KnowledgeTag, error)
|
||||
// GetBySeqIDs retrieves multiple tags by their seq_ids in a single query.
|
||||
GetBySeqIDs(ctx context.Context, tenantID uint64, seqIDs []int64) ([]*types.KnowledgeTag, error)
|
||||
GetByName(ctx context.Context, tenantID uint64, kbID string, name string) (*types.KnowledgeTag, error)
|
||||
ListByKB(
|
||||
ctx context.Context,
|
||||
@@ -48,12 +52,6 @@ type KnowledgeTagRepository interface {
|
||||
kbID string,
|
||||
tagID string,
|
||||
) (knowledgeCount int64, chunkCount int64, err error)
|
||||
// CountUntaggedReferences returns number of knowledges and chunks without a tag.
|
||||
CountUntaggedReferences(
|
||||
ctx context.Context,
|
||||
tenantID uint64,
|
||||
kbID string,
|
||||
) (knowledgeCount int64, chunkCount int64, err error)
|
||||
// DeleteUnusedTags deletes tags that are not referenced by any knowledge or chunk.
|
||||
DeleteUnusedTags(ctx context.Context, tenantID uint64, kbID string) (int64, error)
|
||||
}
|
||||
|
||||
@@ -93,6 +93,8 @@ type Knowledge struct {
|
||||
StorageSize int64 `json:"storage_size"`
|
||||
// Metadata of the knowledge
|
||||
Metadata JSON `json:"metadata" gorm:"type:json"`
|
||||
// Last FAQ import result (for FAQ type knowledge only)
|
||||
LastFAQImportResult JSON `json:"last_faq_import_result" gorm:"type:json"`
|
||||
// Creation time of the knowledge
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
// Last updated time of the knowledge
|
||||
@@ -215,6 +217,32 @@ func (k *Knowledge) SetManualMetadata(meta *ManualKnowledgeMetadata) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetLastFAQImportResult sets FAQ import result to the dedicated field.
|
||||
func (k *Knowledge) SetLastFAQImportResult(result *FAQImportResult) error {
|
||||
if result == nil {
|
||||
k.LastFAQImportResult = nil
|
||||
return nil
|
||||
}
|
||||
jsonValue, err := result.ToJSON()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
k.LastFAQImportResult = jsonValue
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetLastFAQImportResult parses and returns FAQ import result from the dedicated field.
|
||||
func (k *Knowledge) GetLastFAQImportResult() (*FAQImportResult, error) {
|
||||
if len(k.LastFAQImportResult) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
var result FAQImportResult
|
||||
if err := json.Unmarshal(k.LastFAQImportResult, &result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
// IsManual returns true if the knowledge item is manual Markdown knowledge.
|
||||
func (k *Knowledge) IsManual() bool {
|
||||
return k != nil && k.Type == KnowledgeTypeManual
|
||||
|
||||
@@ -99,6 +99,7 @@ type SearchParams struct {
|
||||
DisableVectorMatch bool `json:"disable_vector_match"`
|
||||
KnowledgeIDs []string `json:"knowledge_ids"`
|
||||
TagIDs []string `json:"tag_ids"` // Tag IDs for filtering (used for FAQ priority filtering)
|
||||
OnlyRecommended bool `json:"only_recommended"`
|
||||
}
|
||||
|
||||
// Value implements the driver.Valuer interface, used to convert SearchResult to database value
|
||||
|
||||
@@ -8,6 +8,8 @@ import "time"
|
||||
type KnowledgeTag struct {
|
||||
// Unique identifier of the tag (UUID)
|
||||
ID string `json:"id" gorm:"type:varchar(36);primaryKey"`
|
||||
// SeqID is an auto-increment integer ID for external API usage
|
||||
SeqID int64 `json:"seq_id" gorm:"type:bigint;uniqueIndex;autoIncrement"`
|
||||
// Tenant ID
|
||||
TenantID uint64 `json:"tenant_id"`
|
||||
// Knowledge base ID that this tag belongs to
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
// CleanupStaleRunningTasks 清理可能残留的running task keys
|
||||
// 这是一个调试和维护工具,可以用来清理因异常情况导致的残留running keys
|
||||
func CleanupStaleRunningTasks(ctx context.Context, redisClient *redis.Client, keyPrefix string, maxAge time.Duration) (int, error) {
|
||||
// 获取所有匹配的keys
|
||||
keys, err := redisClient.Keys(ctx, keyPrefix+"*").Result()
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("failed to get keys: %w", err)
|
||||
}
|
||||
|
||||
if len(keys) == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
// 检查每个key的TTL
|
||||
var staleTasks []string
|
||||
for _, key := range keys {
|
||||
ttl, err := redisClient.TTL(ctx, key).Result()
|
||||
if err != nil {
|
||||
continue // 跳过错误的key
|
||||
}
|
||||
|
||||
// 如果TTL小于0(永不过期)或者剩余时间太长(可能是残留的),标记为stale
|
||||
if ttl < 0 || ttl > maxAge {
|
||||
staleTasks = append(staleTasks, key)
|
||||
}
|
||||
}
|
||||
|
||||
if len(staleTasks) == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
// 删除stale keys
|
||||
deleted, err := redisClient.Del(ctx, staleTasks...).Result()
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("failed to delete stale keys: %w", err)
|
||||
}
|
||||
|
||||
return int(deleted), nil
|
||||
}
|
||||
|
||||
// CheckRunningTaskStatus 检查指定running task的状态
|
||||
func CheckRunningTaskStatus(ctx context.Context, redisClient *redis.Client, runningKey, progressKey string) (map[string]interface{}, error) {
|
||||
result := make(map[string]interface{})
|
||||
|
||||
// 检查running key
|
||||
runningTaskID, err := redisClient.Get(ctx, runningKey).Result()
|
||||
if err != nil {
|
||||
if err == redis.Nil {
|
||||
result["running_task_exists"] = false
|
||||
} else {
|
||||
return nil, fmt.Errorf("failed to get running task: %w", err)
|
||||
}
|
||||
} else {
|
||||
result["running_task_exists"] = true
|
||||
result["running_task_id"] = runningTaskID
|
||||
|
||||
// 获取running key的TTL
|
||||
ttl, _ := redisClient.TTL(ctx, runningKey).Result()
|
||||
result["running_task_ttl"] = ttl.String()
|
||||
}
|
||||
|
||||
// 检查progress key
|
||||
progressData, err := redisClient.Get(ctx, progressKey).Result()
|
||||
if err != nil {
|
||||
if err == redis.Nil {
|
||||
result["progress_exists"] = false
|
||||
} else {
|
||||
return nil, fmt.Errorf("failed to get progress: %w", err)
|
||||
}
|
||||
} else {
|
||||
result["progress_exists"] = true
|
||||
result["progress_data"] = progressData
|
||||
|
||||
// 获取progress key的TTL
|
||||
ttl, _ := redisClient.TTL(ctx, progressKey).Result()
|
||||
result["progress_ttl"] = ttl.String()
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// GenerateTaskID generates a unique task ID with multiple collision-resistant elements.
|
||||
// The format is: <taskType>_<tenantID>_<timestamp>_<uuid>_<businessID>
|
||||
//
|
||||
// Parameters:
|
||||
// - taskType: Type of task (e.g., "faq_import", "kb_clone")
|
||||
// - tenantID: Tenant ID for multi-tenancy isolation
|
||||
// - businessID: Optional business-specific ID (e.g., knowledge base ID)
|
||||
//
|
||||
// Returns a task ID like: "faq_import_12345_1704628851692_a1b2c3d4_kb789"
|
||||
func GenerateTaskID(taskType string, tenantID uint64, businessID ...string) string {
|
||||
// Use current timestamp in milliseconds for temporal uniqueness
|
||||
timestamp := time.Now().UnixMilli()
|
||||
|
||||
// Generate a short UUID (first 8 characters for brevity)
|
||||
shortUUID := strings.ReplaceAll(uuid.New().String()[:8], "-", "")
|
||||
|
||||
// Build the task ID components
|
||||
components := []string{
|
||||
sanitizeTaskType(taskType),
|
||||
strconv.FormatUint(tenantID, 10),
|
||||
strconv.FormatInt(timestamp, 10),
|
||||
shortUUID,
|
||||
}
|
||||
|
||||
// Add business ID if provided
|
||||
if len(businessID) > 0 && businessID[0] != "" {
|
||||
components = append(components, sanitizeBusinessID(businessID[0]))
|
||||
}
|
||||
|
||||
return strings.Join(components, "_")
|
||||
}
|
||||
|
||||
// GenerateTaskIDWithPrefix generates a task ID with a custom prefix.
|
||||
// This is useful when you want more control over the task ID format.
|
||||
func GenerateTaskIDWithPrefix(prefix string, tenantID uint64, businessID ...string) string {
|
||||
timestamp := time.Now().UnixMilli()
|
||||
shortUUID := strings.ReplaceAll(uuid.New().String()[:8], "-", "")
|
||||
|
||||
components := []string{
|
||||
sanitizeTaskType(prefix),
|
||||
strconv.FormatUint(tenantID, 10),
|
||||
strconv.FormatInt(timestamp, 10),
|
||||
shortUUID,
|
||||
}
|
||||
|
||||
if len(businessID) > 0 && businessID[0] != "" {
|
||||
components = append(components, sanitizeBusinessID(businessID[0]))
|
||||
}
|
||||
|
||||
return strings.Join(components, "_")
|
||||
}
|
||||
|
||||
// ParseTaskID parses a task ID generated by GenerateTaskID and returns its components.
|
||||
// Returns taskType, tenantID, timestamp, uuid, businessID, and error.
|
||||
func ParseTaskID(taskID string) (taskType string, tenantID uint64, timestamp int64, uuidPart string, businessID string, err error) {
|
||||
parts := strings.Split(taskID, "_")
|
||||
if len(parts) < 4 {
|
||||
err = fmt.Errorf("invalid task ID format: %s", taskID)
|
||||
return
|
||||
}
|
||||
|
||||
taskType = parts[0]
|
||||
|
||||
tenantID, err = strconv.ParseUint(parts[1], 10, 64)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("invalid tenant ID in task ID: %s", parts[1])
|
||||
return
|
||||
}
|
||||
|
||||
timestamp, err = strconv.ParseInt(parts[2], 10, 64)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("invalid timestamp in task ID: %s", parts[2])
|
||||
return
|
||||
}
|
||||
|
||||
uuidPart = parts[3]
|
||||
|
||||
if len(parts) > 4 {
|
||||
businessID = parts[4]
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// sanitizeTaskType ensures task type is safe for use in task ID
|
||||
func sanitizeTaskType(taskType string) string {
|
||||
// Replace colons and other special characters with underscores
|
||||
taskType = strings.ReplaceAll(taskType, ":", "_")
|
||||
taskType = strings.ReplaceAll(taskType, "-", "_")
|
||||
taskType = strings.ReplaceAll(taskType, " ", "_")
|
||||
return strings.ToLower(taskType)
|
||||
}
|
||||
|
||||
// sanitizeBusinessID ensures business ID is safe for use in task ID
|
||||
func sanitizeBusinessID(businessID string) string {
|
||||
// Take first 12 characters and replace special characters
|
||||
if len(businessID) > 12 {
|
||||
businessID = businessID[:12]
|
||||
}
|
||||
businessID = strings.ReplaceAll(businessID, "-", "")
|
||||
businessID = strings.ReplaceAll(businessID, "_", "")
|
||||
businessID = strings.ReplaceAll(businessID, ":", "")
|
||||
return businessID
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
-- Rollback: This migration cannot be fully rolled back as we don't know which entries
|
||||
-- were originally untagged. We can only clear the tag_id for entries that reference
|
||||
-- a "未分类" tag, but this may affect entries that were intentionally tagged.
|
||||
|
||||
-- WARNING: This rollback is destructive and should only be used if absolutely necessary.
|
||||
-- It will set tag_id to empty string for all chunks, knowledges, and embeddings that reference "未分类" tags.
|
||||
|
||||
DO $$
|
||||
DECLARE
|
||||
kb_record RECORD;
|
||||
untagged_tag_id VARCHAR(36);
|
||||
updated_chunks INT;
|
||||
updated_knowledges INT;
|
||||
BEGIN
|
||||
RAISE NOTICE '[Migration 000008 Rollback] WARNING: This rollback will clear tag_id for all entries referencing "未分类" tags';
|
||||
|
||||
-- Find all "未分类" tags
|
||||
FOR kb_record IN
|
||||
SELECT id, tenant_id, knowledge_base_id
|
||||
FROM knowledge_tags
|
||||
WHERE name = '未分类'
|
||||
LOOP
|
||||
untagged_tag_id := kb_record.id;
|
||||
|
||||
-- Clear tag_id for chunks referencing this tag (both faq and document types)
|
||||
UPDATE chunks
|
||||
SET tag_id = '', updated_at = NOW()
|
||||
WHERE tag_id = untagged_tag_id
|
||||
AND chunk_type IN ('faq', 'document');
|
||||
|
||||
GET DIAGNOSTICS updated_chunks = ROW_COUNT;
|
||||
RAISE NOTICE '[Migration 000008 Rollback] Cleared tag_id for % chunks referencing tag %',
|
||||
updated_chunks, untagged_tag_id;
|
||||
|
||||
-- Clear tag_id for knowledges referencing this tag
|
||||
UPDATE knowledges
|
||||
SET tag_id = '', updated_at = NOW()
|
||||
WHERE tag_id = untagged_tag_id;
|
||||
|
||||
GET DIAGNOSTICS updated_knowledges = ROW_COUNT;
|
||||
RAISE NOTICE '[Migration 000008 Rollback] Cleared tag_id for % knowledges referencing tag %',
|
||||
updated_knowledges, untagged_tag_id;
|
||||
|
||||
-- Clear tag_id in embeddings if column exists
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM information_schema.columns
|
||||
WHERE table_name = 'embeddings' AND column_name = 'tag_id'
|
||||
) THEN
|
||||
UPDATE embeddings
|
||||
SET tag_id = ''
|
||||
WHERE tag_id = untagged_tag_id;
|
||||
END IF;
|
||||
|
||||
-- Delete the "未分类" tag
|
||||
DELETE FROM knowledge_tags WHERE id = untagged_tag_id;
|
||||
RAISE NOTICE '[Migration 000008 Rollback] Deleted "未分类" tag: %', untagged_tag_id;
|
||||
END LOOP;
|
||||
|
||||
RAISE NOTICE '[Migration 000008 Rollback] Completed rollback';
|
||||
END $$;
|
||||
@@ -0,0 +1,96 @@
|
||||
-- Migration: Create "未分类" tag for each knowledge base that has untagged entries
|
||||
-- and update chunks, knowledges, and embeddings to reference the new tag
|
||||
|
||||
DO $$
|
||||
DECLARE
|
||||
kb_record RECORD;
|
||||
new_tag_id VARCHAR(36);
|
||||
updated_chunks INT;
|
||||
updated_knowledges INT;
|
||||
updated_embeddings INT;
|
||||
BEGIN
|
||||
-- Find all knowledge bases that have untagged chunks or knowledges
|
||||
FOR kb_record IN
|
||||
SELECT DISTINCT tenant_id, knowledge_base_id
|
||||
FROM (
|
||||
-- Untagged chunks (FAQ or document)
|
||||
SELECT c.tenant_id, c.knowledge_base_id
|
||||
FROM chunks c
|
||||
WHERE c.chunk_type IN ('faq', 'document')
|
||||
AND (c.tag_id = '' OR c.tag_id IS NULL)
|
||||
UNION
|
||||
-- Untagged knowledges (documents)
|
||||
SELECT k.tenant_id, k.knowledge_base_id
|
||||
FROM knowledges k
|
||||
WHERE k.deleted_at IS NULL
|
||||
AND (k.tag_id = '' OR k.tag_id IS NULL)
|
||||
) AS untagged
|
||||
LOOP
|
||||
-- Check if "未分类" tag already exists for this knowledge base
|
||||
SELECT id INTO new_tag_id
|
||||
FROM knowledge_tags
|
||||
WHERE tenant_id = kb_record.tenant_id
|
||||
AND knowledge_base_id = kb_record.knowledge_base_id
|
||||
AND name = '未分类'
|
||||
LIMIT 1;
|
||||
|
||||
-- If not exists, create the tag
|
||||
IF new_tag_id IS NULL THEN
|
||||
new_tag_id := gen_random_uuid()::VARCHAR(36);
|
||||
INSERT INTO knowledge_tags (id, tenant_id, knowledge_base_id, name, color, sort_order, created_at, updated_at)
|
||||
VALUES (new_tag_id, kb_record.tenant_id, kb_record.knowledge_base_id, '未分类', '', 0, NOW(), NOW());
|
||||
RAISE NOTICE '[Migration 000008] Created "未分类" tag (id: %) for tenant_id: %, kb_id: %',
|
||||
new_tag_id, kb_record.tenant_id, kb_record.knowledge_base_id;
|
||||
ELSE
|
||||
RAISE NOTICE '[Migration 000008] "未分类" tag already exists (id: %) for tenant_id: %, kb_id: %',
|
||||
new_tag_id, kb_record.tenant_id, kb_record.knowledge_base_id;
|
||||
END IF;
|
||||
|
||||
-- Update chunks with empty tag_id (both faq and document types)
|
||||
UPDATE chunks
|
||||
SET tag_id = new_tag_id, updated_at = NOW()
|
||||
WHERE tenant_id = kb_record.tenant_id
|
||||
AND knowledge_base_id = kb_record.knowledge_base_id
|
||||
AND chunk_type IN ('faq', 'document')
|
||||
AND (tag_id = '' OR tag_id IS NULL);
|
||||
|
||||
GET DIAGNOSTICS updated_chunks = ROW_COUNT;
|
||||
RAISE NOTICE '[Migration 000008] Updated % chunks for tenant_id: %, kb_id: %',
|
||||
updated_chunks, kb_record.tenant_id, kb_record.knowledge_base_id;
|
||||
|
||||
-- Update knowledges with empty tag_id (documents)
|
||||
UPDATE knowledges
|
||||
SET tag_id = new_tag_id, updated_at = NOW()
|
||||
WHERE tenant_id = kb_record.tenant_id
|
||||
AND knowledge_base_id = kb_record.knowledge_base_id
|
||||
AND deleted_at IS NULL
|
||||
AND (tag_id = '' OR tag_id IS NULL);
|
||||
|
||||
GET DIAGNOSTICS updated_knowledges = ROW_COUNT;
|
||||
RAISE NOTICE '[Migration 000008] Updated % knowledges for tenant_id: %, kb_id: %',
|
||||
updated_knowledges, kb_record.tenant_id, kb_record.knowledge_base_id;
|
||||
|
||||
-- Update embeddings with empty tag_id (if embeddings table exists and has tag_id column)
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM information_schema.columns
|
||||
WHERE table_name = 'embeddings' AND column_name = 'tag_id'
|
||||
) THEN
|
||||
UPDATE embeddings
|
||||
SET tag_id = new_tag_id
|
||||
WHERE knowledge_base_id = kb_record.knowledge_base_id
|
||||
AND (tag_id = '' OR tag_id IS NULL)
|
||||
AND chunk_id IN (
|
||||
SELECT id FROM chunks
|
||||
WHERE tenant_id = kb_record.tenant_id
|
||||
AND knowledge_base_id = kb_record.knowledge_base_id
|
||||
AND chunk_type IN ('faq', 'document')
|
||||
);
|
||||
|
||||
GET DIAGNOSTICS updated_embeddings = ROW_COUNT;
|
||||
RAISE NOTICE '[Migration 000008] Updated % embeddings for kb_id: %',
|
||||
updated_embeddings, kb_record.knowledge_base_id;
|
||||
END IF;
|
||||
END LOOP;
|
||||
|
||||
RAISE NOTICE '[Migration 000008] Completed migration of untagged entries';
|
||||
END $$;
|
||||
@@ -0,0 +1,3 @@
|
||||
-- Remove last_faq_import_result column from knowledge table
|
||||
|
||||
ALTER TABLE knowledges DROP COLUMN IF EXISTS last_faq_import_result;
|
||||
@@ -0,0 +1,4 @@
|
||||
-- Add last_faq_import_result column to knowledge table
|
||||
-- This field stores the latest FAQ import result for FAQ type knowledge
|
||||
|
||||
ALTER TABLE knowledges ADD COLUMN IF NOT EXISTS last_faq_import_result JSON DEFAULT NULL;
|
||||
@@ -0,0 +1,11 @@
|
||||
-- Migration 000010 Down: Remove seq_id from chunks and knowledge_tags tables
|
||||
|
||||
-- Remove seq_id from chunks
|
||||
DROP INDEX IF EXISTS idx_chunks_seq_id;
|
||||
ALTER TABLE chunks DROP COLUMN IF EXISTS seq_id;
|
||||
DROP SEQUENCE IF EXISTS chunks_seq_id_seq;
|
||||
|
||||
-- Remove seq_id from knowledge_tags
|
||||
DROP INDEX IF EXISTS idx_knowledge_tags_seq_id;
|
||||
ALTER TABLE knowledge_tags DROP COLUMN IF EXISTS seq_id;
|
||||
DROP SEQUENCE IF EXISTS knowledge_tags_seq_id_seq;
|
||||
@@ -0,0 +1,52 @@
|
||||
-- Migration 000010: Add seq_id (auto-increment integer ID) to chunks and knowledge_tags tables
|
||||
-- This provides integer IDs for FAQ entries and tags for external API usage
|
||||
|
||||
-- ============================================================================
|
||||
-- Section 1: Add seq_id to chunks table
|
||||
-- ============================================================================
|
||||
|
||||
DO $$ BEGIN RAISE NOTICE '[Migration 000010] Adding seq_id column to chunks table'; END $$;
|
||||
|
||||
-- Create sequence for chunks with starting value > 72528124
|
||||
CREATE SEQUENCE IF NOT EXISTS chunks_seq_id_seq START WITH 100000000;
|
||||
|
||||
-- Add seq_id column to chunks table
|
||||
ALTER TABLE chunks ADD COLUMN IF NOT EXISTS seq_id BIGINT;
|
||||
|
||||
-- Set default value using sequence
|
||||
ALTER TABLE chunks ALTER COLUMN seq_id SET DEFAULT nextval('chunks_seq_id_seq');
|
||||
|
||||
-- Populate existing rows with sequence values
|
||||
UPDATE chunks SET seq_id = nextval('chunks_seq_id_seq') WHERE seq_id IS NULL;
|
||||
|
||||
-- Make seq_id NOT NULL after populating
|
||||
ALTER TABLE chunks ALTER COLUMN seq_id SET NOT NULL;
|
||||
|
||||
-- Create unique index on seq_id
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_chunks_seq_id ON chunks(seq_id);
|
||||
|
||||
-- ============================================================================
|
||||
-- Section 2: Add seq_id to knowledge_tags table
|
||||
-- ============================================================================
|
||||
|
||||
DO $$ BEGIN RAISE NOTICE '[Migration 000010] Adding seq_id column to knowledge_tags table'; END $$;
|
||||
|
||||
-- Create sequence for knowledge_tags with starting value > 2924026
|
||||
CREATE SEQUENCE IF NOT EXISTS knowledge_tags_seq_id_seq START WITH 10000000;
|
||||
|
||||
-- Add seq_id column to knowledge_tags table
|
||||
ALTER TABLE knowledge_tags ADD COLUMN IF NOT EXISTS seq_id BIGINT;
|
||||
|
||||
-- Set default value using sequence
|
||||
ALTER TABLE knowledge_tags ALTER COLUMN seq_id SET DEFAULT nextval('knowledge_tags_seq_id_seq');
|
||||
|
||||
-- Populate existing rows with sequence values
|
||||
UPDATE knowledge_tags SET seq_id = nextval('knowledge_tags_seq_id_seq') WHERE seq_id IS NULL;
|
||||
|
||||
-- Make seq_id NOT NULL after populating
|
||||
ALTER TABLE knowledge_tags ALTER COLUMN seq_id SET NOT NULL;
|
||||
|
||||
-- Create unique index on seq_id
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_knowledge_tags_seq_id ON knowledge_tags(seq_id);
|
||||
|
||||
DO $$ BEGIN RAISE NOTICE '[Migration 000010] seq_id columns added successfully!'; END $$;
|
||||
Reference in New Issue
Block a user