feat(captain): align scenario and tool payloads

This commit is contained in:
2026-06-05 12:05:53 +08:00
parent 41a1b0c47e
commit fc2cb12e63
13 changed files with 588 additions and 107 deletions
@@ -4,9 +4,9 @@ import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/gochat/gochat/internal/model"
"github.com/gochat/gochat/internal/service"
applogger "github.com/gochat/gochat/pkg/logger"
"github.com/gochat/gochat/pkg/pagination"
"github.com/gochat/gochat/pkg/response"
)
@@ -31,7 +31,7 @@ func (h *CaptainCustomToolHandler) Create(c *gin.Context) {
}
var req service.CreateCustomToolRequest
if err := c.ShouldBindJSON(&req); err != nil {
if err := bindNestedJSONPayload(c, "custom_tool", &req); err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error())
return
}
@@ -43,31 +43,41 @@ func (h *CaptainCustomToolHandler) Create(c *gin.Context) {
return
}
response.Created(c, tool)
c.JSON(http.StatusOK, captainCustomToolPayload(tool))
}
// Get retrieves a custom tool by ID.
// GET /api/v1/accounts/:account_id/captain_custom_tools/:id
func (h *CaptainCustomToolHandler) Get(c *gin.Context) {
accountID := parseAccountIDParam(c)
if accountID == 0 {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
return
}
id, err := parseUintAnyParam(c, "tool_id", "id")
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid id")
return
}
tool, err := h.svc.Get(c.Request.Context(), id)
tool, err := h.svc.GetByAccount(c.Request.Context(), accountID, id)
if err != nil {
applogger.L().Errorf("Get captain custom tool: %v", err)
response.AbortWithStatusError(c, http.StatusNotFound, response.ErrNotFound, "custom tool not found")
return
}
response.OK(c, tool)
c.JSON(http.StatusOK, captainCustomToolPayload(tool))
}
// Update updates an existing custom tool.
// PUT /api/v1/accounts/:account_id/captain_custom_tools/:id
func (h *CaptainCustomToolHandler) Update(c *gin.Context) {
accountID := parseAccountIDParam(c)
if accountID == 0 {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
return
}
id, err := parseUintAnyParam(c, "tool_id", "id")
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid id")
@@ -75,33 +85,38 @@ func (h *CaptainCustomToolHandler) Update(c *gin.Context) {
}
var req service.UpdateCustomToolRequest
if err := c.ShouldBindJSON(&req); err != nil {
if err := bindNestedJSONPayload(c, "custom_tool", &req); err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error())
return
}
tool, err := h.svc.Update(c.Request.Context(), id, &req)
tool, err := h.svc.UpdateByAccount(c.Request.Context(), accountID, id, &req)
if err != nil {
applogger.L().Errorf("Update captain custom tool: %v", err)
handleServiceError(c, err)
return
}
response.OK(c, tool)
c.JSON(http.StatusOK, captainCustomToolPayload(tool))
}
// Delete deletes a custom tool.
// DELETE /api/v1/accounts/:account_id/captain_custom_tools/:id
func (h *CaptainCustomToolHandler) Delete(c *gin.Context) {
accountID := parseAccountIDParam(c)
if accountID == 0 {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
return
}
id, err := parseUintAnyParam(c, "tool_id", "id")
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid id")
return
}
if err := h.svc.Delete(c.Request.Context(), id); err != nil {
if err := h.svc.DeleteByAccount(c.Request.Context(), accountID, id); err != nil {
applogger.L().Errorf("Delete captain custom tool: %v", err)
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to delete custom tool")
response.AbortWithStatusError(c, captainAssistantErrorStatus(err), response.ErrInternal, "failed to delete custom tool")
return
}
@@ -117,15 +132,18 @@ func (h *CaptainCustomToolHandler) List(c *gin.Context) {
return
}
p := pagination.Parse(c)
tools, count, err := h.svc.List(c.Request.Context(), accountID, p.Offset, p.PerPage)
tools, count, err := h.svc.List(c.Request.Context(), accountID, 0, 1000)
if err != nil {
applogger.L().Errorf("List captain custom tools: %v", err)
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to list custom tools")
return
}
response.OKWithMeta(c, tools, p.Page, p.PerPage, count)
payload := make([]gin.H, 0, len(tools))
for i := range tools {
payload = append(payload, captainCustomToolPayload(&tools[i]))
}
c.JSON(http.StatusOK, gin.H{"payload": payload, "meta": gin.H{"total_count": count, "page": 1}})
}
// ExecuteTool calls the external HTTP endpoint of a custom tool.
@@ -163,7 +181,7 @@ func (h *CaptainCustomToolHandler) TestTool(c *gin.Context) {
}
var req service.TestToolRequest
if err := c.ShouldBindJSON(&req); err != nil {
if err := bindNestedJSONPayload(c, "custom_tool", &req); err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error())
return
}
@@ -175,5 +193,25 @@ func (h *CaptainCustomToolHandler) TestTool(c *gin.Context) {
return
}
response.OK(c, result)
c.JSON(http.StatusOK, result)
}
func captainCustomToolPayload(tool *model.CaptainCustomTool) gin.H {
return gin.H{
"id": tool.ID,
"slug": tool.Slug,
"title": tool.Title,
"description": tool.Description,
"endpoint_url": tool.EndpointURL,
"http_method": tool.HTTPMethod,
"request_template": tool.RequestTemplate,
"response_template": tool.ResponseTemplate,
"auth_type": tool.AuthType,
"auth_config": rawJSONValue(tool.AuthConfig),
"param_schema": rawJSONValue(tool.ParamSchema),
"enabled": tool.Enabled,
"account_id": tool.AccountID,
"created_at": tool.CreatedAt.Unix(),
"updated_at": tool.UpdatedAt.Unix(),
}
}