mirror of
https://github.com/Ed1s0nZ/CyberStrikeAI.git
synced 2026-07-10 22:33:48 +02:00
Add files via upload
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"cyberstrike-ai/internal/audit"
|
||||
@@ -316,20 +317,55 @@ func (h *KnowledgeHandler) DeleteItem(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"message": "删除成功"})
|
||||
}
|
||||
|
||||
// RebuildIndex 重建索引
|
||||
func (h *KnowledgeHandler) RebuildIndex(c *gin.Context) {
|
||||
// 异步重建索引
|
||||
// StartIndex 构建知识库向量索引。默认仅补齐尚无向量的知识项;mode=full 时全量重建。
|
||||
func (h *KnowledgeHandler) StartIndex(c *gin.Context) {
|
||||
if err := h.indexer.TryBeginIndexRun(); err != nil {
|
||||
c.JSON(http.StatusConflict, gin.H{"error": "已有索引任务正在进行,请等待完成"})
|
||||
return
|
||||
}
|
||||
|
||||
mode := strings.TrimSpace(c.Query("mode"))
|
||||
if mode == "" {
|
||||
mode = "missing"
|
||||
}
|
||||
if mode != "full" && mode != "missing" {
|
||||
h.indexer.FinishIndexRun()
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "无效的 mode 参数,可选值:missing、full"})
|
||||
return
|
||||
}
|
||||
|
||||
fullRebuild := mode == "full"
|
||||
message := "索引构建已开始,将在后台进行"
|
||||
auditAction := "index_build"
|
||||
auditDetail := "构建知识库索引"
|
||||
if fullRebuild {
|
||||
message = "全量索引重建已开始,将在后台进行"
|
||||
auditAction = "index_rebuild_full"
|
||||
auditDetail = "全量重建知识库索引"
|
||||
}
|
||||
|
||||
go func() {
|
||||
defer h.indexer.FinishIndexRun()
|
||||
ctx := context.Background()
|
||||
if err := h.indexer.RebuildIndex(ctx); err != nil {
|
||||
h.logger.Error("重建索引失败", zap.Error(err))
|
||||
var err error
|
||||
if fullRebuild {
|
||||
err = h.indexer.RunRebuildIndex(ctx)
|
||||
} else {
|
||||
err = h.indexer.RunIndexMissing(ctx)
|
||||
}
|
||||
if err != nil {
|
||||
if fullRebuild {
|
||||
h.logger.Error("全量重建索引失败", zap.Error(err))
|
||||
} else {
|
||||
h.logger.Error("构建知识库索引失败", zap.Error(err))
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
if h.audit != nil {
|
||||
h.audit.RecordOK(c, "knowledge", "index_rebuild", "重建知识库索引", "knowledge", "", nil)
|
||||
h.audit.RecordOK(c, "knowledge", auditAction, auditDetail, "knowledge", "", nil)
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"message": "索引重建已开始,将在后台进行"})
|
||||
c.JSON(http.StatusOK, gin.H{"message": message, "mode": mode})
|
||||
}
|
||||
|
||||
// ScanKnowledgeBase 扫描知识库
|
||||
|
||||
@@ -4416,16 +4416,35 @@ func (h *OpenAPIHandler) GetOpenAPISpec(c *gin.Context) {
|
||||
"/api/knowledge/index": map[string]interface{}{
|
||||
"post": map[string]interface{}{
|
||||
"tags": []string{"知识库"},
|
||||
"summary": "重建索引",
|
||||
"description": "重新构建知识库索引",
|
||||
"operationId": "rebuildIndex",
|
||||
"summary": "构建索引",
|
||||
"description": "构建知识库向量索引。默认仅处理尚无向量的知识项;mode=full 时全量重建。",
|
||||
"operationId": "startKnowledgeIndex",
|
||||
"parameters": []map[string]interface{}{
|
||||
{
|
||||
"name": "mode",
|
||||
"in": "query",
|
||||
"required": false,
|
||||
"description": "索引模式:missing(默认,补齐缺失向量)或 full(全量重建)",
|
||||
"schema": map[string]interface{}{
|
||||
"type": "string",
|
||||
"enum": []string{"missing", "full"},
|
||||
"default": "missing",
|
||||
},
|
||||
},
|
||||
},
|
||||
"responses": map[string]interface{}{
|
||||
"200": map[string]interface{}{
|
||||
"description": "重建索引任务已启动",
|
||||
"description": "索引任务已启动",
|
||||
},
|
||||
"400": map[string]interface{}{
|
||||
"description": "无效的 mode 参数",
|
||||
},
|
||||
"401": map[string]interface{}{
|
||||
"description": "未授权",
|
||||
},
|
||||
"409": map[string]interface{}{
|
||||
"description": "已有索引任务正在进行",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
@@ -43,7 +43,7 @@ var apiDocI18nSummaryToKey = map[string]string{
|
||||
"设置对话置顶": "pinConversation", "设置分组置顶": "pinGroup", "设置分组中对话的置顶": "pinGroupConversation",
|
||||
"获取分类": "getCategories", "列出知识项": "listKnowledgeItems", "创建知识项": "createKnowledgeItem",
|
||||
"获取知识项": "getKnowledgeItem", "更新知识项": "updateKnowledgeItem", "删除知识项": "deleteKnowledgeItem",
|
||||
"获取索引状态": "getIndexStatus", "重建索引": "rebuildIndex", "扫描知识库": "scanKnowledgeBase",
|
||||
"获取索引状态": "getIndexStatus", "构建索引": "startKnowledgeIndex", "扫描知识库": "scanKnowledgeBase",
|
||||
"搜索知识库": "searchKnowledgeBase", "基础搜索": "basicSearch", "按风险类型搜索": "searchByRiskType",
|
||||
"获取检索日志": "getRetrievalLogs", "删除检索日志": "deleteRetrievalLog",
|
||||
"MCP端点": "mcpEndpoint", "列出所有工具": "listAllTools", "调用工具": "invokeTool", "初始化连接": "initConnection",
|
||||
|
||||
Reference in New Issue
Block a user