Refactor: remove pkg mem cache
This commit is contained in:
@ -1,13 +1,13 @@
|
||||
package llmapi
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"gralias/broker"
|
||||
"gralias/config"
|
||||
"gralias/models"
|
||||
"gralias/pkg/cache"
|
||||
"gralias/repos"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
@ -19,6 +19,7 @@ import (
|
||||
|
||||
var (
|
||||
// botname -> channel
|
||||
repo = repos.NewRepoProvider("sqlite3://../gralias.db")
|
||||
SignalChanMap = make(map[string]chan bool)
|
||||
DoneChanMap = make(map[string]chan bool)
|
||||
// got prompt: control character (\\u0000-\\u001F) found while parsing a string at line 4 column 0
|
||||
@ -49,7 +50,7 @@ func convertToSliceOfStrings(value any) ([]string, error) {
|
||||
}
|
||||
}
|
||||
|
||||
//nolint: unused
|
||||
// nolint: unused
|
||||
func (b *Bot) checkGuesses(tempMap map[string]any, room *models.Room) error {
|
||||
guesses, err := convertToSliceOfStrings(tempMap["guesses"])
|
||||
if err != nil {
|
||||
@ -162,7 +163,8 @@ func (b *Bot) BotMove() {
|
||||
// botJournalName := models.NotifyJournalPrefix + b.RoomID
|
||||
b.log.Debug("got signal", "bot-team", b.Team, "bot-role", b.Role)
|
||||
// get room cards and actions
|
||||
room, err := getRoomByID(b.RoomID)
|
||||
// room, err := getRoomByID(b.RoomID)
|
||||
room, err := repo.RoomGetExtended(context.Background(), b.RoomID)
|
||||
if err != nil {
|
||||
b.log.Error("bot loop", "error", err)
|
||||
return
|
||||
@ -316,7 +318,8 @@ func NewBot(role, team, name, roomID string, cfg *config.Config, recovery bool)
|
||||
bot.LLMParser = NewOpenRouterParser(bot.log)
|
||||
}
|
||||
// add to room
|
||||
room, err := getRoomByID(bot.RoomID)
|
||||
// room, err := getRoomByID(bot.RoomID)
|
||||
room, err := repo.RoomGetExtended(context.Background(), bot.RoomID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -371,35 +374,39 @@ func NewBot(role, team, name, roomID string, cfg *config.Config, recovery bool)
|
||||
}
|
||||
|
||||
func saveBot(bot *Bot) error {
|
||||
key := models.CacheBotPredix + bot.RoomID + bot.BotName
|
||||
data, err := json.Marshal(bot)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
cache.MemCache.Set(key, data)
|
||||
return nil
|
||||
// key := models.CacheBotPredix + bot.RoomID + bot.BotName
|
||||
// data, err := json.Marshal(bot)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// // }
|
||||
// cache.MemCache.Set(key, data)
|
||||
botPlayer := bot.ToPlayer()
|
||||
return repo.PlayerAdd(context.Background(), botPlayer)
|
||||
}
|
||||
|
||||
func getRoomByID(roomID string) (*models.Room, error) {
|
||||
roomBytes, err := cache.MemCache.Get(models.CacheRoomPrefix + roomID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp := &models.Room{}
|
||||
if err := json.Unmarshal(roomBytes, &resp); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
// func getRoomByID(roomID string) (*models.Room, error) {
|
||||
// roomBytes, err := cache.MemCache.Get(models.CacheRoomPrefix + roomID)
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// resp := &models.Room{}
|
||||
// if err := json.Unmarshal(roomBytes, &resp); err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// return resp, nil
|
||||
// }
|
||||
|
||||
func saveRoom(room *models.Room) error {
|
||||
key := models.CacheRoomPrefix + room.ID
|
||||
data, err := json.Marshal(room)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
cache.MemCache.Set(key, data)
|
||||
return nil
|
||||
// key := models.CacheRoomPrefix + room.ID
|
||||
// data, err := json.Marshal(room)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// cache.MemCache.Set(key, data)
|
||||
// ------------
|
||||
// probably need to update other tables
|
||||
// like word_cards or marks;
|
||||
return repo.RoomUpdate(context.Background(), room)
|
||||
}
|
||||
|
||||
func (b *Bot) BuildSimpleGuesserPrompt(room *models.Room) string {
|
||||
|
@ -2,6 +2,7 @@ package llmapi
|
||||
|
||||
import (
|
||||
"gralias/config"
|
||||
"gralias/models"
|
||||
"log/slog"
|
||||
)
|
||||
|
||||
@ -84,3 +85,13 @@ type Bot struct {
|
||||
// SignalsCh chan bool
|
||||
// DoneCh chan bool
|
||||
}
|
||||
|
||||
func (b *Bot) ToPlayer() *models.Player {
|
||||
return &models.Player{
|
||||
Role: models.StrToUserRole(b.Role),
|
||||
Team: models.StrToUserTeam(b.Team),
|
||||
RoomID: &b.RoomID,
|
||||
Username: b.BotName,
|
||||
IsBot: true,
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user