56 lines
1.4 KiB
Go
56 lines
1.4 KiB
Go
package plugin
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
type Config struct {
|
|
Enabled bool `yaml:"enabled"`
|
|
CodexOnly bool `yaml:"codex_only"`
|
|
DatabasePath string `yaml:"database_path"`
|
|
BootstrapName string `yaml:"bootstrap_name"`
|
|
BootstrapKey string `yaml:"bootstrap_key"`
|
|
}
|
|
|
|
func defaultConfig() Config {
|
|
return Config{
|
|
Enabled: true, CodexOnly: true, DatabasePath: "data/cpa-ext.db",
|
|
BootstrapName: "default", BootstrapKey: "000000",
|
|
}
|
|
}
|
|
|
|
func decodeConfig(raw []byte) (Config, error) {
|
|
cfg := defaultConfig()
|
|
if len(raw) == 0 {
|
|
return cfg, nil
|
|
}
|
|
if err := yaml.Unmarshal(raw, &cfg); err != nil {
|
|
return Config{}, fmt.Errorf("解析插件配置: %w", err)
|
|
}
|
|
if strings.TrimSpace(cfg.DatabasePath) == "" {
|
|
return Config{}, fmt.Errorf("database_path 不能为空")
|
|
}
|
|
if strings.TrimSpace(cfg.BootstrapName) == "" {
|
|
return Config{}, fmt.Errorf("bootstrap_name 不能为空")
|
|
}
|
|
if !validManagedSecret(cfg.BootstrapKey) {
|
|
return Config{}, fmt.Errorf("bootstrap_key 必须为 6-256 个不含空白或控制字符的字符")
|
|
}
|
|
return cfg, nil
|
|
}
|
|
|
|
func (c Config) accepts(record UsageRecord) bool {
|
|
if !c.Enabled {
|
|
return false
|
|
}
|
|
if !c.CodexOnly {
|
|
return true
|
|
}
|
|
provider := strings.ToLower(strings.TrimSpace(record.Provider))
|
|
model := strings.ToLower(strings.TrimSpace(record.Model))
|
|
return provider == "codex" || strings.Contains(model, "codex") || strings.HasPrefix(model, "gpt-")
|
|
}
|