-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin_config.go
More file actions
113 lines (101 loc) · 2.91 KB
/
Copy pathplugin_config.go
File metadata and controls
113 lines (101 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"encoding/json"
"fmt"
"strings"
"sync"
"gopkg.in/yaml.v3"
)
const defaultCLIProxyConfigPath = "/CLIProxyAPI/config.yaml"
const defaultAccountsPath = "/CLIProxyAPI/opencode-accounts.yaml"
type lifecycleRequest struct {
ConfigYAML []byte `json:"config_yaml"`
}
type pluginConfig struct {
ConfigPath string `yaml:"config_path"`
AccountsPath string `yaml:"accounts_path"`
ProviderNames []string `yaml:"provider_names"`
MaxConcurrency int `yaml:"max_concurrency"`
TimeoutSeconds int `yaml:"timeout_seconds"`
AutoPool bool `yaml:"auto_pool"`
}
var runtimeConfig = struct {
sync.RWMutex
value pluginConfig
}{value: defaultPluginConfig()}
func defaultPluginConfig() pluginConfig {
return pluginConfig{
ConfigPath: defaultCLIProxyConfigPath,
AccountsPath: defaultAccountsPath,
MaxConcurrency: 4,
TimeoutSeconds: 15,
}
}
func parsePluginConfig(raw []byte) (pluginConfig, error) {
config := defaultPluginConfig()
if len(raw) > 0 {
if err := yaml.Unmarshal(raw, &config); err != nil {
return pluginConfig{}, fmt.Errorf("decode plugin config: %w", err)
}
}
config.ConfigPath = strings.TrimSpace(config.ConfigPath)
if config.ConfigPath == "" {
config.ConfigPath = defaultCLIProxyConfigPath
}
config.AccountsPath = strings.TrimSpace(config.AccountsPath)
if config.AccountsPath == "" {
config.AccountsPath = defaultAccountsPath
}
if config.MaxConcurrency < 1 || config.MaxConcurrency > 16 {
return pluginConfig{}, fmt.Errorf("max_concurrency must be between 1 and 16")
}
if config.TimeoutSeconds < 1 || config.TimeoutSeconds > 60 {
return pluginConfig{}, fmt.Errorf("timeout_seconds must be between 1 and 60")
}
config.ProviderNames = normalizeProviderNames(config.ProviderNames)
return config, nil
}
func configurePlugin(request []byte) error {
var lifecycle lifecycleRequest
if len(request) > 0 {
if err := json.Unmarshal(request, &lifecycle); err != nil {
return fmt.Errorf("decode plugin lifecycle request: %w", err)
}
}
config, err := parsePluginConfig(lifecycle.ConfigYAML)
if err != nil {
return err
}
setPluginConfig(config)
return nil
}
func currentPluginConfig() pluginConfig {
runtimeConfig.RLock()
defer runtimeConfig.RUnlock()
config := runtimeConfig.value
config.ProviderNames = append([]string(nil), config.ProviderNames...)
return config
}
func setPluginConfig(config pluginConfig) {
config.ProviderNames = append([]string(nil), config.ProviderNames...)
runtimeConfig.Lock()
runtimeConfig.value = config
runtimeConfig.Unlock()
}
func normalizeProviderNames(names []string) []string {
result := make([]string, 0, len(names))
seen := make(map[string]struct{}, len(names))
for _, name := range names {
name = strings.TrimSpace(name)
key := strings.ToLower(name)
if key == "" {
continue
}
if _, exists := seen[key]; exists {
continue
}
seen[key] = struct{}{}
result = append(result, name)
}
return result
}