feat(captain): align playground fallback

This commit is contained in:
2026-06-05 13:09:23 +08:00
parent e1b376cd76
commit 67d182b132
5 changed files with 319 additions and 31 deletions
@@ -301,8 +301,13 @@ func (h *CaptainAssistantHandler) Tools(c *gin.Context) {
}
// GenerateResponse generates an AI response via RAG.
// POST /api/v1/accounts/:account_id/captain_assistants/:id/generate_response
// POST /api/v1/accounts/:account_id/captain/assistants/:assistant_id/playground
func (h *CaptainAssistantHandler) GenerateResponse(c *gin.Context) {
accountID := parseAccountIDParam(c)
if accountID == 0 {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
return
}
id, err := parseUintAnyParam(c, "assistant_id", "id")
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid id")
@@ -311,39 +316,52 @@ func (h *CaptainAssistantHandler) GenerateResponse(c *gin.Context) {
var req struct {
Assistant struct {
MessageContent string `json:"message_content"`
Query string `json:"query"`
MessageContent string `json:"message_content"`
Query string `json:"query"`
MessageHistory []service.PlaygroundMessage `json:"message_history"`
} `json:"assistant"`
MessageContent string `json:"message_content"`
Query string `json:"query"`
MessageContent string `json:"message_content"`
Query string `json:"query"`
MessageHistory []service.PlaygroundMessage `json:"message_history"`
}
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
messageContent := req.MessageContent
if messageContent == "" {
messageContent = req.Query
}
if query == "" {
query = req.Assistant.Query
if messageContent == "" {
messageContent = req.Assistant.MessageContent
}
if query == "" {
query = req.Assistant.MessageContent
if messageContent == "" {
messageContent = req.Assistant.Query
}
if query == "" {
if messageContent == "" {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, "message_content is required")
return
}
messageHistory := req.MessageHistory
if len(messageHistory) == 0 && len(req.Assistant.MessageHistory) > 0 {
messageHistory = req.Assistant.MessageHistory
}
result, err := h.svc.GenerateResponse(c.Request.Context(), id, query)
result, err := h.svc.GeneratePlaygroundResponse(c.Request.Context(), accountID, id, service.PlaygroundRequest{
MessageContent: messageContent,
MessageHistory: messageHistory,
})
if err != nil {
applogger.L().Errorf("GenerateResponse: %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 generate response")
return
}
c.JSON(http.StatusOK, gin.H{"response": result})
c.JSON(http.StatusOK, result)
}
func bindCaptainAssistantPayload(c *gin.Context, dst any) error {
@@ -2,6 +2,7 @@ package v1
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
@@ -10,6 +11,7 @@ import (
"testing"
"github.com/gin-gonic/gin"
"github.com/gochat/gochat/internal/llm"
"github.com/gochat/gochat/internal/model"
"github.com/gochat/gochat/internal/repository"
"github.com/gochat/gochat/internal/service"
@@ -51,12 +53,43 @@ func setupCaptainAssistantHandlerTest(t *testing.T) (*gin.Engine, *gorm.DB) {
assistants.GET("/:assistant_id", handler.Get)
assistants.PUT("/:assistant_id", handler.Update)
assistants.DELETE("/:assistant_id", handler.Delete)
assistants.POST("/:assistant_id/playground", handler.GenerateResponse)
assistants.GET("/:assistant_id/inboxes", handler.ListInboxes)
assistants.POST("/:assistant_id/inboxes", handler.AssociateInbox)
assistants.DELETE("/:assistant_id/inboxes/:inbox_id", handler.DissociateInbox)
return router, db
}
func setupCaptainAssistantHandlerTestWithProvider(t *testing.T, provider llm.Provider) (*gin.Engine, *gorm.DB) {
t.Helper()
gin.SetMode(gin.TestMode)
dbName := fmt.Sprintf("file:%s-provider?mode=memory&cache=private", t.Name())
db, err := gorm.Open(sqlite.Open(dbName), &gorm.Config{})
require.NoError(t, err)
require.NoError(t, db.AutoMigrate(
&model.Account{},
&model.Inbox{},
&model.CaptainAssistant{},
&model.CaptainInbox{},
))
t.Cleanup(func() {
sqlDB, _ := db.DB()
sqlDB.Close()
})
assistantRepo := repository.NewCaptainAssistantRepo(db)
inboxRepo := repository.NewCaptainInboxRepo(db)
documentRepo := repository.NewCaptainDocumentRepo(db)
responseRepo := repository.NewCaptainAssistantResponseRepo(db)
svc := service.NewCaptainAssistantService(assistantRepo, inboxRepo, documentRepo, responseRepo, provider)
handler := NewCaptainAssistantHandler(svc)
router := gin.New()
assistants := router.Group("/api/v1/accounts/:account_id/captain/assistants")
assistants.POST("/:assistant_id/playground", handler.GenerateResponse)
return router, db
}
func seedCaptainAssistantAccount(t *testing.T, db *gorm.DB, name string) *model.Account {
t.Helper()
account := &model.Account{Name: name, Locale: "en", Active: true}
@@ -182,3 +215,97 @@ func TestCaptainAssistantHandler_AccountScopedShowAndInboxBinding(t *testing.T)
w = captainAssistantJSONRequest(t, router, http.MethodDelete, fmt.Sprintf("%s/%d/inboxes/%d", basePath, assistant.ID, inbox.ID), nil)
assert.Equal(t, http.StatusNoContent, w.Code)
}
func TestCaptainAssistantHandler_PlaygroundLegacyNoLLMFallback(t *testing.T) {
router, db := setupCaptainAssistantHandlerTest(t)
account := seedCaptainAssistantAccount(t, db, "Captain Org")
assistant := &model.CaptainAssistant{AccountID: account.ID, Name: "Fin", Description: "Support", Config: json.RawMessage(`{"model":"gpt-test"}`), Status: model.AssistantStatusActive}
require.NoError(t, db.Create(assistant).Error)
body := map[string]any{
"message_content": "Hello assistant",
"message_history": []map[string]any{
{"role": "user", "content": "Previous message"},
{"role": "assistant", "content": "Previous response", "agent_name": "billing_scenario"},
},
}
w := captainAssistantJSONRequest(t, router, http.MethodPost, fmt.Sprintf("/api/v1/accounts/%d/captain/assistants/%d/playground", account.ID, assistant.ID), body)
assert.Equal(t, http.StatusOK, w.Code)
var payload map[string]any
require.NoError(t, json.Unmarshal(w.Body.Bytes(), &payload))
assert.NotContains(t, payload, "success")
assert.NotContains(t, payload, "data")
assert.Equal(t, "Captain assistant response generation is not configured for this account.", payload["content"])
assert.NotContains(t, payload, "response")
}
func TestCaptainAssistantHandler_PlaygroundDefaultsHistoryAndScopesAccount(t *testing.T) {
router, db := setupCaptainAssistantHandlerTest(t)
account := seedCaptainAssistantAccount(t, db, "Account One")
otherAccount := seedCaptainAssistantAccount(t, db, "Account Two")
assistant := &model.CaptainAssistant{AccountID: account.ID, Name: "Fin", Description: "Support", Config: json.RawMessage(`{}`), Status: model.AssistantStatusActive}
require.NoError(t, db.Create(assistant).Error)
body := map[string]any{"message_content": "Hello assistant"}
w := captainAssistantJSONRequest(t, router, http.MethodPost, fmt.Sprintf("/api/v1/accounts/%d/captain/assistants/%d/playground", account.ID, assistant.ID), body)
assert.Equal(t, http.StatusOK, w.Code)
w = captainAssistantJSONRequest(t, router, http.MethodPost, fmt.Sprintf("/api/v1/accounts/%d/captain/assistants/%d/playground", otherAccount.ID, assistant.ID), body)
assert.Equal(t, http.StatusNotFound, w.Code)
}
func TestCaptainAssistantHandler_PlaygroundV2AppendsCurrentMessageOnce(t *testing.T) {
provider := &captainPlaygroundFakeProvider{content: "Assistant response"}
router, db := setupCaptainAssistantHandlerTestWithProvider(t, provider)
account := seedCaptainAssistantAccount(t, db, "Captain Org")
account.FeatureFlags = `{"captain_integration_v2":true}`
require.NoError(t, db.Save(account).Error)
assistant := &model.CaptainAssistant{AccountID: account.ID, Name: "Fin", Description: "Support", Config: json.RawMessage(`{"model":"gpt-test","temperature":0.2}`), Status: model.AssistantStatusActive}
require.NoError(t, db.Create(assistant).Error)
body := map[string]any{
"message_content": "Hello assistant",
"message_history": []map[string]any{
{"role": "user", "content": "Previous message"},
{"role": "assistant", "content": "Previous response", "agent_name": "billing_scenario"},
},
}
w := captainAssistantJSONRequest(t, router, http.MethodPost, fmt.Sprintf("/api/v1/accounts/%d/captain/assistants/%d/playground", account.ID, assistant.ID), body)
assert.Equal(t, http.StatusOK, w.Code)
var payload map[string]any
require.NoError(t, json.Unmarshal(w.Body.Bytes(), &payload))
assert.Equal(t, "Assistant response", payload["response"])
assert.NotContains(t, payload, "content")
require.Len(t, provider.lastRequest.Messages, 4)
assert.Equal(t, "Previous message", provider.lastRequest.Messages[1].Content)
assert.Equal(t, "Previous response", provider.lastRequest.Messages[2].Content)
assert.Equal(t, "Hello assistant", provider.lastRequest.Messages[3].Content)
body = map[string]any{
"message_content": "Hello assistant",
"message_history": []map[string]any{{"role": "user", "content": "Hello assistant"}},
}
w = captainAssistantJSONRequest(t, router, http.MethodPost, fmt.Sprintf("/api/v1/accounts/%d/captain/assistants/%d/playground", account.ID, assistant.ID), body)
assert.Equal(t, http.StatusOK, w.Code)
require.Len(t, provider.lastRequest.Messages, 2)
assert.Equal(t, "Hello assistant", provider.lastRequest.Messages[1].Content)
}
type captainPlaygroundFakeProvider struct {
content string
lastRequest llm.ChatRequest
}
func (p *captainPlaygroundFakeProvider) ChatCompletion(ctx context.Context, req llm.ChatRequest) (*llm.ChatResponse, error) {
p.lastRequest = req
return &llm.ChatResponse{Choices: []llm.ChatChoice{{Message: llm.ChatMessage{Role: "assistant", Content: p.content}}}}, nil
}
func (p *captainPlaygroundFakeProvider) CreateEmbedding(ctx context.Context, req llm.EmbeddingRequest) (*llm.EmbeddingResponse, error) {
return &llm.EmbeddingResponse{}, nil
}
func (p *captainPlaygroundFakeProvider) ChatCompletionStream(ctx context.Context, req llm.ChatRequest, onChunk func(llm.StreamChunk) error) error {
return nil
}
@@ -36,6 +36,14 @@ func (r *CaptainAssistantRepo) GetByAccountAndID(ctx context.Context, accountID,
return &assistant, nil
}
func (r *CaptainAssistantRepo) GetAccountFeatureFlags(ctx context.Context, accountID uint) (string, error) {
var account model.Account
if err := r.db.WithContext(ctx).Select("feature_flags").First(&account, accountID).Error; err != nil {
return "", err
}
return account.FeatureFlags, nil
}
func (r *CaptainAssistantRepo) Update(ctx context.Context, assistant *model.CaptainAssistant) error {
return r.db.WithContext(ctx).Save(assistant).Error
}
@@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"strings"
"github.com/gochat/gochat/internal/llm"
"github.com/gochat/gochat/internal/model"
@@ -59,6 +60,17 @@ type UpdateAssistantRequest struct {
Status string `json:"status"`
}
type PlaygroundMessage struct {
Role string `json:"role"`
Content string `json:"content"`
AgentName string `json:"agent_name,omitempty"`
}
type PlaygroundRequest struct {
MessageContent string `json:"message_content"`
MessageHistory []PlaygroundMessage `json:"message_history"`
}
// --- CRUD Operations ---
// Create creates a new CaptainAssistant.
@@ -309,6 +321,118 @@ func (s *CaptainAssistantService) GenerateResponse(ctx context.Context, assistan
return resp.Choices[0].Message.Content, nil
}
const captainPlaygroundFallbackMessage = "Captain assistant response generation is not configured for this account."
// GeneratePlaygroundResponse follows Chatwoot Captain assistant playground behavior.
func (s *CaptainAssistantService) GeneratePlaygroundResponse(ctx context.Context, accountID, assistantID uint, req PlaygroundRequest) (map[string]any, error) {
assistant, err := s.assistantRepo.GetByAccountAndID(ctx, accountID, assistantID)
if err != nil {
return nil, fmt.Errorf("assistant not found: %w", err)
}
if s.captainV2Enabled(ctx, accountID) {
history := playgroundMessageHistory(req.MessageHistory, req.MessageContent)
content, err := s.generatePlaygroundLLMResponse(ctx, assistant, history)
if err != nil {
return nil, err
}
return map[string]any{"response": content}, nil
}
history := append([]PlaygroundMessage{}, req.MessageHistory...)
content, err := s.generatePlaygroundLLMResponse(ctx, assistant, appendAdditionalPlaygroundMessage(history, req.MessageContent))
if err != nil {
return nil, err
}
return map[string]any{"content": content}, nil
}
func (s *CaptainAssistantService) generatePlaygroundLLMResponse(ctx context.Context, assistant *model.CaptainAssistant, history []PlaygroundMessage) (string, error) {
if s.llmProvider == nil {
return captainPlaygroundFallbackMessage, nil
}
cfg, _ := assistant.GetConfig()
messages := []llm.ChatMessage{{Role: "system", Content: buildSystemPrompt(assistant, cfg)}}
for _, message := range history {
if message.Role == "" || message.Content == "" {
continue
}
messages = append(messages, llm.ChatMessage{Role: message.Role, Content: message.Content})
}
resp, err := s.llmProvider.ChatCompletion(ctx, llm.ChatRequest{
Model: cfg.Model,
Messages: messages,
Temperature: cfg.Temperature,
MaxTokens: 1024,
})
if err != nil {
applogger.L().Errorf("GeneratePlaygroundResponse LLM call: %v", err)
return "", fmt.Errorf("llm generation failed: %w", err)
}
if len(resp.Choices) == 0 {
return "", fmt.Errorf("no response from LLM")
}
return resp.Choices[0].Message.Content, nil
}
func (s *CaptainAssistantService) captainV2Enabled(ctx context.Context, accountID uint) bool {
flags, err := s.assistantRepo.GetAccountFeatureFlags(ctx, accountID)
if err != nil {
return false
}
return featureFlagStringEnabled(flags, "captain_integration_v2")
}
func featureFlagStringEnabled(raw, flag string) bool {
raw = strings.TrimSpace(raw)
if raw == "" {
return false
}
var objectFlags map[string]bool
if err := json.Unmarshal([]byte(raw), &objectFlags); err == nil {
return objectFlags[flag]
}
var arrayFlags []string
if err := json.Unmarshal([]byte(raw), &arrayFlags); err == nil {
for _, item := range arrayFlags {
if item == flag {
return true
}
}
return false
}
for _, item := range strings.Split(raw, ",") {
if strings.TrimSpace(item) == flag {
return true
}
}
return false
}
func playgroundMessageHistory(history []PlaygroundMessage, current string) []PlaygroundMessage {
result := append([]PlaygroundMessage{}, history...)
if strings.TrimSpace(current) == "" {
return result
}
currentMessage := PlaygroundMessage{Role: "user", Content: current}
if len(result) > 0 {
last := result[len(result)-1]
if last.Role == currentMessage.Role && last.Content == currentMessage.Content && last.AgentName == "" {
return result
}
}
return append(result, currentMessage)
}
func appendAdditionalPlaygroundMessage(history []PlaygroundMessage, current string) []PlaygroundMessage {
if strings.TrimSpace(current) == "" {
return history
}
return append(history, PlaygroundMessage{Role: "user", Content: current})
}
// buildSystemPrompt constructs the system prompt from assistant config and guidelines.
func buildSystemPrompt(assistant *model.CaptainAssistant, cfg *model.AssistantConfig) string {
prompt := fmt.Sprintf("You are %s, an AI assistant.", assistant.Name)