feat(captain): align assistant resources
This commit is contained in:
@@ -1,15 +1,17 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"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"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// CaptainAssistantHandler handles CaptainAssistant REST API endpoints.
|
||||
@@ -26,83 +28,106 @@ func NewCaptainAssistantHandler(svc *service.CaptainAssistantService) *CaptainAs
|
||||
// Create creates a new captain assistant.
|
||||
// POST /api/v1/accounts/:account_id/captain_assistants
|
||||
func (h *CaptainAssistantHandler) 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
|
||||
}
|
||||
|
||||
var req service.CreateAssistantRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
if err := bindCaptainAssistantPayload(c, &req); err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
assistant, err := h.svc.Create(c.Request.Context(), uint(accountID), &req)
|
||||
assistant, err := h.svc.Create(c.Request.Context(), accountID, &req)
|
||||
if err != nil {
|
||||
applogger.L().Errorf("Create captain assistant: %v", err)
|
||||
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to create assistant")
|
||||
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
response.Created(c, assistant)
|
||||
c.JSON(http.StatusOK, captainAssistantPayload(assistant))
|
||||
}
|
||||
|
||||
// Get retrieves a captain assistant by ID.
|
||||
// GET /api/v1/accounts/:account_id/captain_assistants/:id
|
||||
func (h *CaptainAssistantHandler) 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
|
||||
}
|
||||
id, err := parseUintParam(c, "assistant_id")
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid id")
|
||||
return
|
||||
}
|
||||
|
||||
assistant, err := h.svc.Get(c.Request.Context(), uint(id))
|
||||
assistant, err := h.svc.Get(c.Request.Context(), accountID, id)
|
||||
if err != nil {
|
||||
applogger.L().Errorf("Get captain assistant: %v", err)
|
||||
response.AbortWithStatusError(c, http.StatusNotFound, response.ErrNotFound, "assistant not found")
|
||||
return
|
||||
}
|
||||
|
||||
response.OK(c, assistant)
|
||||
c.JSON(http.StatusOK, captainAssistantPayload(assistant))
|
||||
}
|
||||
|
||||
// Update updates an existing captain assistant.
|
||||
// PUT /api/v1/accounts/:account_id/captain_assistants/:id
|
||||
func (h *CaptainAssistantHandler) 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
|
||||
}
|
||||
id, err := parseUintParam(c, "assistant_id")
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid id")
|
||||
return
|
||||
}
|
||||
|
||||
var req service.UpdateAssistantRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
if err := bindCaptainAssistantPayload(c, &req); err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
assistant, err := h.svc.Update(c.Request.Context(), uint(id), &req)
|
||||
assistant, err := h.svc.Update(c.Request.Context(), accountID, id, &req)
|
||||
if err != nil {
|
||||
applogger.L().Errorf("Update captain assistant: %v", err)
|
||||
if captainAssistantErrorStatus(err) == http.StatusNotFound {
|
||||
response.AbortWithStatusError(c, http.StatusNotFound, response.ErrNotFound, "assistant not found")
|
||||
return
|
||||
}
|
||||
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to update assistant")
|
||||
return
|
||||
}
|
||||
|
||||
response.OK(c, assistant)
|
||||
c.JSON(http.StatusOK, captainAssistantPayload(assistant))
|
||||
}
|
||||
|
||||
// Delete deletes a captain assistant.
|
||||
// DELETE /api/v1/accounts/:account_id/captain_assistants/:id
|
||||
func (h *CaptainAssistantHandler) 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
|
||||
}
|
||||
id, err := parseUintParam(c, "assistant_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.Delete(c.Request.Context(), accountID, id); err != nil {
|
||||
applogger.L().Errorf("Delete captain assistant: %v", err)
|
||||
if captainAssistantErrorStatus(err) == http.StatusNotFound {
|
||||
response.AbortWithStatusError(c, http.StatusNotFound, response.ErrNotFound, "assistant not found")
|
||||
return
|
||||
}
|
||||
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to delete assistant")
|
||||
return
|
||||
}
|
||||
@@ -113,33 +138,36 @@ func (h *CaptainAssistantHandler) Delete(c *gin.Context) {
|
||||
// List retrieves captain assistants for an account.
|
||||
// GET /api/v1/accounts/:account_id/captain_assistants
|
||||
func (h *CaptainAssistantHandler) List(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
|
||||
}
|
||||
|
||||
p := pagination.Parse(c)
|
||||
assistants, count, err := h.svc.List(c.Request.Context(), uint(accountID), p.Offset, p.PerPage)
|
||||
assistants, count, err := h.svc.List(c.Request.Context(), accountID, 0, 1000)
|
||||
if err != nil {
|
||||
applogger.L().Errorf("List captain assistants: %v", err)
|
||||
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to list assistants")
|
||||
return
|
||||
}
|
||||
|
||||
response.OKWithMeta(c, assistants, p.Page, p.PerPage, count)
|
||||
payload := make([]gin.H, 0, len(assistants))
|
||||
for i := range assistants {
|
||||
payload = append(payload, captainAssistantPayload(&assistants[i]))
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"payload": payload, "meta": gin.H{"total_count": count, "page": 1}})
|
||||
}
|
||||
|
||||
// GetConfig retrieves the assistant's JSONB config.
|
||||
// GET /api/v1/accounts/:account_id/captain_assistants/:id/config
|
||||
func (h *CaptainAssistantHandler) GetConfig(c *gin.Context) {
|
||||
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
id, err := parseUintAnyParam(c, "assistant_id", "id")
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid id")
|
||||
return
|
||||
}
|
||||
|
||||
cfg, err := h.svc.GetConfig(c.Request.Context(), uint(id))
|
||||
cfg, err := h.svc.GetConfig(c.Request.Context(), id)
|
||||
if err != nil {
|
||||
applogger.L().Errorf("GetConfig captain assistant: %v", err)
|
||||
response.AbortWithStatusError(c, http.StatusNotFound, response.ErrNotFound, "assistant not found")
|
||||
@@ -152,7 +180,7 @@ func (h *CaptainAssistantHandler) GetConfig(c *gin.Context) {
|
||||
// SetConfig updates the assistant's JSONB config.
|
||||
// PUT /api/v1/accounts/:account_id/captain_assistants/:id/config
|
||||
func (h *CaptainAssistantHandler) SetConfig(c *gin.Context) {
|
||||
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
id, err := parseUintAnyParam(c, "assistant_id", "id")
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid id")
|
||||
return
|
||||
@@ -164,7 +192,7 @@ func (h *CaptainAssistantHandler) SetConfig(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.svc.SetConfig(c.Request.Context(), uint(id), &cfg); err != nil {
|
||||
if err := h.svc.SetConfig(c.Request.Context(), id, &cfg); err != nil {
|
||||
applogger.L().Errorf("SetConfig captain assistant: %v", err)
|
||||
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to update config")
|
||||
return
|
||||
@@ -176,80 +204,191 @@ func (h *CaptainAssistantHandler) SetConfig(c *gin.Context) {
|
||||
// AssociateInbox binds an assistant to an inbox.
|
||||
// POST /api/v1/accounts/:account_id/captain_assistants/:id/inboxes
|
||||
func (h *CaptainAssistantHandler) AssociateInbox(c *gin.Context) {
|
||||
assistantID, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
assistantID, err := parseUintParam(c, "assistant_id")
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid assistant id")
|
||||
return
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
var req struct {
|
||||
InboxID uint `json:"inbox_id" validate:"required"`
|
||||
Inbox struct {
|
||||
InboxID uint `json:"inbox_id"`
|
||||
} `json:"inbox"`
|
||||
InboxID uint `json:"inbox_id"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error())
|
||||
return
|
||||
}
|
||||
inboxID := req.InboxID
|
||||
if inboxID == 0 {
|
||||
inboxID = req.Inbox.InboxID
|
||||
}
|
||||
|
||||
if err := h.svc.AssociateInbox(c.Request.Context(), uint(assistantID), req.InboxID, uint(accountID)); err != nil {
|
||||
inbox, err := h.svc.AssociateInbox(c.Request.Context(), assistantID, inboxID, accountID)
|
||||
if err != nil {
|
||||
applogger.L().Errorf("AssociateInbox: %v", err)
|
||||
if captainAssistantErrorStatus(err) == http.StatusNotFound {
|
||||
response.AbortWithStatusError(c, http.StatusNotFound, response.ErrNotFound, "captain inbox not found")
|
||||
return
|
||||
}
|
||||
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to associate inbox")
|
||||
return
|
||||
}
|
||||
|
||||
response.OK(c, gin.H{"associated": true})
|
||||
c.JSON(http.StatusOK, serializeInbox(inbox))
|
||||
}
|
||||
|
||||
// DissociateInbox unbinds an assistant from an inbox.
|
||||
// DELETE /api/v1/accounts/:account_id/captain_assistants/:id/inboxes/:inbox_id
|
||||
func (h *CaptainAssistantHandler) DissociateInbox(c *gin.Context) {
|
||||
assistantID, 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
|
||||
}
|
||||
inboxID, err := strconv.ParseUint(c.Param("inbox_id"), 10, 64)
|
||||
inboxID, err := parseUintParam(c, "inbox_id")
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid inbox_id")
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.svc.DissociateInbox(c.Request.Context(), uint(assistantID), uint(inboxID)); err != nil {
|
||||
if err := h.svc.DissociateInbox(c.Request.Context(), accountID, assistantID, inboxID); err != nil {
|
||||
applogger.L().Errorf("DissociateInbox: %v", err)
|
||||
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to dissociate inbox")
|
||||
response.AbortWithStatusError(c, http.StatusNotFound, response.ErrNotFound, "captain inbox not found")
|
||||
return
|
||||
}
|
||||
|
||||
response.OK(c, gin.H{"dissociated": true})
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func (h *CaptainAssistantHandler) ListInboxes(c *gin.Context) {
|
||||
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
|
||||
}
|
||||
inboxes, err := h.svc.ListInboxes(c.Request.Context(), accountID, assistantID)
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusNotFound, response.ErrNotFound, "assistant not found")
|
||||
return
|
||||
}
|
||||
payload := make([]gin.H, 0, len(inboxes))
|
||||
for i := range inboxes {
|
||||
payload = append(payload, serializeInbox(&inboxes[i]))
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"payload": payload, "meta": gin.H{"total_count": len(payload), "page": 1}})
|
||||
}
|
||||
|
||||
func (h *CaptainAssistantHandler) Tools(c *gin.Context) {
|
||||
accountID := parseAccountIDParam(c)
|
||||
c.JSON(http.StatusOK, h.svc.AvailableTools(c.Request.Context(), accountID))
|
||||
}
|
||||
|
||||
// GenerateResponse generates an AI response via RAG.
|
||||
// POST /api/v1/accounts/:account_id/captain_assistants/:id/generate_response
|
||||
func (h *CaptainAssistantHandler) GenerateResponse(c *gin.Context) {
|
||||
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
id, err := parseUintAnyParam(c, "assistant_id", "id")
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid id")
|
||||
return
|
||||
}
|
||||
|
||||
var req struct {
|
||||
Query string `json:"query" validate:"required"`
|
||||
Assistant struct {
|
||||
MessageContent string `json:"message_content"`
|
||||
Query string `json:"query"`
|
||||
} `json:"assistant"`
|
||||
MessageContent string `json:"message_content"`
|
||||
Query string `json:"query"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error())
|
||||
return
|
||||
}
|
||||
query := req.Query
|
||||
if query == "" {
|
||||
query = req.MessageContent
|
||||
}
|
||||
if query == "" {
|
||||
query = req.Assistant.Query
|
||||
}
|
||||
if query == "" {
|
||||
query = req.Assistant.MessageContent
|
||||
}
|
||||
if query == "" {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, "message_content is required")
|
||||
return
|
||||
}
|
||||
|
||||
result, err := h.svc.GenerateResponse(c.Request.Context(), uint(id), req.Query)
|
||||
result, err := h.svc.GenerateResponse(c.Request.Context(), id, query)
|
||||
if err != nil {
|
||||
applogger.L().Errorf("GenerateResponse: %v", err)
|
||||
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to generate response")
|
||||
return
|
||||
}
|
||||
|
||||
response.OK(c, gin.H{"response": result})
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"response": result})
|
||||
}
|
||||
|
||||
func bindCaptainAssistantPayload(c *gin.Context, dst any) error {
|
||||
var raw map[string]json.RawMessage
|
||||
if err := c.ShouldBindJSON(&raw); err != nil {
|
||||
return err
|
||||
}
|
||||
if nested, ok := raw["assistant"]; ok {
|
||||
return json.Unmarshal(nested, dst)
|
||||
}
|
||||
body, err := json.Marshal(raw)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return json.Unmarshal(body, dst)
|
||||
}
|
||||
|
||||
func captainAssistantPayload(assistant *model.CaptainAssistant) gin.H {
|
||||
return gin.H{
|
||||
"account_id": assistant.AccountID,
|
||||
"config": rawJSONValue(assistant.Config),
|
||||
"created_at": assistant.CreatedAt.Unix(),
|
||||
"description": assistant.Description,
|
||||
"guardrails": rawJSONValue(assistant.Guardrails),
|
||||
"id": assistant.ID,
|
||||
"name": assistant.Name,
|
||||
"response_guidelines": rawJSONValue(assistant.ResponseGuidelines),
|
||||
"updated_at": assistant.UpdatedAt.Unix(),
|
||||
}
|
||||
}
|
||||
|
||||
func rawJSONValue(raw json.RawMessage) any {
|
||||
if len(raw) == 0 || string(raw) == "null" {
|
||||
return nil
|
||||
}
|
||||
var value any
|
||||
if err := json.Unmarshal(raw, &value); err != nil {
|
||||
return fmt.Sprintf("%s", raw)
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func captainAssistantErrorStatus(err error) int {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return http.StatusNotFound
|
||||
}
|
||||
return http.StatusInternalServerError
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user