make something to save the books.pb
This commit is contained in:
parent
1aaa1d0e07
commit
60ef1deb90
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
27
config.go
27
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
|
||||
}
|
||||
|
|
|
@ -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, "", "", ""}
|
||||
|
|
Loading…
Reference in New Issue