package chatpb import ( "fmt" "os" "path/filepath" "time" "github.com/google/uuid" "go.wit.com/log" "google.golang.org/protobuf/proto" timestamppb "google.golang.org/protobuf/types/known/timestamppb" ) func (c *Chats) AddGeminiComment(s string) *Chat { chat := new(Chat) chat.From = Who_GEMINI chat.Content = s chat.Ctime = timestamppb.New(time.Now()) c.Append(chat) return chat } func (c *Chats) AddUserComment(s string) *Chat { chat := new(Chat) chat.From = Who_USER chat.Content = s c.Append(chat) return chat } func UnmarshalChats(data []byte) (*Chats, error) { c := new(Chats) err := c.Unmarshal(data) return c, err } func UnmarshalChatsTEXT(data []byte) (*Chats, error) { c := new(Chats) err := c.UnmarshalTEXT(data) return c, err } func (all *Chats) AddFile(filename string) error { data, err := os.ReadFile(filename) if err != nil { log.Fatalf("Error reading file %s: %v", filename, err) return err } logData, err := UnmarshalChatsTEXT(data) if err != nil { log.Fatalf("Error unmarshaling log file %s: %v", filename, err) return err } for _, chat := range logData.GetChats() { // make a copy newc := proto.Clone(chat).(*Chat) // Handle content: prefer content_file, fallback to content. var content string if contentFile := chat.GetContentFile(); contentFile != "" { // Construct the full path relative to the log file's directory. logDir := filepath.Dir(filename) contentPath := filepath.Join(logDir, contentFile) contentBytes, err := os.ReadFile(contentPath) if err != nil { content = fmt.Sprintf("--- ERROR: Could not read content file %s: %v ---", contentPath, err) } else { content = string(contentBytes) } } else { // Fallback for older log formats. content = chat.GetContent() } newc.Content = content newc.VerifyUuid() all.AppendByUuid(newc) } return nil } func (chats *Chats) VerifyUuids() bool { var changed bool all := chats.SortByUuid() for all.Scan() { chat := all.Next() if chat.Uuid == "" { chat.Uuid = uuid.New().String() changed = true } } return changed } func (c *Chat) VerifyUuid() bool { if c.Uuid == "" { c.Uuid = uuid.New().String() return true } return false }