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
}