feat(captain): align scenario and tool payloads
This commit is contained in:
@@ -2,12 +2,11 @@ package v1
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
@@ -25,89 +24,119 @@ func NewCaptainScenarioHandler(svc *service.CaptainScenarioService) *CaptainScen
|
||||
// Create creates a new scenario under an assistant.
|
||||
// POST /api/v1/accounts/:account_id/captain_assistants/:assistant_id/scenarios
|
||||
func (h *CaptainScenarioHandler) Create(c *gin.Context) {
|
||||
accountID, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
accountID := parseAccountIDParam(c)
|
||||
if accountID == 0 {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
||||
return
|
||||
}
|
||||
assistantID, err := strconv.ParseUint(c.Param("assistant_id"), 10, 64)
|
||||
assistantID, err := parseUintParam(c, "assistant_id")
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid assistant_id")
|
||||
return
|
||||
}
|
||||
|
||||
var req service.CreateScenarioRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
if err := bindNestedJSONPayload(c, "scenario", &req); err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
scenario, err := h.svc.Create(c.Request.Context(), uint(accountID), uint(assistantID), &req)
|
||||
scenario, err := h.svc.Create(c.Request.Context(), accountID, assistantID, &req)
|
||||
if err != nil {
|
||||
applogger.L().Errorf("Create captain scenario: %v", err)
|
||||
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to create scenario")
|
||||
response.AbortWithStatusError(c, captainAssistantErrorStatus(err), response.ErrInternal, "failed to create scenario")
|
||||
return
|
||||
}
|
||||
|
||||
response.Created(c, scenario)
|
||||
c.JSON(http.StatusOK, captainScenarioPayload(scenario))
|
||||
}
|
||||
|
||||
// Get retrieves a scenario by ID.
|
||||
// GET /api/v1/accounts/:account_id/captain_assistants/:assistant_id/scenarios/:id
|
||||
func (h *CaptainScenarioHandler) Get(c *gin.Context) {
|
||||
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
accountID := parseAccountIDParam(c)
|
||||
if accountID == 0 {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
||||
return
|
||||
}
|
||||
assistantID, err := parseUintParam(c, "assistant_id")
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid assistant_id")
|
||||
return
|
||||
}
|
||||
id, err := parseUintAnyParam(c, "scenario_id", "id")
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid id")
|
||||
return
|
||||
}
|
||||
|
||||
scenario, err := h.svc.GetByID(c.Request.Context(), uint(id))
|
||||
scenario, err := h.svc.Get(c.Request.Context(), accountID, assistantID, id)
|
||||
if err != nil {
|
||||
applogger.L().Errorf("Get captain scenario: %v", err)
|
||||
response.AbortWithStatusError(c, http.StatusNotFound, response.ErrNotFound, "scenario not found")
|
||||
return
|
||||
}
|
||||
|
||||
response.OK(c, scenario)
|
||||
c.JSON(http.StatusOK, captainScenarioPayload(scenario))
|
||||
}
|
||||
|
||||
// Update updates an existing scenario.
|
||||
// PUT /api/v1/accounts/:account_id/captain_assistants/:assistant_id/scenarios/:id
|
||||
func (h *CaptainScenarioHandler) Update(c *gin.Context) {
|
||||
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
accountID := parseAccountIDParam(c)
|
||||
if accountID == 0 {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
||||
return
|
||||
}
|
||||
assistantID, err := parseUintParam(c, "assistant_id")
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid assistant_id")
|
||||
return
|
||||
}
|
||||
id, err := parseUintAnyParam(c, "scenario_id", "id")
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid id")
|
||||
return
|
||||
}
|
||||
|
||||
var req service.UpdateScenarioRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
if err := bindNestedJSONPayload(c, "scenario", &req); err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
scenario, err := h.svc.Update(c.Request.Context(), uint(id), &req)
|
||||
scenario, err := h.svc.UpdateScoped(c.Request.Context(), accountID, assistantID, id, &req)
|
||||
if err != nil {
|
||||
applogger.L().Errorf("Update captain scenario: %v", err)
|
||||
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to update scenario")
|
||||
response.AbortWithStatusError(c, captainAssistantErrorStatus(err), response.ErrInternal, "failed to update scenario")
|
||||
return
|
||||
}
|
||||
|
||||
response.OK(c, scenario)
|
||||
c.JSON(http.StatusOK, captainScenarioPayload(scenario))
|
||||
}
|
||||
|
||||
// Delete deletes a scenario.
|
||||
// DELETE /api/v1/accounts/:account_id/captain_assistants/:assistant_id/scenarios/:id
|
||||
func (h *CaptainScenarioHandler) Delete(c *gin.Context) {
|
||||
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
accountID := parseAccountIDParam(c)
|
||||
if accountID == 0 {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
||||
return
|
||||
}
|
||||
assistantID, err := parseUintParam(c, "assistant_id")
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid assistant_id")
|
||||
return
|
||||
}
|
||||
id, err := parseUintAnyParam(c, "scenario_id", "id")
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid id")
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.svc.Delete(c.Request.Context(), uint(id)); err != nil {
|
||||
if err := h.svc.DeleteScoped(c.Request.Context(), accountID, assistantID, id); err != nil {
|
||||
applogger.L().Errorf("Delete captain scenario: %v", err)
|
||||
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to delete scenario")
|
||||
response.AbortWithStatusError(c, captainAssistantErrorStatus(err), response.ErrInternal, "failed to delete scenario")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -117,19 +146,46 @@ func (h *CaptainScenarioHandler) Delete(c *gin.Context) {
|
||||
// List retrieves scenarios for an assistant.
|
||||
// GET /api/v1/accounts/:account_id/captain_assistants/:assistant_id/scenarios
|
||||
func (h *CaptainScenarioHandler) List(c *gin.Context) {
|
||||
assistantID, err := strconv.ParseUint(c.Param("assistant_id"), 10, 64)
|
||||
accountID := parseAccountIDParam(c)
|
||||
if accountID == 0 {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
||||
return
|
||||
}
|
||||
assistantID, err := parseUintParam(c, "assistant_id")
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid assistant_id")
|
||||
return
|
||||
}
|
||||
|
||||
p := pagination.Parse(c)
|
||||
scenarios, count, err := h.svc.ListByAssistant(c.Request.Context(), uint(assistantID), p.Offset, p.PerPage)
|
||||
scenarios, count, err := h.svc.ListByAccountAssistant(c.Request.Context(), accountID, assistantID)
|
||||
if err != nil {
|
||||
applogger.L().Errorf("List captain scenarios: %v", err)
|
||||
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to list scenarios")
|
||||
response.AbortWithStatusError(c, captainAssistantErrorStatus(err), response.ErrInternal, "failed to list scenarios")
|
||||
return
|
||||
}
|
||||
|
||||
response.OKWithMeta(c, scenarios, p.Page, p.PerPage, count)
|
||||
}
|
||||
payload := make([]gin.H, 0, len(scenarios))
|
||||
for i := range scenarios {
|
||||
payload = append(payload, captainScenarioPayload(&scenarios[i]))
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"payload": payload, "meta": gin.H{"total_count": count, "page": 1}})
|
||||
}
|
||||
|
||||
func captainScenarioPayload(scenario *model.CaptainScenario) gin.H {
|
||||
payload := gin.H{
|
||||
"id": scenario.ID,
|
||||
"title": scenario.Title,
|
||||
"description": scenario.Description,
|
||||
"instruction": scenario.Instruction,
|
||||
"tools": rawJSONValue(scenario.Tools),
|
||||
"enabled": scenario.Enabled,
|
||||
"assistant_id": scenario.AssistantID,
|
||||
"account_id": scenario.AccountID,
|
||||
"created_at": scenario.CreatedAt,
|
||||
"updated_at": scenario.UpdatedAt,
|
||||
}
|
||||
if scenario.Assistant.ID != 0 {
|
||||
payload["assistant"] = gin.H{"id": scenario.Assistant.ID, "name": scenario.Assistant.Name}
|
||||
}
|
||||
return payload
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user