make an --add <filename> option

This commit is contained in:
Jeff Carr 2025-08-21 14:13:42 -05:00
parent b221e7a95c
commit a10b75d453
5 changed files with 80 additions and 6 deletions

View File

@ -10,7 +10,7 @@ package main
var argv args
type args struct {
Add *EmptyCmd `arg:"subcommand:add" help:"add a conversation"`
Add string `arg:"--add" help:"add a new chat"`
Format *EmptyCmd `arg:"subcommand:format" help:"add a conversation"`
Playback *PlaybackCmd `arg:"subcommand:playback" help:"dump your prior conversations to the terminal'"`
Force bool `arg:"--force" help:"try to strong arm things"`

View File

@ -30,7 +30,7 @@ func (args) doBashAuto() {
default:
if argv.BashAuto[0] == ARGNAME {
// list the subcommands here
fmt.Println("add format playback")
fmt.Println("--add format playback")
}
}
os.Exit(0)

View File

@ -12,6 +12,41 @@ import (
const termWidth = 120 // The target width for the formatted output boxes.
func parseRichLog(filename string) *chatpb.Chats {
data, err := os.ReadFile(filename)
if err != nil {
log.Fatalf("Error reading file %s: %v", filename, err)
}
logData, err := chatpb.UnmarshalChatsTEXT(data)
if err != nil {
log.Fatalf("Error unmarshaling log file %s: %v", filename, err)
}
for _, chat := range logData.GetChats() {
// 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()
}
chat.Content = content
}
return logData
}
func formatRichLog(filename string) *chatpb.Chats {
data, err := os.ReadFile(filename)
if err != nil {

40
main.go
View File

@ -9,6 +9,7 @@ import (
"embed"
"os"
"github.com/google/uuid"
"go.wit.com/dev/alexflint/arg"
"go.wit.com/gui"
"go.wit.com/lib/protobuf/chatpb"
@ -55,8 +56,29 @@ func main() {
os.Exit(0)
}
if argv.Add != nil {
log.Info("add new conversation to protobuf")
me.chats = chatpb.NewChats()
if err := me.chats.ConfigLoad(); err != nil {
badExit(err)
}
if verifyUuids(me.chats) {
me.chats.ConfigSave()
}
if argv.Add != "" {
log.Info("add new conversation to protobuf", argv.Add)
pb := parseRichLog(argv.Add)
verifyUuids(pb)
all := pb.SortByUuid()
for all.Scan() {
chat := all.Next()
log.Info("NEW CHAT", chat.From, chat.Uuid)
if test := me.chats.FindByContentFile(chat.ContentFile); test != nil {
log.Info("NOT NEW CHAT", test.ContentFile)
continue
}
me.chats.AppendByUuid(chat)
}
pb.ConfigSave()
okExit("")
}
@ -76,3 +98,17 @@ func main() {
// doGui()
okExit("")
}
func verifyUuids(chats *chatpb.Chats) 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
}

View File

@ -5,12 +5,15 @@ package main
import (
"go.wit.com/dev/alexflint/arg"
"go.wit.com/gui"
"go.wit.com/lib/protobuf/chatpb"
)
var me *mainType
// this app's variables
type mainType struct {
pp *arg.Parser // for parsing the command line args. Yay to alexf lint!
// myGui *gui.Node // the gui toolkit handle
pp *arg.Parser // for parsing the command line args. Yay to alexf lint!
chats *chatpb.Chats // all our prior conversations with gemini
myGui *gui.Node // the gui toolkit handle
}