feat(captain): align task payload persistence

This commit is contained in:
2026-06-05 13:38:55 +08:00
parent 3263ed9284
commit 13cb750a2a
11 changed files with 822 additions and 89 deletions
@@ -1,6 +1,7 @@
package v1
import (
"bytes"
"context"
"encoding/json"
"fmt"
@@ -47,12 +48,14 @@ func setupTaskExtendedHandlerTest(t *testing.T, mockLLM llm.Provider) (*CaptainT
&model.Message{},
&model.Inbox{},
&model.Contact{},
&model.CopilotSuggestionMessage{},
))
convRepo := repository.NewConversationRepo(db)
msgRepo := repository.NewMessageRepo(db)
assistantRepo := repository.NewCaptainAssistantRepo(db)
prefRepo := repository.NewCaptainPreferenceRepo(db)
svc := service.NewCaptainTaskExtendedService(convRepo, msgRepo, assistantRepo, prefRepo, mockLLM)
suggestionRepo := repository.NewCopilotSuggestionRepo(db)
svc := service.NewCaptainTaskExtendedService(convRepo, msgRepo, assistantRepo, prefRepo, mockLLM, suggestionRepo)
handler := NewCaptainTaskExtendedHandler(svc)
return handler, db
}
@@ -103,6 +106,40 @@ func TestCaptainTaskExtendedHandler_LabelSuggestion_MissingConversationIDs(t *te
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestCaptainTaskExtendedHandler_LabelSuggestion_ChatwootPostRawPayload(t *testing.T) {
mockLLM := &mockTaskExtHandlerLLM{
response: &llm.ChatResponse{Choices: []llm.ChatChoice{{Message: llm.ChatMessage{Role: "assistant", Content: "billing, urgent"}}}},
}
handler, db := setupTaskExtendedHandlerTest(t, mockLLM)
displayID := uint(77)
inbox := &model.Inbox{AccountID: 1, Name: "Inbox", ChannelType: "web_widget"}
require.NoError(t, db.Create(inbox).Error)
conv := &model.Conversation{AccountID: 1, InboxID: inbox.ID, DisplayID: &displayID, Status: "open", ChannelType: "web_widget", Channel: "web_widget"}
require.NoError(t, db.Create(conv).Error)
msg := &model.Message{ConversationID: conv.ID, AccountID: 1, InboxID: inbox.ID, SenderType: "contact", Content: "billing help", ContentType: "text", MessageType: "incoming"}
require.NoError(t, db.Create(msg).Error)
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Params = gin.Params{{Key: "id", Value: "1"}}
c.Request = httptest.NewRequest(http.MethodPost, "/api/v1/accounts/1/captain/tasks/label_suggestion", bytes.NewReader([]byte(`{"conversation_display_id":77}`)))
c.Request.Header.Set("Content-Type", "application/json")
handler.LabelSuggestion(c)
require.Equal(t, http.StatusOK, w.Code)
var resp map[string]interface{}
require.NoError(t, json.Unmarshal(w.Body.Bytes(), &resp))
assert.Equal(t, "billing, urgent", resp["message"])
assert.NotContains(t, resp, "success")
require.Contains(t, resp, "follow_up_context")
var stored []model.CopilotSuggestionMessage
require.NoError(t, db.Find(&stored).Error)
require.Len(t, stored, 1)
assert.Equal(t, "billing, urgent", stored[0].Content)
}
func TestCaptainTaskExtendedHandler_FollowUp(t *testing.T) {
mockLLM := &mockTaskExtHandlerLLM{
response: &llm.ChatResponse{
@@ -143,3 +180,42 @@ func TestCaptainTaskExtendedHandler_FollowUp_MissingConversationIDs(t *testing.T
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestCaptainTaskExtendedHandler_FollowUp_ChatwootPostUpdatesContext(t *testing.T) {
mockLLM := &mockTaskExtHandlerLLM{
response: &llm.ChatResponse{Choices: []llm.ChatChoice{{Message: llm.ChatMessage{Role: "assistant", Content: "Refined answer"}}}},
}
handler, db := setupTaskExtendedHandlerTest(t, mockLLM)
displayID := uint(88)
inbox := &model.Inbox{AccountID: 1, Name: "Inbox", ChannelType: "web_widget"}
require.NoError(t, db.Create(inbox).Error)
conv := &model.Conversation{AccountID: 1, InboxID: inbox.ID, DisplayID: &displayID, Status: "open", ChannelType: "web_widget", Channel: "web_widget"}
require.NoError(t, db.Create(conv).Error)
body := []byte(`{
"conversation_display_id": 88,
"message": "Make it warmer",
"follow_up_context": {
"event_name": "professional",
"original_context": "Original draft",
"last_response": "Previous answer",
"conversation_history": []
}
}`)
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Params = gin.Params{{Key: "id", Value: "1"}}
c.Request = httptest.NewRequest(http.MethodPost, "/api/v1/accounts/1/captain/tasks/follow_up", bytes.NewReader(body))
c.Request.Header.Set("Content-Type", "application/json")
handler.FollowUp(c)
require.Equal(t, http.StatusOK, w.Code)
var resp map[string]interface{}
require.NoError(t, json.Unmarshal(w.Body.Bytes(), &resp))
assert.Equal(t, "Refined answer", resp["message"])
ctx := resp["follow_up_context"].(map[string]interface{})
assert.Equal(t, "Refined answer", ctx["last_response"])
history := ctx["conversation_history"].([]interface{})
require.Len(t, history, 2)
}