feat: conf hot reload

This commit is contained in:
Zhang Minghan 2023-12-03 04:44:53 +08:00
parent 1315782930
commit e6ca4c7015
2 changed files with 7 additions and 5 deletions

View File

@ -3,7 +3,6 @@ package chatgpt
import ( import (
"chat/globals" "chat/globals"
"fmt" "fmt"
"github.com/spf13/viper"
) )
type ChatInstance struct { type ChatInstance struct {
@ -40,7 +39,7 @@ func NewChatInstance(endpoint, apiKey string) *ChatInstance {
func NewChatInstanceFromConfig(conf globals.ChannelConfig) *ChatInstance { func NewChatInstanceFromConfig(conf globals.ChannelConfig) *ChatInstance {
return NewChatInstance( return NewChatInstance(
viper.GetString(conf.GetEndpoint()), conf.GetEndpoint(),
viper.GetString(conf.GetRandomSecret()), conf.GetRandomSecret(),
) )
} }

View File

@ -25,13 +25,14 @@ func NewManager() *Manager {
Models: []string{}, Models: []string{},
PreflightSequence: map[string]Sequence{}, PreflightSequence: map[string]Sequence{},
} }
manager.Init() manager.Load()
return manager return manager
} }
func (m *Manager) Init() { func (m *Manager) Load() {
// init support models // init support models
m.Models = []string{}
for _, channel := range m.GetActiveSequence() { for _, channel := range m.GetActiveSequence() {
for _, model := range channel.GetModels() { for _, model := range channel.GetModels() {
if !utils.Contains(model, m.Models) { if !utils.Contains(model, m.Models) {
@ -41,6 +42,7 @@ func (m *Manager) Init() {
} }
// init preflight sequence // init preflight sequence
m.PreflightSequence = map[string]Sequence{}
for _, model := range m.Models { for _, model := range m.Models {
var seq Sequence var seq Sequence
for _, channel := range m.GetActiveSequence() { for _, channel := range m.GetActiveSequence() {
@ -108,6 +110,7 @@ func (m *Manager) GetMaxId() int {
func (m *Manager) SaveConfig() error { func (m *Manager) SaveConfig() error {
viper.Set("channel", m.Sequence) viper.Set("channel", m.Sequence)
m.Load()
return viper.WriteConfig() return viper.WriteConfig()
} }