feat(captain): align task payload persistence
This commit is contained in:
@@ -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{}
|
||||
|
||||
Reference in New Issue
Block a user