Files
gochat/backend/internal/handler/api/v1/captain_conversation_handler.go
T
rogee 42b8b6c7f9 Activate Captain AI features: config, RAG routes, conversation handler
Phase 1 of AI_FEATURE_ROADMAP.md:

1. config.yaml: Add captain configuration section (llm_provider, llm_model,
   llm_api_key, llm_base_url, embedding_model, embedding_dims, max_tokens,
   temperature). Secrets injected via GOCHAT_CAPTAIN_LLM_API_KEY env var.

2. RAG route registration:
   - bootstrap.go: instantiate RAGService + RAGHandler, add to Handlers
   - router.go: register POST /captain/rag/query + /captain/rag/index/:response_id
   - rag_handler.go: fix account_id param name (was "id", route uses "account_id")

3. CaptainConversationService activation:
   - Remove "_ = captainConversationService" ignore in bootstrap.go
   - Create CaptainConversationHandler with BuildResponse endpoint
   - Register POST /captain/conversations/:conversation_id/respond route
   - Fix account_id param name in handler

Verified:
- go build ./... passes
- go vet passes on all modified packages
- go test passes (llm + service packages)
- Server starts with captain config loaded
- RAG query/index routes return proper handler responses (not 404)
- Conversation respond route returns proper skip for non-pending conversations
- Existing captain routes unaffected (assistants list still works)
2026-07-08 15:38:08 +08:00

68 lines
2.4 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/response"
)
// CaptainConversationHandler handles Captain conversation-level AI auto-response.
// Reference: M12 PRD §Captain AI — Conversation Auto-Response (handoff mode)
//
// This handler exposes the CaptainConversationService, which generates AI
// responses for conversations bound to a Captain Assistant. When the AI
// determines a handoff is needed, the conversation is transferred to a human
// agent instead of sending an AI-generated reply.
type CaptainConversationHandler struct {
svc *service.CaptainConversationService
}
// NewCaptainConversationHandler creates a new CaptainConversationHandler.
func NewCaptainConversationHandler(svc *service.CaptainConversationService) *CaptainConversationHandler {
return &CaptainConversationHandler{svc: svc}
}
// BuildResponse triggers AI response generation for a conversation.
// The assistant must be bound to the conversation's inbox via CaptainInbox.
//
// POST /api/v1/accounts/:account_id/captain/conversations/:conversation_id/respond
func (h *CaptainConversationHandler) BuildResponse(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
}
conversationID, err := strconv.ParseUint(c.Param("conversation_id"), 10, 64)
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, "invalid_conversation_id", "Invalid conversation ID")
return
}
var req struct {
AssistantID uint `json:"assistant_id" binding:"required"`
}
if err := c.ShouldBindJSON(&req); err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, "invalid_request", err.Error())
return
}
msg, err := h.svc.BuildConversationResponseByAccount(c.Request.Context(), uint(accountID), uint(conversationID), req.AssistantID)
if err != nil {
applogger.L().Errorf("CaptainConversationHandler.BuildResponse: %v", err)
response.AbortWithStatusError(c, http.StatusInternalServerError, "response_failed", "Failed to generate AI response")
return
}
if msg == nil {
response.OK(c, map[string]any{"status": "skipped", "message": "Conversation not eligible for AI response"})
return
}
response.OK(c, map[string]any{"status": "sent", "message_id": msg.ID})
}