feat: Enhance FAQ import functionality and UI improvements

- Added support for tracking the status of FAQ import tasks, including progress indicators for ongoing imports.
- Introduced new localization strings for processing tasks and batch operations in multiple languages.
- Updated the FAQ entry manager to display import progress and improved user feedback during the import process.
- Enhanced the KnowledgeBase component to reflect the processing state of knowledge items, improving user experience during document uploads.
- Implemented batch deletion functionality for chunks, streamlining the management of knowledge base content.
This commit is contained in:
wizardchen
2025-11-23 17:32:50 +08:00
parent e853605cea
commit 057cfd9eb0
46 changed files with 3672 additions and 1400 deletions
+142 -11
View File
@@ -1,7 +1,11 @@
package middleware
import (
"bytes"
"context"
"io"
"regexp"
"strings"
"time"
"github.com/Tencent/WeKnora/internal/logger"
@@ -10,6 +14,88 @@ import (
"github.com/google/uuid"
)
const (
maxBodySize = 1024 * 10 // 最大记录10KB的body内容
)
// loggerResponseBodyWriter 自定义ResponseWriter用于捕获响应内容(用于logger中间件)
type loggerResponseBodyWriter struct {
gin.ResponseWriter
body *bytes.Buffer
}
// Write 重写Write方法,同时写入buffer和原始writer
func (r loggerResponseBodyWriter) Write(b []byte) (int, error) {
r.body.Write(b)
return r.ResponseWriter.Write(b)
}
// sanitizeBody 清理敏感信息
func sanitizeBody(body string) string {
result := body
// 替换常见的敏感字段(JSON格式)
sensitivePatterns := []struct {
pattern string
replacement string
}{
{`"password"\s*:\s*"[^"]*"`, `"password":"***"`},
{`"token"\s*:\s*"[^"]*"`, `"token":"***"`},
{`"access_token"\s*:\s*"[^"]*"`, `"access_token":"***"`},
{`"refresh_token"\s*:\s*"[^"]*"`, `"refresh_token":"***"`},
{`"authorization"\s*:\s*"[^"]*"`, `"authorization":"***"`},
{`"api_key"\s*:\s*"[^"]*"`, `"api_key":"***"`},
{`"secret"\s*:\s*"[^"]*"`, `"secret":"***"`},
{`"apikey"\s*:\s*"[^"]*"`, `"apikey":"***"`},
{`"apisecret"\s*:\s*"[^"]*"`, `"apisecret":"***"`},
}
for _, p := range sensitivePatterns {
re := regexp.MustCompile(p.pattern)
result = re.ReplaceAllString(result, p.replacement)
}
return result
}
// readRequestBody 读取请求体(限制大小用于日志,但完整读取用于重置)
func readRequestBody(c *gin.Context) string {
if c.Request.Body == nil {
return ""
}
// 检查Content-Type,只记录JSON类型
contentType := c.GetHeader("Content-Type")
if !strings.Contains(contentType, "application/json") &&
!strings.Contains(contentType, "application/x-www-form-urlencoded") &&
!strings.Contains(contentType, "text/") {
return "[非文本类型,已跳过]"
}
// 完整读取body内容(不限制大小),因为需要完整重置给后续handler使用
bodyBytes, err := io.ReadAll(c.Request.Body)
if err != nil {
return "[读取请求体失败]"
}
// 重置request body,使用完整内容,确保后续handler能读取到完整数据
c.Request.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
// 用于日志的body(限制大小)
var logBodyBytes []byte
if len(bodyBytes) > maxBodySize {
logBodyBytes = bodyBytes[:maxBodySize]
} else {
logBodyBytes = bodyBytes
}
bodyStr := string(logBodyBytes)
if len(bodyBytes) > maxBodySize {
bodyStr += "... [内容过长,已截断]"
}
return sanitizeBody(bodyStr)
}
// RequestID middleware adds a unique request ID to the context
func RequestID() gin.HandlerFunc {
return func(c *gin.Context) {
@@ -41,13 +127,27 @@ func RequestID() gin.HandlerFunc {
}
}
// Logger middleware logs request details with request ID
// Logger middleware logs request details with request ID, input and output
func Logger() gin.HandlerFunc {
return func(c *gin.Context) {
start := time.Now()
path := c.Request.URL.Path
raw := c.Request.URL.RawQuery
// 读取请求体(在Next之前读取,因为Next会消费body)
var requestBody string
if c.Request.Method == "POST" || c.Request.Method == "PUT" || c.Request.Method == "PATCH" {
requestBody = readRequestBody(c)
}
// 创建响应体捕获器
responseBody := &bytes.Buffer{}
responseWriter := &loggerResponseBodyWriter{
ResponseWriter: c.Writer,
body: responseBody,
}
c.Writer = responseWriter
// Process request
c.Next()
@@ -69,15 +169,46 @@ func Logger() gin.HandlerFunc {
path = path + "?" + raw
}
// Log with request ID
logger.GetLogger(c).Infof("[%s] %d | %3d | %13v | %15s | %s %s",
requestID,
statusCode,
c.Writer.Size(),
latency,
clientIP,
method,
path,
)
// 读取响应体
responseBodyStr := ""
if responseBody.Len() > 0 {
// 检查Content-Type,只记录JSON类型
contentType := c.Writer.Header().Get("Content-Type")
if strings.Contains(contentType, "application/json") ||
strings.Contains(contentType, "text/") {
bodyBytes := responseBody.Bytes()
if len(bodyBytes) > maxBodySize {
responseBodyStr = string(bodyBytes[:maxBodySize]) + "... [内容过长,已截断]"
} else {
responseBodyStr = string(bodyBytes)
}
responseBodyStr = sanitizeBody(responseBodyStr)
} else {
responseBodyStr = "[非文本类型,已跳过]"
}
}
// 构建日志消息
logMsg := logger.GetLogger(c)
logMsg = logMsg.WithFields(map[string]interface{}{
"request_id": requestID,
"method": method,
"path": path,
"status_code": statusCode,
"size": c.Writer.Size(),
"latency": latency.String(),
"client_ip": clientIP,
})
// 添加请求体(如果有)
if requestBody != "" {
logMsg = logMsg.WithField("request_body", requestBody)
}
// 添加响应体(如果有)
if responseBodyStr != "" {
logMsg = logMsg.WithField("response_body", responseBodyStr)
}
logMsg.Info()
}
}