-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
192 lines (160 loc) · 4.24 KB
/
main.go
File metadata and controls
192 lines (160 loc) · 4.24 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package main
import (
"encoding/json"
"flag"
"fmt"
"net/http"
"os"
"path/filepath"
"time"
)
var version = "dev"
const usage = `Usage: %s [flags] <output-file>
Generates OpenCode config from LiteLLM models endpoint.
Flags:
-h, --help Show help
--base-url string LiteLLM base URL (default "https://litellm.dius.network/v1")
--provider-name string Provider display name (default "LiteLLM Dius")
--provider-key string Provider key in config (default "litellm-dius")
Environment:
DIUS_LITELLM_SK Required. API key for LiteLLM
`
type cliConfig struct {
baseURL string
providerName string
providerKey string
outputFile string
}
type Model struct {
ID string `json:"id"`
}
type ModelsResponse struct {
Data []Model `json:"data"`
Error *struct {
Message string `json:"message"`
} `json:"error"`
}
type ModelConfig struct {
Name string `json:"name"`
}
type ProviderOptions struct {
BaseURL string `json:"baseURL"`
}
type Provider struct {
NPM string `json:"npm"`
Name string `json:"name"`
Options ProviderOptions `json:"options"`
Models map[string]ModelConfig `json:"models"`
}
type Config struct {
Schema string `json:"$schema"`
Provider map[string]Provider `json:"provider"`
}
func main() {
if len(os.Args) > 1 && (os.Args[1] == "--version" || os.Args[1] == "-v") {
fmt.Println(version)
os.Exit(0)
}
cfg := parseFlags()
apiKey := requireEnvVar("DIUS_LITELLM_SK")
models := fetchModels(cfg.baseURL, apiKey)
config := buildConfig(cfg, models)
createParentDirs(cfg.outputFile)
writeConfig(cfg.outputFile, config)
fmt.Printf("Config written to %s\n", cfg.outputFile)
}
func parseFlags() cliConfig {
baseURL := flag.String("base-url", "https://litellm.dius.network/v1", "LiteLLM base URL")
providerName := flag.String("provider-name", "LiteLLM Dius", "Provider display name")
providerKey := flag.String("provider-key", "litellm-dius", "Provider key in config")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, usage, os.Args[0])
}
flag.Parse()
if flag.NArg() != 1 {
flag.Usage()
os.Exit(1)
}
return cliConfig{
baseURL: *baseURL,
providerName: *providerName,
providerKey: *providerKey,
outputFile: flag.Arg(0),
}
}
func requireEnvVar(name string) string {
value := os.Getenv(name)
if value == "" {
fatal("%s env var not set", name)
}
return value
}
func fetchModels(baseURL, apiKey string) []Model {
client := &http.Client{Timeout: 30 * time.Second}
req, err := http.NewRequest("GET", baseURL+"/models", nil)
if err != nil {
fatal("failed to create request: %v", err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+apiKey)
resp, err := client.Do(req)
if err != nil {
fatal("failed to fetch models: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
fatal("API returned status %d", resp.StatusCode)
}
var modelsResp ModelsResponse
if err := json.NewDecoder(resp.Body).Decode(&modelsResp); err != nil {
fatal("failed to parse response: %v", err)
}
if modelsResp.Error != nil {
fatal("API returned error: %s", modelsResp.Error.Message)
}
return modelsResp.Data
}
func buildConfig(cfg cliConfig, models []Model) Config {
modelConfigs := make(map[string]ModelConfig)
for _, m := range models {
modelConfigs[m.ID] = ModelConfig{Name: m.ID}
}
return Config{
Schema: "https://opencode.ai/config.json",
Provider: map[string]Provider{
cfg.providerKey: {
NPM: "@ai-sdk/openai-compatible",
Name: cfg.providerName,
Options: ProviderOptions{
BaseURL: cfg.baseURL,
},
Models: modelConfigs,
},
},
}
}
func createParentDirs(path string) {
dir := filepath.Dir(path)
if dir == "." {
return
}
if err := os.MkdirAll(dir, 0755); err != nil {
fatal("failed to create directory: %v", err)
}
}
func writeConfig(path string, config Config) {
f, err := os.Create(path)
if err != nil {
fatal("failed to create file: %v", err)
}
defer f.Close()
enc := json.NewEncoder(f)
enc.SetIndent("", " ")
if err := enc.Encode(config); err != nil {
fatal("failed to write config: %v", err)
}
}
func fatal(format string, args ...any) {
fmt.Fprintf(os.Stderr, "Error: "+format+"\n", args...)
os.Exit(1)
}