feat: add knowledge management functionality with CRUD operations and database schema
- Implemented KnowledgeHandler for managing knowledge groups and items, including listing, creating, updating, and deleting operations. - Added database migration scripts to create necessary tables for knowledge management, including knowledge_groups, knowledge_items, knowledge_parse_tasks, and knowledge_chunks_meta. - Introduced prompt_rule_knowledge_groups table to associate prompt rules with knowledge groups.
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"github.com/geo-platform/tenant-api/internal/shared/auditlog"
|
||||
@@ -39,35 +40,43 @@ type PromptRuleGroupResponse struct {
|
||||
// --- Rule types ---
|
||||
|
||||
type PromptRuleRequest struct {
|
||||
GroupID *int64 `json:"group_id"`
|
||||
Name string `json:"name" binding:"required"`
|
||||
PromptContent string `json:"prompt_content" binding:"required"`
|
||||
Scene *string `json:"scene"`
|
||||
DefaultTone *string `json:"default_tone"`
|
||||
DefaultWordCount *int `json:"default_word_count"`
|
||||
GroupID *int64 `json:"group_id"`
|
||||
Name string `json:"name" binding:"required"`
|
||||
PromptContent string `json:"prompt_content" binding:"required"`
|
||||
Scene *string `json:"scene"`
|
||||
DefaultTone *string `json:"default_tone"`
|
||||
DefaultWordCount *int `json:"default_word_count"`
|
||||
KnowledgeGroupIDs []int64 `json:"knowledge_group_ids"`
|
||||
}
|
||||
|
||||
type PromptRuleKnowledgeGroup struct {
|
||||
ID int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type PromptRuleResponse struct {
|
||||
ID int64 `json:"id"`
|
||||
GroupID *int64 `json:"group_id"`
|
||||
Name string `json:"name"`
|
||||
PromptContent string `json:"prompt_content"`
|
||||
Scene *string `json:"scene"`
|
||||
DefaultTone *string `json:"default_tone"`
|
||||
DefaultWordCount *int `json:"default_word_count"`
|
||||
Status string `json:"status"`
|
||||
ArticleCount int `json:"article_count"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
ID int64 `json:"id"`
|
||||
GroupID *int64 `json:"group_id"`
|
||||
Name string `json:"name"`
|
||||
PromptContent string `json:"prompt_content"`
|
||||
Scene *string `json:"scene"`
|
||||
DefaultTone *string `json:"default_tone"`
|
||||
DefaultWordCount *int `json:"default_word_count"`
|
||||
KnowledgeGroupIDs []int64 `json:"knowledge_group_ids"`
|
||||
KnowledgeGroups []PromptRuleKnowledgeGroup `json:"knowledge_groups"`
|
||||
Status string `json:"status"`
|
||||
ArticleCount int `json:"article_count"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
}
|
||||
|
||||
type PromptRuleListParams struct {
|
||||
GroupID *int64 `json:"group_id"`
|
||||
Status *string `json:"status"`
|
||||
Keyword *string `json:"keyword"`
|
||||
Page int `json:"page"`
|
||||
PageSize int `json:"page_size"`
|
||||
Ungrouped bool `json:"ungrouped"`
|
||||
GroupID *int64 `json:"group_id"`
|
||||
Status *string `json:"status"`
|
||||
Keyword *string `json:"keyword"`
|
||||
Page int `json:"page"`
|
||||
PageSize int `json:"page_size"`
|
||||
Ungrouped bool `json:"ungrouped"`
|
||||
}
|
||||
|
||||
type PromptRuleListResponse struct {
|
||||
@@ -297,12 +306,23 @@ func (s *PromptRuleService) List(ctx context.Context, params PromptRuleListParam
|
||||
}
|
||||
r.CreatedAt = fmt.Sprintf("%v", ca)
|
||||
r.UpdatedAt = fmt.Sprintf("%v", ua)
|
||||
r.KnowledgeGroupIDs = []int64{}
|
||||
r.KnowledgeGroups = []PromptRuleKnowledgeGroup{}
|
||||
items = append(items, r)
|
||||
}
|
||||
if items == nil {
|
||||
items = []PromptRuleResponse{}
|
||||
}
|
||||
|
||||
groupMap, err := s.loadPromptRuleKnowledgeGroupMap(ctx, actor.TenantID, collectPromptRuleIDs(items))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for index := range items {
|
||||
items[index].KnowledgeGroups = groupMap[items[index].ID]
|
||||
items[index].KnowledgeGroupIDs = promptRuleKnowledgeGroupIDs(items[index].KnowledgeGroups)
|
||||
}
|
||||
|
||||
return &PromptRuleListResponse{Items: items, Total: total}, nil
|
||||
}
|
||||
|
||||
@@ -327,14 +347,32 @@ func (s *PromptRuleService) Detail(ctx context.Context, id int64) (*PromptRuleRe
|
||||
r.CreatedAt = fmt.Sprintf("%v", ca)
|
||||
r.UpdatedAt = fmt.Sprintf("%v", ua)
|
||||
r.ArticleCount = articleCount
|
||||
|
||||
groupMap, groupErr := s.loadPromptRuleKnowledgeGroupMap(ctx, actor.TenantID, []int64{id})
|
||||
if groupErr != nil {
|
||||
return nil, groupErr
|
||||
}
|
||||
r.KnowledgeGroups = groupMap[id]
|
||||
r.KnowledgeGroupIDs = promptRuleKnowledgeGroupIDs(r.KnowledgeGroups)
|
||||
return &r, nil
|
||||
}
|
||||
|
||||
func (s *PromptRuleService) Create(ctx context.Context, req PromptRuleRequest) (*PromptRuleResponse, error) {
|
||||
actor := auth.MustActor(ctx)
|
||||
knowledgeGroups, err := s.validatePromptRuleKnowledgeGroups(ctx, actor.TenantID, req.KnowledgeGroupIDs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tx, err := s.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return nil, response.ErrInternal(50010, "create_failed", "failed to create prompt rule")
|
||||
}
|
||||
defer func() { _ = tx.Rollback(ctx) }()
|
||||
|
||||
var id int64
|
||||
var ca interface{}
|
||||
err := s.pool.QueryRow(ctx, `
|
||||
err = tx.QueryRow(ctx, `
|
||||
INSERT INTO prompt_rules (tenant_id, group_id, name, prompt_content, scene, default_tone, default_word_count)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING id, created_at
|
||||
`, actor.TenantID, req.GroupID, req.Name, req.PromptContent, req.Scene, req.DefaultTone, req.DefaultWordCount).Scan(&id, &ca)
|
||||
@@ -342,6 +380,13 @@ func (s *PromptRuleService) Create(ctx context.Context, req PromptRuleRequest) (
|
||||
return nil, response.ErrInternal(50010, "create_failed", "failed to create prompt rule")
|
||||
}
|
||||
|
||||
if err := s.replacePromptRuleKnowledgeGroups(ctx, tx, id, promptRuleKnowledgeGroupIDs(knowledgeGroups)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return nil, response.ErrInternal(50010, "create_failed", "failed to commit prompt rule")
|
||||
}
|
||||
|
||||
afterJSON, _ := json.Marshal(map[string]interface{}{"id": id, "name": req.Name})
|
||||
result := "success"
|
||||
resourceType := "prompt_rule"
|
||||
@@ -359,15 +404,36 @@ func (s *PromptRuleService) Create(ctx context.Context, req PromptRuleRequest) (
|
||||
})
|
||||
|
||||
return &PromptRuleResponse{
|
||||
ID: id, GroupID: req.GroupID, Name: req.Name, PromptContent: req.PromptContent,
|
||||
Scene: req.Scene, DefaultTone: req.DefaultTone, DefaultWordCount: req.DefaultWordCount,
|
||||
Status: "enabled", ArticleCount: 0, CreatedAt: fmt.Sprintf("%v", ca),
|
||||
ID: id,
|
||||
GroupID: req.GroupID,
|
||||
Name: req.Name,
|
||||
PromptContent: req.PromptContent,
|
||||
Scene: req.Scene,
|
||||
DefaultTone: req.DefaultTone,
|
||||
DefaultWordCount: req.DefaultWordCount,
|
||||
KnowledgeGroupIDs: promptRuleKnowledgeGroupIDs(knowledgeGroups),
|
||||
KnowledgeGroups: knowledgeGroups,
|
||||
Status: "enabled",
|
||||
ArticleCount: 0,
|
||||
CreatedAt: fmt.Sprintf("%v", ca),
|
||||
UpdatedAt: fmt.Sprintf("%v", ca),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *PromptRuleService) Update(ctx context.Context, id int64, req PromptRuleRequest) error {
|
||||
actor := auth.MustActor(ctx)
|
||||
tag, err := s.pool.Exec(ctx, `
|
||||
knowledgeGroups, err := s.validatePromptRuleKnowledgeGroups(ctx, actor.TenantID, req.KnowledgeGroupIDs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
tx, err := s.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return response.ErrInternal(50010, "update_failed", "failed to update prompt rule")
|
||||
}
|
||||
defer func() { _ = tx.Rollback(ctx) }()
|
||||
|
||||
tag, err := tx.Exec(ctx, `
|
||||
UPDATE prompt_rules SET name = $1, group_id = $2, prompt_content = $3,
|
||||
scene = $4, default_tone = $5, default_word_count = $6, updated_at = NOW()
|
||||
WHERE id = $7 AND tenant_id = $8 AND deleted_at IS NULL
|
||||
@@ -375,6 +441,12 @@ func (s *PromptRuleService) Update(ctx context.Context, id int64, req PromptRule
|
||||
if err != nil || tag.RowsAffected() == 0 {
|
||||
return response.ErrNotFound(40431, "rule_not_found", "prompt rule not found")
|
||||
}
|
||||
if err := s.replacePromptRuleKnowledgeGroups(ctx, tx, id, promptRuleKnowledgeGroupIDs(knowledgeGroups)); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return response.ErrInternal(50010, "update_failed", "failed to commit prompt rule")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -443,3 +515,122 @@ func (s *PromptRuleService) ListSimple(ctx context.Context) ([]PromptRuleSimple,
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
func (s *PromptRuleService) validatePromptRuleKnowledgeGroups(
|
||||
ctx context.Context,
|
||||
tenantID int64,
|
||||
groupIDs []int64,
|
||||
) ([]PromptRuleKnowledgeGroup, error) {
|
||||
groupIDs = normalizeInt64IDs(groupIDs)
|
||||
if len(groupIDs) == 0 {
|
||||
return []PromptRuleKnowledgeGroup{}, nil
|
||||
}
|
||||
|
||||
rows, err := s.pool.Query(ctx, `
|
||||
SELECT id, name
|
||||
FROM knowledge_groups
|
||||
WHERE tenant_id = $1 AND id = ANY($2::bigint[]) AND deleted_at IS NULL
|
||||
ORDER BY sort_order, created_at, id
|
||||
`, tenantID, groupIDs)
|
||||
if err != nil {
|
||||
return nil, response.ErrInternal(50010, "knowledge_group_query_failed", "failed to query knowledge groups")
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
groups := make([]PromptRuleKnowledgeGroup, 0, len(groupIDs))
|
||||
found := make(map[int64]struct{}, len(groupIDs))
|
||||
for rows.Next() {
|
||||
var item PromptRuleKnowledgeGroup
|
||||
if err := rows.Scan(&item.ID, &item.Name); err != nil {
|
||||
return nil, response.ErrInternal(50010, "knowledge_group_query_failed", err.Error())
|
||||
}
|
||||
groups = append(groups, item)
|
||||
found[item.ID] = struct{}{}
|
||||
}
|
||||
|
||||
if len(found) != len(groupIDs) {
|
||||
return nil, response.ErrNotFound(40451, "knowledge_group_not_found", "knowledge group not found")
|
||||
}
|
||||
|
||||
return groups, nil
|
||||
}
|
||||
|
||||
func (s *PromptRuleService) loadPromptRuleKnowledgeGroupMap(
|
||||
ctx context.Context,
|
||||
tenantID int64,
|
||||
ruleIDs []int64,
|
||||
) (map[int64][]PromptRuleKnowledgeGroup, error) {
|
||||
result := make(map[int64][]PromptRuleKnowledgeGroup, len(ruleIDs))
|
||||
ruleIDs = normalizeInt64IDs(ruleIDs)
|
||||
if len(ruleIDs) == 0 {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
rows, err := s.pool.Query(ctx, `
|
||||
SELECT prkg.prompt_rule_id, kg.id, kg.name
|
||||
FROM prompt_rule_knowledge_groups prkg
|
||||
JOIN knowledge_groups kg ON kg.id = prkg.knowledge_group_id
|
||||
WHERE prkg.prompt_rule_id = ANY($1::bigint[])
|
||||
AND kg.tenant_id = $2
|
||||
AND kg.deleted_at IS NULL
|
||||
ORDER BY kg.sort_order, kg.created_at, kg.id
|
||||
`, ruleIDs, tenantID)
|
||||
if err != nil {
|
||||
return nil, response.ErrInternal(50010, "knowledge_group_query_failed", "failed to query prompt rule knowledge groups")
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var (
|
||||
ruleID int64
|
||||
group PromptRuleKnowledgeGroup
|
||||
)
|
||||
if err := rows.Scan(&ruleID, &group.ID, &group.Name); err != nil {
|
||||
return nil, response.ErrInternal(50010, "knowledge_group_query_failed", err.Error())
|
||||
}
|
||||
result[ruleID] = append(result[ruleID], group)
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *PromptRuleService) replacePromptRuleKnowledgeGroups(
|
||||
ctx context.Context,
|
||||
tx pgx.Tx,
|
||||
promptRuleID int64,
|
||||
groupIDs []int64,
|
||||
) error {
|
||||
if _, err := tx.Exec(ctx, `
|
||||
DELETE FROM prompt_rule_knowledge_groups
|
||||
WHERE prompt_rule_id = $1
|
||||
`, promptRuleID); err != nil {
|
||||
return response.ErrInternal(50010, "update_failed", "failed to update prompt rule knowledge groups")
|
||||
}
|
||||
|
||||
for _, groupID := range normalizeInt64IDs(groupIDs) {
|
||||
if _, err := tx.Exec(ctx, `
|
||||
INSERT INTO prompt_rule_knowledge_groups (prompt_rule_id, knowledge_group_id)
|
||||
VALUES ($1, $2)
|
||||
`, promptRuleID, groupID); err != nil {
|
||||
return response.ErrInternal(50010, "update_failed", "failed to update prompt rule knowledge groups")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func collectPromptRuleIDs(items []PromptRuleResponse) []int64 {
|
||||
ids := make([]int64, 0, len(items))
|
||||
for _, item := range items {
|
||||
ids = append(ids, item.ID)
|
||||
}
|
||||
return normalizeInt64IDs(ids)
|
||||
}
|
||||
|
||||
func promptRuleKnowledgeGroupIDs(groups []PromptRuleKnowledgeGroup) []int64 {
|
||||
ids := make([]int64, 0, len(groups))
|
||||
for _, group := range groups {
|
||||
ids = append(ids, group.ID)
|
||||
}
|
||||
return normalizeInt64IDs(ids)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user