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
+27
View File
@@ -2,6 +2,9 @@ package model
import (
"encoding/json"
"fmt"
"gorm.io/gorm"
)
// --- Copilot Thread Model ---
@@ -62,6 +65,30 @@ type CopilotMessage struct {
func (CopilotMessage) TableName() string { return "copilot_messages" }
// BeforeSave mirrors Chatwoot's CopilotMessage JSON key validation so
// reloadable tool-call state stays compatible with the reused frontend.
func (m *CopilotMessage) BeforeSave(tx *gorm.DB) error {
if len(m.Message) == 0 {
return nil
}
var msgMap map[string]interface{}
if err := json.Unmarshal(m.Message, &msgMap); err != nil {
return err
}
allowed := map[string]bool{
"content": true,
"reasoning": true,
"function_name": true,
"reply_suggestion": true,
}
for key := range msgMap {
if !allowed[key] {
return fmt.Errorf("message contains invalid attribute: %s", key)
}
}
return nil
}
// GetMessageContent extracts the "content" field from the JSONB message.
func (m *CopilotMessage) GetMessageContent() string {
var msgMap map[string]interface{}