Fix: new chat from json

This commit is contained in:
Grail Finder
2025-07-20 15:57:01 +03:00
parent e4b8a89f25
commit 6c0a290cec
2 changed files with 11 additions and 6 deletions

View File

@@ -2,11 +2,11 @@ package pngmeta
import ( import (
"bytes" "bytes"
"gf-lt/models"
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"gf-lt/models"
"io" "io"
"log/slog" "log/slog"
"os" "os"
@@ -100,7 +100,7 @@ func ReadCard(fname, uname string) (*models.CharCard, error) {
return charSpec.Simplify(uname, fname), nil return charSpec.Simplify(uname, fname), nil
} }
func readCardJson(fname string) (*models.CharCard, error) { func ReadCardJson(fname string) (*models.CharCard, error) {
data, err := os.ReadFile(fname) data, err := os.ReadFile(fname)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -134,7 +134,7 @@ func ReadDirCards(dirname, uname string, log *slog.Logger) ([]*models.CharCard,
} }
if strings.HasSuffix(f.Name(), ".json") { if strings.HasSuffix(f.Name(), ".json") {
fpath := path.Join(dirname, f.Name()) fpath := path.Join(dirname, f.Name())
cc, err := readCardJson(fpath) cc, err := ReadCardJson(fpath)
if err != nil { if err != nil {
return nil, err // better to log and continue return nil, err // better to log and continue
} }

View File

@@ -144,13 +144,18 @@ func makeChatTable(chatMap map[string]models.Chat) *tview.Table {
return return
} }
// Reload card from disk // Reload card from disk
newCard := &models.CharCard{}
newCard, err := pngmeta.ReadCard(cc.FilePath, cfg.UserRole) newCard, err := pngmeta.ReadCard(cc.FilePath, cfg.UserRole)
if err != nil { if err != nil {
logger.Error("failed to reload charcard", "path", cc.FilePath, "error", err) logger.Error("failed to reload charcard", "path", cc.FilePath, "error", err)
if err := notifyUser("error", "failed to reload card: "+cc.FilePath); err != nil { newCard, err = pngmeta.ReadCardJson(cc.FilePath)
logger.Warn("failed to notify", "error", err) if err != nil {
logger.Error("failed to reload charcard", "path", cc.FilePath, "error", err)
if err := notifyUser("error", "failed to reload card: "+cc.FilePath); err != nil {
logger.Warn("failed to notify", "error", err)
}
return
} }
return
} }
// Update sysMap with fresh card data // Update sysMap with fresh card data
sysMap[agentName] = newCard sysMap[agentName] = newCard