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,110 @@
package memory
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"time"
"github.com/hxclaw/hxclaw/cmd/hxclaw/internal"
)
type ExportDocument struct {
Version int `json:"version"`
ExportedAt string `json:"exported_at"`
Sessions []*SessionData `json:"sessions"`
}
type SessionData struct {
ID int64 `json:"id"`
UUID string `json:"uuid"`
Summary string `json:"summary"`
ChatIDs []int64 `json:"chat_ids"`
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
Chats []*Chat `json:"chats"`
}
func ExportIfNeeded() {
memoryCfg := internal.GetProjectConfig().Memory
if !memoryCfg.Enabled || !memoryCfg.AutoExport {
return
}
db := GetDB()
if db == nil {
return
}
exportPath := filepath.Join(internal.GetConfigDir(), "export-data.json")
if err := ExportToFile(exportPath); err != nil {
fmt.Printf("警告:导出数据失败: %v\n", err)
return
}
fmt.Printf("数据已导出: %s\n", exportPath)
}
func ExportToFile(filename string) error {
db := GetDB()
if db == nil {
return fmt.Errorf("数据库未初始化")
}
sessions, err := db.GetAllSessions()
if err != nil {
return fmt.Errorf("查询会话失败: %w", err)
}
var doc ExportDocument
doc.Version = 1
doc.ExportedAt = time.Now().Format(time.RFC3339)
if _, err := os.Stat(filename); err == nil {
data, readErr := os.ReadFile(filename)
if readErr == nil {
json.Unmarshal(data, &doc)
}
}
sessionMap := make(map[string]*SessionData)
for i := range doc.Sessions {
sessionMap[doc.Sessions[i].UUID] = doc.Sessions[i]
}
for _, s := range sessions {
if existing, ok := sessionMap[s.UUID]; ok {
existing.Summary = s.Summary
existing.ChatIDs = s.ChatIDs
existing.UpdatedAt = s.UpdatedAt
} else {
sessionData := &SessionData{
ID: s.ID,
UUID: s.UUID,
Summary: s.Summary,
ChatIDs: s.ChatIDs,
CreatedAt: s.CreatedAt,
UpdatedAt: s.UpdatedAt,
Chats: []*Chat{},
}
doc.Sessions = append(doc.Sessions, sessionData)
sessionMap[s.UUID] = sessionData
}
chats, err := db.GetChatsBySessionID(s.ID)
if err != nil {
continue
}
sessionMap[s.UUID].Chats = append(sessionMap[s.UUID].Chats, chats...)
}
file, err := os.Create(filename)
if err != nil {
return fmt.Errorf("创建文件失败: %w", err)
}
defer file.Close()
encoder := json.NewEncoder(file)
encoder.SetIndent("", " ")
return encoder.Encode(doc)
}