refactor: 将客服后端重构为宿主可嵌入模块
- 注入数据库、运行时配置、统一响应、文件存储和平台 AI 能力,补充业务读写工具与客户快捷操作契约。 - 移除模块内重复的组织、客户、工单、标签、技能、旧工作流、MCP 和迁移实现,将身份权限与业务主体交由宿主管理。 - 使用 libSQL 重构向量存储,并完善图片消息、访客身份、排队调度、企业微信和支持聊天页面。 - 统一 HTTP、DTO 与 WebSocket 的 snake_case 协议,补齐模块初始化、业务动作和公共载荷等回归测试。
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
package httpx
|
||||
|
||||
import (
|
||||
"code.tczkiot.com/wlw/ai-agent/contract"
|
||||
"code.tczkiot.com/wlw/ai-agent/internal/pkg/errorsx"
|
||||
"code.tczkiot.com/wlw/ai-agent/internal/pkg/i18nx"
|
||||
"net/http"
|
||||
"sync"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/mlogclub/simple/sqls"
|
||||
@@ -21,10 +23,32 @@ type pageData struct {
|
||||
paging *sqls.Paging
|
||||
}
|
||||
|
||||
type cursorResult struct {
|
||||
Results any `json:"results"`
|
||||
Cursor string `json:"cursor"`
|
||||
HasMore bool `json:"has_more"`
|
||||
}
|
||||
|
||||
type pageResult struct {
|
||||
Page *sqls.Paging `json:"page"`
|
||||
Results any `json:"results"`
|
||||
}
|
||||
|
||||
type localizedError interface {
|
||||
Message(locale string) string
|
||||
}
|
||||
|
||||
var (
|
||||
responseWriterMu sync.RWMutex
|
||||
responseWriter contract.ResponseWriter
|
||||
)
|
||||
|
||||
func SetResponseWriter(writer contract.ResponseWriter) {
|
||||
responseWriterMu.Lock()
|
||||
defer responseWriterMu.Unlock()
|
||||
responseWriter = writer
|
||||
}
|
||||
|
||||
func CursorData(results any, cursor string, hasMore bool) any {
|
||||
return cursorData{results: results, cursor: cursor, hasMore: hasMore}
|
||||
}
|
||||
@@ -34,11 +58,36 @@ func PageData(results any, paging *sqls.Paging) any {
|
||||
}
|
||||
|
||||
func WriteJSON(ctx *gin.Context, result any) {
|
||||
ctx.JSON(http.StatusOK, buildJSONResult(ctx, result))
|
||||
writeJSON(ctx, http.StatusOK, result)
|
||||
}
|
||||
|
||||
func WriteHttpStatusJSON(ctx *gin.Context, statusCode int, result any) {
|
||||
ctx.JSON(statusCode, buildJSONResult(ctx, result))
|
||||
writeJSON(ctx, statusCode, result)
|
||||
}
|
||||
|
||||
func AbortJSON(ctx *gin.Context, statusCode int, result any) {
|
||||
writeJSON(ctx, statusCode, result)
|
||||
ctx.Abort()
|
||||
}
|
||||
|
||||
func writeJSON(ctx *gin.Context, statusCode int, result any) {
|
||||
payload := buildJSONResult(ctx, result)
|
||||
responseWriterMu.RLock()
|
||||
writer := responseWriter
|
||||
responseWriterMu.RUnlock()
|
||||
if writer == nil {
|
||||
ctx.JSON(statusCode, payload)
|
||||
return
|
||||
}
|
||||
if err := writer(ctx.Writer, ctx.Request, contract.Response{
|
||||
StatusCode: statusCode,
|
||||
ErrorCode: payload.ErrorCode,
|
||||
Message: payload.Message,
|
||||
Data: payload.Data,
|
||||
Success: payload.Success,
|
||||
}); err != nil {
|
||||
_ = ctx.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func buildJSONResult(ctx *gin.Context, result any) *web.JsonResult {
|
||||
@@ -62,9 +111,24 @@ func buildJSONResult(ctx *gin.Context, result any) *web.JsonResult {
|
||||
case error:
|
||||
return web.JsonError(value)
|
||||
case cursorData:
|
||||
return web.JsonCursorData(value.results, value.cursor, value.hasMore)
|
||||
results := value.results
|
||||
if results == nil {
|
||||
results = []any{}
|
||||
}
|
||||
return web.JsonData(&cursorResult{
|
||||
Results: results,
|
||||
Cursor: value.cursor,
|
||||
HasMore: value.hasMore,
|
||||
})
|
||||
case pageData:
|
||||
return web.JsonPageData(value.results, value.paging)
|
||||
results := value.results
|
||||
if results == nil {
|
||||
results = []any{}
|
||||
}
|
||||
return web.JsonData(&pageResult{
|
||||
Page: value.paging,
|
||||
Results: results,
|
||||
})
|
||||
case web.RspBuilder:
|
||||
return value.JsonResult()
|
||||
case *web.RspBuilder:
|
||||
|
||||
Reference in New Issue
Block a user