fix: 补充提交 memory 模块和 tts/userdir 文件

This commit is contained in:
2026-04-27 07:15:08 +08:00
parent f6332fbaaf
commit 662c4e05a4
8 changed files with 1651 additions and 0 deletions

View File

@@ -0,0 +1,98 @@
package memory
import (
"encoding/json"
"time"
)
type Session struct {
ID int64 `json:"id"`
UUID string `json:"uuid"`
Summary string `json:"summary"`
SummaryEmbedding []byte `json:"-"`
ChatIDs []int64 `json:"chat_ids"`
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
}
type Chat struct {
ID int64 `json:"id"`
SessionID int64 `json:"session_id"`
UserInput string `json:"user_input"`
AIReplies []string `json:"ai_replies"`
Summary string `json:"summary"`
SummaryEmbedding []byte `json:"-"`
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
}
func NewSession(uuid string) *Session {
now := time.Now().Unix()
return &Session{
UUID: uuid,
ChatIDs: []int64{},
CreatedAt: now,
UpdatedAt: now,
}
}
func NewChat(sessionID int64, userInput string) *Chat {
now := time.Now().Unix()
return &Chat{
SessionID: sessionID,
UserInput: userInput,
AIReplies: []string{},
CreatedAt: now,
UpdatedAt: now,
}
}
func (s *Session) AddChatID(chatID int64) {
s.ChatIDs = append(s.ChatIDs, chatID)
s.UpdatedAt = time.Now().Unix()
}
func (s *Session) GetChatIDsJSON() (string, error) {
data, err := json.Marshal(s.ChatIDs)
if err != nil {
return "", err
}
return string(data), nil
}
func (s *Session) SetChatIDsFromJSON(data string) error {
if data == "" {
s.ChatIDs = []int64{}
return nil
}
return json.Unmarshal([]byte(data), &s.ChatIDs)
}
func (c *Chat) AddAIReply(reply string) {
c.AIReplies = append(c.AIReplies, reply)
c.UpdatedAt = time.Now().Unix()
}
func (c *Chat) GetAIRepliesJSON() (string, error) {
data, err := json.Marshal(c.AIReplies)
if err != nil {
return "", err
}
return string(data), nil
}
func (c *Chat) SetAIRepliesFromJSON(data string) error {
if data == "" {
c.AIReplies = []string{}
return nil
}
return json.Unmarshal([]byte(data), &c.AIReplies)
}
func GenerateSummary(userInput, aiReply string) string {
fullText := userInput + "\n" + aiReply
if len(fullText) > 200 {
fullText = fullText[:200]
}
return fullText
}