AutoReplyRule complete integration: - bootstrap.go: instantiate AutoReplyRuleService + AutoReplyListener, register listener on channel dispatcher for message.created events - router.go: register /captain/auto_reply_rules CRUD + /evaluate routes - auto_reply_rule_handler.go: fix c.Param(id) → c.Param(account_id), override evalCtx.AccountID from path param - auto_reply_rule_service.go: add JSON tags to AutoReplyEvaluationContext for correct request body binding - auto_reply_listener.go (new): EventListener that triggers on incoming messages, evaluates active rules, composes reply (static/LLM/mixed), respects DelaySeconds and OneTimeOnly flags, sends via MessageService - migration 000048: create captain_auto_reply_rules table AgentBot + Captain integration: - agent_bot_listener.go: add captainConvSvc field + SetCaptainConversationService method. In HandleEvent loop, check bot.BotType == captain and route to CaptainConversationService.BuildConversationResponseByAccount instead of webhook push. Extract assistant_id from bot.Config JSONB, extract conversation_id from event data. - bootstrap.go: inject captainConversationService into agentBotListener CaptainConversationService improvement: - generateConversationResponse: use assistant config for system prompt, model name, and temperature instead of hardcoded values Verified: - go build ./... passes - go vet passes on all internal packages - go test passes (service + repository + llm, SQLite mode) - Auto-reply CRUD: create/get/update/delete all work - Auto-reply evaluate: correctly matches hello → should_reply=true, correctly rejects non-matching message - Existing routes unaffected (assistants, RAG, conversation respond) - Migration 000048 creates captain_auto_reply_rules table successfully
174 lines
6.1 KiB
Go
174 lines
6.1 KiB
Go
package v1
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"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"
|
|
)
|
|
|
|
// AutoReplyRuleHandler handles Captain auto-reply rule REST API endpoints.
|
|
// Reference: M12 PRD §Captain AI — Auto-Reply Rules
|
|
type AutoReplyRuleHandler struct {
|
|
svc *service.AutoReplyRuleService
|
|
}
|
|
|
|
// NewAutoReplyRuleHandler creates a new AutoReplyRuleHandler.
|
|
func NewAutoReplyRuleHandler(svc *service.AutoReplyRuleService) *AutoReplyRuleHandler {
|
|
return &AutoReplyRuleHandler{svc: svc}
|
|
}
|
|
|
|
// Create creates a new auto-reply rule for an assistant.
|
|
// POST /api/v1/accounts/:account_id/captain/assistants/:assistant_id/auto_reply_rules
|
|
func (h *AutoReplyRuleHandler) Create(c *gin.Context) {
|
|
accountID, err := strconv.ParseUint(c.Param("account_id"), 10, 64)
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, "invalid_account_id", "Invalid account ID")
|
|
return
|
|
}
|
|
|
|
var req service.CreateAutoReplyRuleRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, "invalid_request", err.Error())
|
|
return
|
|
}
|
|
|
|
result, err := h.svc.CreateRule(c.Request.Context(), uint(accountID), &req)
|
|
if err != nil {
|
|
applogger.L().Errorf("AutoReplyRuleHandler.Create failed: %v", err)
|
|
response.AbortWithStatusError(c, http.StatusUnprocessableEntity, "create_failed", "Failed to create auto-reply rule")
|
|
return
|
|
}
|
|
|
|
response.Created(c, result)
|
|
}
|
|
|
|
// Get retrieves a single auto-reply rule.
|
|
// GET /api/v1/accounts/:account_id/captain/auto_reply_rules/:rule_id
|
|
func (h *AutoReplyRuleHandler) Get(c *gin.Context) {
|
|
accountID, err := strconv.ParseUint(c.Param("account_id"), 10, 64)
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, "invalid_account_id", "Invalid account ID")
|
|
return
|
|
}
|
|
|
|
ruleID, err := strconv.ParseUint(c.Param("rule_id"), 10, 64)
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, "invalid_rule_id", "Invalid rule ID")
|
|
return
|
|
}
|
|
|
|
result, err := h.svc.GetRule(c.Request.Context(), uint(accountID), uint(ruleID))
|
|
if err != nil {
|
|
applogger.L().Errorf("AutoReplyRuleHandler.Get failed: %v", err)
|
|
response.AbortWithStatusError(c, http.StatusNotFound, "not_found", "Auto-reply rule not found")
|
|
return
|
|
}
|
|
|
|
response.OK(c, result)
|
|
}
|
|
|
|
// Update updates an existing auto-reply rule.
|
|
// PUT /api/v1/accounts/:account_id/captain/auto_reply_rules/:rule_id
|
|
func (h *AutoReplyRuleHandler) Update(c *gin.Context) {
|
|
accountID, err := strconv.ParseUint(c.Param("account_id"), 10, 64)
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, "invalid_account_id", "Invalid account ID")
|
|
return
|
|
}
|
|
|
|
ruleID, err := strconv.ParseUint(c.Param("rule_id"), 10, 64)
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, "invalid_rule_id", "Invalid rule ID")
|
|
return
|
|
}
|
|
|
|
var req service.UpdateAutoReplyRuleRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, "invalid_request", err.Error())
|
|
return
|
|
}
|
|
|
|
result, err := h.svc.UpdateRule(c.Request.Context(), uint(accountID), uint(ruleID), &req)
|
|
if err != nil {
|
|
applogger.L().Errorf("AutoReplyRuleHandler.Update failed: %v", err)
|
|
response.AbortWithStatusError(c, http.StatusUnprocessableEntity, "update_failed", "Failed to update auto-reply rule")
|
|
return
|
|
}
|
|
|
|
response.OK(c, result)
|
|
}
|
|
|
|
// Delete removes an auto-reply rule.
|
|
// DELETE /api/v1/accounts/:account_id/captain/auto_reply_rules/:rule_id
|
|
func (h *AutoReplyRuleHandler) Delete(c *gin.Context) {
|
|
accountID, err := strconv.ParseUint(c.Param("account_id"), 10, 64)
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, "invalid_account_id", "Invalid account ID")
|
|
return
|
|
}
|
|
|
|
ruleID, err := strconv.ParseUint(c.Param("rule_id"), 10, 64)
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, "invalid_rule_id", "Invalid rule ID")
|
|
return
|
|
}
|
|
|
|
if err := h.svc.DeleteRule(c.Request.Context(), uint(accountID), uint(ruleID)); err != nil {
|
|
applogger.L().Errorf("AutoReplyRuleHandler.Delete failed: %v", err)
|
|
response.AbortWithStatusError(c, http.StatusUnprocessableEntity, "delete_failed", "Failed to delete auto-reply rule")
|
|
return
|
|
}
|
|
|
|
response.NoContent(c)
|
|
}
|
|
|
|
// List returns auto-reply rules for an account, optionally filtered by assistant.
|
|
// GET /api/v1/accounts/:account_id/captain/auto_reply_rules
|
|
func (h *AutoReplyRuleHandler) List(c *gin.Context) {
|
|
accountID, err := strconv.ParseUint(c.Param("account_id"), 10, 64)
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, "invalid_account_id", "Invalid account ID")
|
|
return
|
|
}
|
|
|
|
p := pagination.Parse(c)
|
|
results, total, err := h.svc.ListRules(c.Request.Context(), uint(accountID), p.Offset, p.PerPage)
|
|
if err != nil {
|
|
applogger.L().Errorf("AutoReplyRuleHandler.List failed: %v", err)
|
|
response.AbortWithStatusError(c, http.StatusInternalServerError, "list_failed", "Failed to list auto-reply rules")
|
|
return
|
|
}
|
|
|
|
response.OKWithMeta(c, results, p.Page, p.PerPage, total)
|
|
}
|
|
|
|
// Evaluate checks rules against a conversation context and returns the best match.
|
|
// POST /api/v1/accounts/:account_id/captain/auto_reply_rules/evaluate
|
|
func (h *AutoReplyRuleHandler) Evaluate(c *gin.Context) {
|
|
accountID, _ := strconv.ParseUint(c.Param("account_id"), 10, 64)
|
|
_ = uint(accountID) // scoped by account_id path param; will be used when EvaluateRules gains account-scoping
|
|
|
|
var evalCtx service.AutoReplyEvaluationContext
|
|
if err := c.ShouldBindJSON(&evalCtx); err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, "invalid_request", err.Error())
|
|
return
|
|
}
|
|
|
|
// Override account_id from path param (authoritative source)
|
|
evalCtx.AccountID = uint(accountID)
|
|
|
|
result, err := h.svc.EvaluateRules(c.Request.Context(), &evalCtx)
|
|
if err != nil {
|
|
applogger.L().Errorf("AutoReplyRuleHandler.Evaluate failed: %v", err)
|
|
response.AbortWithStatusError(c, http.StatusInternalServerError, "evaluate_failed", "Failed to evaluate auto-reply rules")
|
|
return
|
|
}
|
|
|
|
response.OK(c, result)
|
|
}
|