Skip to content

Commit

Permalink
Merge 6f07f1c into 0cf0777
Browse files Browse the repository at this point in the history
  • Loading branch information
Hoshinonyaruko authored May 9, 2024
2 parents 0cf0777 + 6f07f1c commit 951e6b1
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 27 deletions.
39 changes: 30 additions & 9 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,51 @@ type Config struct {
Settings structs.Settings `yaml:"settings"`
}

// LoadConfig 从文件中加载配置并初始化单例配置
// // 防抖
// type ConfigFileLoader struct {
// EventDelay time.Duration
// LastLoad time.Time
// }

// // 防抖
// func (fl *ConfigFileLoader) LoadConfigF(path string) (*Config, error) {
// now := time.Now()
// if now.Sub(fl.LastLoad) < fl.EventDelay {
// return instance, nil
// }
// fl.LastLoad = now

// return LoadConfig(path)
// }

func LoadConfig(path string) (*Config, error) {
mu.Lock()
defer mu.Unlock()

// 如果单例已经被初始化了,直接返回
if instance != nil {
return instance, nil
conf, err := loadConfigFromFile(path)
if err != nil {
return nil, err
}

instance = conf
return instance, nil
}

func loadConfigFromFile(path string) (*Config, error) {
configData, err := os.ReadFile(path)
if err != nil {
log.Println("Failed to read file:", err)
return nil, err
}

conf := &Config{}
err = yaml.Unmarshal(configData, conf)
if err != nil {
if err := yaml.Unmarshal(configData, conf); err != nil {
log.Println("Failed to unmarshal YAML:", err)
return nil, err
}

// 设置单例实例
instance = conf
return instance, nil
log.Printf("成功加载配置文件 %s\n", path)
return conf, nil
}

// 获取secretId
Expand Down
82 changes: 65 additions & 17 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"path/filepath"
"syscall"

"github.com/fsnotify/fsnotify"
_ "github.com/mattn/go-sqlite3" // 只导入,作为驱动

"github.com/hoshinonyaruko/gensokyo-llm/applogic"
Expand All @@ -38,32 +39,22 @@ func main() {

// 检查配置文件是否存在
if _, err := os.Stat(configFilePath); os.IsNotExist(err) {
if *ymlPath == "" {
// 用户没有指定-yml参数,按照默认行为处理
err = os.WriteFile(configFilePath, []byte(template.ConfigTemplate), 0644)
if err != nil {
fmtf.Println("Error writing config.yml:", err)
return
}
fmtf.Println("请配置config.yml然后再次运行.")
fmtf.Print("按下 Enter 继续...")
bufio.NewReader(os.Stdin).ReadBytes('\n')
os.Exit(0)
} else {
// 用户指定了-yml参数,但指定的文件不存在
fmtf.Println("指定的配置文件不存在:", *ymlPath)
return
}
handleMissingConfigFile(ymlPath, configFilePath)
} else {
if *ymlPath != "" {
fmtf.Println("载入成功:", *ymlPath)
fmt.Println("配置载入成功:", *ymlPath)
}
}

// 加载配置
conf, err := config.LoadConfig(configFilePath)
if err != nil {
log.Fatalf("error: %v", err)
}

// 设置配置文件监视器
go setupConfigWatcher(configFilePath)

// 日志落地
if config.GetSavelogs() {
fmtf.SetEnableFileLog(true)
Expand Down Expand Up @@ -244,3 +235,60 @@ func main() {
// 启动HTTP服务器
log.Fatal(http.ListenAndServe(portStr, nil))
}

func handleMissingConfigFile(ymlPath *string, configFilePath string) {
if *ymlPath == "" {
// 用户没有指定-yml参数,按照默认行为处理
err := os.WriteFile(configFilePath, []byte(template.ConfigTemplate), 0644)
if err != nil {
fmt.Println("Error writing config.yml:", err)
return
}
fmt.Println("请配置config.yml然后再次运行.")
fmt.Print("按下 Enter 继续...")
bufio.NewReader(os.Stdin).ReadBytes('\n')
os.Exit(0)
} else {
// 用户指定了-yml参数,但指定的文件不存在
fmt.Println("指定的配置文件不存在:", *ymlPath)
os.Exit(0)
}
}

func setupConfigWatcher(configFilePath string) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatalf("Error setting up watcher: %v", err)
}

// 添加一个100毫秒的Debouncing
//fileLoader := &config.ConfigFileLoader{EventDelay: 100 * time.Millisecond}

// Start the goroutine to handle file system events.
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return // Exit if channel is closed.
}
if event.Op&fsnotify.Write == fsnotify.Write {
fmt.Println("检测到配置文件变动:", event.Name)
//fileLoader.LoadConfigF(configFilePath)
config.LoadConfig(configFilePath)
}
case err, ok := <-watcher.Errors:
if !ok {
return // Exit if channel is closed.
}
log.Println("Watcher error:", err)
}
}
}()

// Add the config file to the list of watched files.
err = watcher.Add(configFilePath)
if err != nil {
log.Fatalf("Error adding watcher: %v", err)
}
}
24 changes: 23 additions & 1 deletion prompt/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"reflect"
"strings"
"sync"
"time"

"github.com/fsnotify/fsnotify"

Expand Down Expand Up @@ -40,6 +41,23 @@ func init() {
}
}

// 防抖
type FileLoader struct {
eventDelay time.Duration
lastLoad time.Time
fileName string
}

func (fl *FileLoader) LoadFile(event fsnotify.Event) {
now := time.Now()
if now.Sub(fl.lastLoad) < fl.eventDelay {
return
}
fl.lastLoad = now
fl.fileName = event.Name
loadFile(event.Name)
}

// LoadPrompts 确保目录存在并尝试加载提示词文件
func LoadPrompts() error {
// 构建目录路径
Expand Down Expand Up @@ -68,6 +86,9 @@ func LoadPrompts() error {
return err
}

// 添加一个100毫秒的Debouncing
fileLoader := &FileLoader{eventDelay: 100 * time.Millisecond}

go func() {
for {
select {
Expand All @@ -76,7 +97,7 @@ func LoadPrompts() error {
return
}
if event.Op&fsnotify.Write == fsnotify.Write {
loadFile(event.Name)
fileLoader.LoadFile(event)
}
case err, ok := <-watcher.Errors:
if !ok {
Expand Down Expand Up @@ -114,6 +135,7 @@ func loadFile(filename string) {

baseName := filepath.Base(filename)
promptsCache[baseName] = prompts
fmt.Printf("成功载入prompts[%v]\n", baseName)
}

// GetMessagesFromFilename returns a list of messages, each potentially with randomized content if '||' is used in prompts
Expand Down

0 comments on commit 951e6b1

Please sign in to comment.