-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig.go
More file actions
213 lines (180 loc) · 4.97 KB
/
config.go
File metadata and controls
213 lines (180 loc) · 4.97 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package main
import (
"errors"
"fmt"
"os"
"strconv"
"strings"
)
var (
errAPIKeyRequired = errors.New("api_key is required")
errInputPromptRequired = errors.New("input_prompt is required")
)
// Config holds all configuration for the LLM action
type Config struct {
BaseURL string
APIKey string
Model string
SkipSSLVerify bool
CACert string
SystemPrompt string
InputPrompt string
ToolSchema string
Temperature float64
MaxTokens int
Debug bool
Headers map[string]string
}
// LoadConfig loads configuration from environment variables
func LoadConfig() (*Config, error) {
config := &Config{
BaseURL: os.Getenv("INPUT_BASE_URL"),
APIKey: os.Getenv("INPUT_API_KEY"),
Model: os.Getenv("INPUT_MODEL"),
Temperature: 0.7, // default
MaxTokens: 1000, // default
}
// Set default base URL if not provided
if config.BaseURL == "" {
config.BaseURL = "https://api.openai.com/v1"
}
// Validate required inputs
if config.APIKey == "" {
return nil, errAPIKeyRequired
}
// Load input prompt (supports text, file path, or URL)
inputPromptInput := os.Getenv("INPUT_INPUT_PROMPT")
if inputPromptInput == "" {
return nil, errInputPromptRequired
}
loadedInputPrompt, err := LoadPrompt(inputPromptInput)
if err != nil {
return nil, fmt.Errorf("failed to load input_prompt: %w", err)
}
config.InputPrompt = loadedInputPrompt
// Load system prompt (supports text, file path, or URL)
systemPromptInput := os.Getenv("INPUT_SYSTEM_PROMPT")
if systemPromptInput != "" {
loadedPrompt, err := LoadPrompt(systemPromptInput)
if err != nil {
return nil, fmt.Errorf("failed to load system_prompt: %w", err)
}
config.SystemPrompt = loadedPrompt
}
// Load CA certificate (supports content, file path, or URL)
caCertInput := os.Getenv("INPUT_CA_CERT")
if caCertInput != "" {
loadedCACert, err := LoadContent(caCertInput)
if err != nil {
return nil, fmt.Errorf("failed to load ca_cert: %w", err)
}
config.CACert = loadedCACert
}
// Load tool schema (supports text, file path, or URL with template rendering)
toolSchemaInput := os.Getenv("INPUT_TOOL_SCHEMA")
if toolSchemaInput != "" {
loadedSchema, err := LoadPrompt(toolSchemaInput)
if err != nil {
return nil, fmt.Errorf("failed to load tool_schema: %w", err)
}
config.ToolSchema = loadedSchema
}
// Parse optional parameters
if err := config.parseTemperature(os.Getenv("INPUT_TEMPERATURE")); err != nil {
return nil, err
}
if err := config.parseMaxTokens(os.Getenv("INPUT_MAX_TOKENS")); err != nil {
return nil, err
}
if err := config.parseSkipSSL(os.Getenv("INPUT_SKIP_SSL_VERIFY")); err != nil {
return nil, err
}
if err := config.parseDebug(os.Getenv("INPUT_DEBUG")); err != nil {
return nil, err
}
if err := config.parseHeaders(os.Getenv("INPUT_HEADERS")); err != nil {
return nil, err
}
return config, nil
}
// parseTemperature parses temperature string to float64
func (c *Config) parseTemperature(s string) error {
if s == "" {
return nil
}
temp, err := strconv.ParseFloat(s, 64)
if err != nil {
return fmt.Errorf("invalid temperature value: %w", err)
}
c.Temperature = temp
return nil
}
// parseMaxTokens parses max tokens string to int
func (c *Config) parseMaxTokens(s string) error {
if s == "" {
return nil
}
tokens, err := strconv.Atoi(s)
if err != nil {
return fmt.Errorf("invalid max_tokens value: %w", err)
}
if tokens < 0 {
return fmt.Errorf("max_tokens must be positive")
}
c.MaxTokens = tokens
return nil
}
// parseSkipSSL parses skip SSL verify string to bool
func (c *Config) parseSkipSSL(s string) error {
if s == "" {
return nil
}
skip, err := strconv.ParseBool(s)
if err != nil {
return fmt.Errorf("invalid skip_ssl_verify value: %w", err)
}
c.SkipSSLVerify = skip
return nil
}
// parseDebug parses debug string to bool
func (c *Config) parseDebug(s string) error {
if s == "" {
return nil
}
debug, err := strconv.ParseBool(s)
if err != nil {
return fmt.Errorf("invalid debug value: %w", err)
}
c.Debug = debug
return nil
}
// parseHeaders parses headers string to map
// Format: "Header1:Value1,Header2:Value2" or multiline "Header1:Value1\nHeader2:Value2"
func (c *Config) parseHeaders(s string) error {
if s == "" {
return nil
}
c.Headers = make(map[string]string)
// Support both comma-separated and newline-separated formats
// First normalize newlines to commas for consistent parsing
normalized := strings.ReplaceAll(s, "\n", ",")
pairs := strings.Split(normalized, ",")
for _, pair := range pairs {
pair = strings.TrimSpace(pair)
if pair == "" {
continue
}
// Split on first colon only (value may contain colons)
idx := strings.Index(pair, ":")
if idx == -1 {
return fmt.Errorf("invalid header format: %q (expected 'Key:Value')", pair)
}
key := strings.TrimSpace(pair[:idx])
value := strings.TrimSpace(pair[idx+1:])
if key == "" {
return fmt.Errorf("empty header key in: %q", pair)
}
c.Headers[key] = value
}
return nil
}