Files
ai-agent/internal/pkg/httpx/response.go
T
mlogclub 5d7c10aeab refactor: rename agent widget references to AI agent for consistency
- Updated runtime configuration to use __CS_AI_AGENT_WIDGET_CONFIG__ instead of __CS_AGENT_WIDGET_CONFIG__.
- Changed message types in support host bridge from "cs-agent" to "cs-ai-agent".
- Minified SDK script updated to reflect new AI agent naming conventions.
- Adjusted scrollbar styles in main.scss to use .cs-ai-agent-scrollbar instead of .cs-agent-scrollbar.
2026-05-30 21:19:36 +08:00

73 lines
1.7 KiB
Go

package httpx
import (
"cs-ai-agent/internal/pkg/i18nx"
"net/http"
"github.com/gin-gonic/gin"
"github.com/mlogclub/simple/sqls"
"github.com/mlogclub/simple/web"
)
type cursorData struct {
results any
cursor string
hasMore bool
}
type pageData struct {
results any
paging *sqls.Paging
}
func CursorData(results any, cursor string, hasMore bool) any {
return cursorData{results: results, cursor: cursor, hasMore: hasMore}
}
func PageData(results any, paging *sqls.Paging) any {
return pageData{results: results, paging: paging}
}
func WriteJSON(ctx *gin.Context, result any) {
ctx.JSON(http.StatusOK, localizeJSONResult(ctx, buildJSONResult(result)))
}
func WriteHttpStatusJSON(ctx *gin.Context, statusCode int, result any) {
ctx.JSON(statusCode, localizeJSONResult(ctx, buildJSONResult(result)))
}
func localizeJSONResult(ctx *gin.Context, result *web.JsonResult) *web.JsonResult {
if result == nil || result.Success || result.Message == "" {
return result
}
result.Message = i18nx.TranslateKnownMessage(i18nx.Locale(ctx), result.Message)
return result
}
func buildJSONResult(result any) *web.JsonResult {
switch value := result.(type) {
case nil:
return web.JsonSuccess()
case *web.JsonResult:
return value
case web.JsonResult:
return &value
case *web.CodeError:
return web.JsonError(value)
case web.CodeError:
return web.JsonError(&value)
case error:
return web.JsonError(value)
case cursorData:
return web.JsonCursorData(value.results, value.cursor, value.hasMore)
case pageData:
return web.JsonPageData(value.results, value.paging)
case web.RspBuilder:
return value.JsonResult()
case *web.RspBuilder:
return value.JsonResult()
default:
return web.JsonData(result)
}
}