From 60ef1deb90eafd1741f231ddbc19f48b6199061b Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Wed, 3 Sep 2025 01:23:01 -0500 Subject: [PATCH] make something to save the books.pb --- book.proto | 1 + config.books.go | 78 +++++++++++++++++++++++++++++++++++++++++++++++++ config.go | 27 ++++++++++------- humanTable.go | 1 + 4 files changed, 96 insertions(+), 11 deletions(-) create mode 100644 config.books.go diff --git a/book.proto b/book.proto index cb34151..1bd663a 100644 --- a/book.proto +++ b/book.proto @@ -23,4 +23,5 @@ message Books { // `autogenpb:marsha repeated Book Books = 3; // THIS MUST BE Chat and then Chats google.protobuf.Timestamp ctime = 4; string Title = 5; + string TitleUuid = 6; } diff --git a/config.books.go b/config.books.go new file mode 100644 index 0000000..8717540 --- /dev/null +++ b/config.books.go @@ -0,0 +1,78 @@ +package chatpb + +// functions to import and export the protobuf +// data to and from config files + +import ( + "errors" + "os" + "path/filepath" + + "go.wit.com/lib/protobuf/bugpb" + "go.wit.com/log" +) + +// write to ~/.config/regex/ unless ENV{REGEX_HOME} is set +func (all *Books) ConfigSave() error { + if os.Getenv("REGEX_HOME") == "" { + homeDir, _ := os.UserHomeDir() + fullpath := filepath.Join(homeDir, ".config/regex") + os.Setenv("REGEX_HOME", fullpath) + } + if all == nil { + log.Warn("chatpb all == nil") + return errors.New("chatpb.Books.ConfigSave() all == nil") + } + log.Info("Books.ConfigSave()") + + // --- Start of Fix --- + // Create a new, clean Books object to avoid marshaling a slice with nil entries. + cleanBooks := NewBooks() // Assuming NewBooks() initializes the struct correctly. + + // Loop through the original books and append only the non-nil ones. + for _, book := range all.GetBooks() { + if book != nil { + cleanBooks.Books = append(cleanBooks.Books, book) + } else { + log.Warn("Found and skipped a nil book entry during Books.ConfigSave") + } + } + // --- End of Fix --- + + data, err := cleanBooks.Marshal() // Marshal the clean object, not 'all' + if err != nil { + log.Info("chatpb proto.Marshal() failed len", len(data), err) + // The tryValidate logic might be less necessary now but kept for safety. + if err := cleanBooks.tryValidate(); err != nil { + return err + } else { + data, err = cleanBooks.Marshal() // Retry with the clean object + if err == nil { + log.Info("chatpb.Books.ConfigSave() pb.Marshal() worked after validation len", len(cleanBooks.Books), "books") + configWrite("regex.pb", data) + return nil + } + } + return err + } + + filename := "book." + all.GetTitleUuid() + ".pb" + if err := configWrite(filename, data); err != nil { + log.Infof("chatpb.Books.ConfigSave() failed len(Books)=%d bytes=%d", len(cleanBooks.Books), len(data)) + return err + } + return nil +} + +func (all *Books) tryValidate() error { + err := bugpb.ValidateProtoUTF8(all) + if err != nil { + log.Printf("Protobuf UTF-8 validation failed: %v\n", err) + } + if err := bugpb.SanitizeProtoUTF8(all); err != nil { + log.Warn("Sanitation failed:", err) + // log.Fatalf("Sanitization failed: %v", err) + return err + } + return nil +} diff --git a/config.go b/config.go index 715aabc..b5533a0 100644 --- a/config.go +++ b/config.go @@ -10,6 +10,7 @@ import ( "path/filepath" "time" + "go.wit.com/lib/gui/shell" "go.wit.com/lib/protobuf/bugpb" "go.wit.com/log" ) @@ -25,6 +26,7 @@ func (all *Chats) ConfigSave() error { log.Warn("chatpb all == nil") return errors.New("chatpb.ConfigSave() all == nil") } + log.Info("DOING ConfigSave()") // --- Start of Fix --- // Create a new, clean Chats object to avoid marshaling a slice with nil entries. @@ -46,7 +48,7 @@ func (all *Chats) ConfigSave() error { if err != nil { log.Info("chatpb proto.Marshal() failed len", len(data), err) // The tryValidate logic might be less necessary now but kept for safety. - if err := all.tryValidate(); err != nil { + if err := cleanChats.tryValidate(); err != nil { return err } else { data, err = cleanChats.Marshal() // Retry with the clean object @@ -61,6 +63,19 @@ func (all *Chats) ConfigSave() error { // --- Backup Logic --- filename := filepath.Join(os.Getenv("REGEX_HOME"), "regex.pb") + filebackup := filepath.Join(os.Getenv("REGEX_HOME"), "regex.backup.pb") + if s, err := os.Stat(filebackup); err == nil { + log.Info("STAT OF CONFIG BACKUP WORKED", filebackup) + age := time.Since(s.ModTime()) + if age > 10*time.Second { + log.Info(filebackup, "is greater than 10 minutes") + shell.RunVerbose([]string{"cp", filename, filebackup}) + } else { + log.Info(filebackup, "is less than 10 minutes") + } + } else { + log.Info("STAT OF CONFIG BACKUP FAILED", filebackup) + } if _, err := os.Stat(filename); err == nil { // File exists, so back it up. dir := filepath.Dir(filename) @@ -83,7 +98,6 @@ func (all *Chats) ConfigSave() error { } func (all *Chats) tryValidate() error { - err := bugpb.ValidateProtoUTF8(all) if err != nil { log.Printf("Protobuf UTF-8 validation failed: %v\n", err) @@ -169,15 +183,6 @@ func configWrite(filename string, data []byte) error { log.Warn("open config file :", err) return err } - if filename == "regex.text" { - // add header - cfgfile.Write([]byte("# this file is automatically re-generated from regex.pb, however,\n")) - cfgfile.Write([]byte("# if you want to edit it by hand, you can:\n")) - cfgfile.Write([]byte("# stop regex; remove regex.pb; edit regex.text; start regex\n")) - cfgfile.Write([]byte("# this will cause the default behavior to fallback to parsing this file for the config\n")) - cfgfile.Write([]byte("\n")) - cfgfile.Write([]byte("# this file is intended to be used to customize settings on what\n")) - } cfgfile.Write(data) return nil } diff --git a/humanTable.go b/humanTable.go index d77bc01..689677b 100644 --- a/humanTable.go +++ b/humanTable.go @@ -143,6 +143,7 @@ func (gr *GeminiRequest) PrintGeminiTable() { if fr := p.GetFunctionResponse(); fr != nil { what = "FuncResp" txt = fr.Name + txt = log.Sprintf("%s %s, %v", fr.Id, fr.Name, fr.Response) } args = []string{model, what, "", cId, partId, p.ThoughtSignature, txt, "", "", ""}