Phase 2: AutoReplyRule integration + AgentBot Captain type

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
This commit is contained in:
2026-07-08 15:38:08 +08:00
parent 42b8b6c7f9
commit b769b9a3e4
9 changed files with 440 additions and 17 deletions
@@ -25,7 +25,7 @@ func NewAutoReplyRuleHandler(svc *service.AutoReplyRuleService) *AutoReplyRuleHa
// 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("id"), 10, 64)
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
@@ -50,7 +50,7 @@ func (h *AutoReplyRuleHandler) Create(c *gin.Context) {
// 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("id"), 10, 64)
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
@@ -75,7 +75,7 @@ func (h *AutoReplyRuleHandler) Get(c *gin.Context) {
// 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("id"), 10, 64)
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
@@ -106,7 +106,7 @@ func (h *AutoReplyRuleHandler) Update(c *gin.Context) {
// 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("id"), 10, 64)
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
@@ -130,7 +130,7 @@ func (h *AutoReplyRuleHandler) Delete(c *gin.Context) {
// 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("id"), 10, 64)
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
@@ -150,7 +150,7 @@ func (h *AutoReplyRuleHandler) List(c *gin.Context) {
// 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("id"), 10, 64)
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
@@ -159,6 +159,9 @@ func (h *AutoReplyRuleHandler) Evaluate(c *gin.Context) {
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)