Skip to content

Commit

Permalink
polish code
Browse files Browse the repository at this point in the history
  • Loading branch information
lyricat committed Mar 28, 2023
1 parent 8986a53 commit 1be37ef
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 60 deletions.
4 changes: 2 additions & 2 deletions cmd/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ func NewCmdAppCreate() *cobra.Command {
apps := app.New(h)
appz := appServ.New(appServ.Config{
SecretKey: cfg.Sys.SecretKey,
}, apps)
}, apps, nil)

app, err := appz.CreateApp(ctx, userID, appName)
if err != nil {
cmd.PrintErrf("create app failed: %v'n", err)
return
}

cmd.Printf("app_id: %s, app_secret: %s", app.AppID, app.AppSecret)
cmd.Printf("app_id: %s\napp_secret: %s\n", app.AppID, app.AppSecret)
},
}

Expand Down
6 changes: 3 additions & 3 deletions cmd/httpd/httpd.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,6 @@ func NewCmdHttpd() *cobra.Command {
bots := bot.New(h)
orders := order.New(h)

appz := appServ.New(appServ.Config{
SecretKey: cfg.Sys.SecretKey,
}, apps)
milvusClient, err := milvus.Init(ctx, cfg.Milvus.Address)
if err != nil {
return err
Expand All @@ -93,6 +90,9 @@ func NewCmdHttpd() *cobra.Command {
InitUserCredits: cfg.Sys.InitUserCredits,
}, client, users, models)
indexService := indexServ.NewService(ctx, gptHandler, indexes, userz)
appz := appServ.New(appServ.Config{
SecretKey: cfg.Sys.SecretKey,
}, apps, indexService)

middlewarez := middlewareServ.New(middlewareServ.Config{}, apps, indexService)
botz := botServ.New(botServ.Config{}, apps, bots, middlewarez)
Expand Down
25 changes: 19 additions & 6 deletions service/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import (
func New(
cfg Config,
apps core.AppStore,
indexz core.IndexService,
) *service {
return &service{
cfg: cfg,
apps: apps,
cfg: cfg,
apps: apps,
indexz: indexz,
}
}

Expand All @@ -23,12 +25,13 @@ type Config struct {
}

type service struct {
cfg Config
apps core.AppStore
cfg Config
apps core.AppStore
indexz core.IndexService
}

func (s *service) ReplaceStore(apps core.AppStore) core.AppService {
return New(s.cfg, apps)
return New(s.cfg, apps, s.indexz)
}

func (s *service) CreateApp(ctx context.Context, userID uint64, name string) (*core.App, error) {
Expand Down Expand Up @@ -97,8 +100,18 @@ func (s *service) UpdateApp(ctx context.Context, id uint64, name string) error {
}

func (s *service) DeleteApp(ctx context.Context, id uint64) error {
if err := s.apps.DeleteApp(ctx, id); err != nil {
app, err := s.apps.GetApp(ctx, id)
if err != nil {
return err
}

if err := s.apps.DeleteApp(ctx, app.ID); err != nil {
return err
}

if err := s.indexz.ResetIndexes(ctx, app.AppID); err != nil {
return err
}

return nil
}
59 changes: 10 additions & 49 deletions service/bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,10 @@ import (
"context"
"encoding/json"
"fmt"
"os"
"path"
"strings"
"text/template"
"time"

"github.com/pandodao/botastic/core"
"github.com/patrickmn/go-cache"

"gopkg.in/yaml.v2"
)

func New(
Expand All @@ -22,10 +16,6 @@ func New(
bots core.BotStore,
middlewarez core.MiddlewareService,
) *service {
// botMap, err := LoadBots()
// if err != nil {
// panic(err)
// }
botCache := cache.New(time.Minute*5, time.Minute*5)

conversationMap := make(map[string]*core.Conversation)
Expand Down Expand Up @@ -54,42 +44,6 @@ type (
}
)

func LoadBots() (map[uint64]*core.Bot, error) {
avatarMap := make(map[uint64]*core.Bot)
base := "./bot_data"
filenames := make([]string, 0)
items, _ := os.ReadDir(base)
for _, item := range items {
if !item.IsDir() {
if !strings.HasSuffix(item.Name(), ".yaml") {
continue
}
filenames = append(filenames, item.Name())
}
}

// read yaml file into s.avatarMap
for _, filename := range filenames {
// read yaml file
data, err := os.ReadFile(path.Join(base, filename))
if err != nil {
continue
}
output := &core.Bot{}
err = yaml.Unmarshal(data, output)
if err != nil {
fmt.Printf("Error unmarshaling yaml content of file %s: %v\n", filename, err)
continue
}

output.PromptTpl = template.
Must(template.New(fmt.Sprintf("%d-prompt-tmpl", output.ID)).Parse(output.Prompt))

avatarMap[output.ID] = output
}
return avatarMap, nil
}

func (s *service) ReplaceStore(bots core.BotStore) core.BotService {
return New(s.cfg, s.apps, bots, s.middlewarez)
}
Expand Down Expand Up @@ -221,16 +175,23 @@ func (s *service) UpdateBot(ctx context.Context, id uint64, name, model, prompt
return err
}

key := fmt.Sprintf("bot-%d", id)

s.botCache.Delete(key)
s.botCache.Delete(fmt.Sprintf("bot-%d", id))
s.botCache.Delete(fmt.Sprintf("user-bots-%d", bot.UserID))
return nil
}

func (s *service) DeleteBot(ctx context.Context, id uint64) error {
bot, err := s.bots.GetBot(ctx, id)
if err != nil {
fmt.Printf("bots.GetBot err: %v\n", err)
return err
}

if err := s.bots.DeleteBot(ctx, id); err != nil {
return err
}

s.botCache.Delete(fmt.Sprintf("bot-%d", bot.ID))
s.botCache.Delete(fmt.Sprintf("user-bots-%d", bot.UserID))
return nil
}

0 comments on commit 1be37ef

Please sign in to comment.