-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenrich.go
More file actions
336 lines (291 loc) · 10.8 KB
/
enrich.go
File metadata and controls
336 lines (291 loc) · 10.8 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// Package cmd contains all CLI commands for spec-forge.
package cmd
import (
"errors"
"fmt"
"log/slog"
"os"
"path/filepath"
"strings"
"time"
"github.com/getkin/kin-openapi/openapi3"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
"github.com/spencercjh/spec-forge/internal/cli"
"github.com/spencercjh/spec-forge/internal/config"
"github.com/spencercjh/spec-forge/internal/enricher"
"github.com/spencercjh/spec-forge/internal/enricher/processor"
"github.com/spencercjh/spec-forge/internal/enricher/provider"
)
// enrichCmd represents the enrich command
var enrichCmd = &cobra.Command{
Use: "enrich \u003cspec-file\u003e",
Short: "Enrich OpenAPI spec with AI-generated descriptions",
Long: `Enrich OpenAPI specification by using LLM to generate missing descriptions
for APIs and fields.
Supports multiple LLM providers: OpenAI, Anthropic, Ollama, and custom OpenAI-compatible services.
Examples:
# Enrich with OpenAI
spec-forge enrich openapi.yaml --provider openai --model gpt-4o
# Enrich with Chinese descriptions
spec-forge enrich openapi.yaml --provider openai --language zh
# Use custom internal AI service
spec-forge enrich openapi.yaml \
--provider custom \
--custom-base-url https://ai.company.com/v1 \
--custom-api-key-env COMPANY_AI_KEY`,
Args: cobra.ExactArgs(1),
RunE: runEnrich,
}
//nolint:gocyclo // CLI command runner with many branches
func runEnrich(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
specFile := args[0]
cfg := config.Get()
// Get flag values from command (isolated per command instance)
//nolint:errcheck // flags are bound at command creation, errors not possible
providerFlag, _ := cmd.Flags().GetString("provider")
//nolint:errcheck
modelFlag, _ := cmd.Flags().GetString("model")
//nolint:errcheck
languageFlag, _ := cmd.Flags().GetString("language")
//nolint:errcheck
concurrencyFlag, _ := cmd.Flags().GetInt("concurrency")
//nolint:errcheck
timeoutFlag, _ := cmd.Flags().GetDuration("timeout")
//nolint:errcheck
customBaseURLFlag, _ := cmd.Flags().GetString("custom-base-url")
//nolint:errcheck
customAPIKeyEnvFlag, _ := cmd.Flags().GetString("custom-api-key-env")
//nolint:errcheck
noStreamFlag, _ := cmd.Flags().GetBool("no-stream")
//nolint:errcheck
forceFlag, _ := cmd.Flags().GetBool("force")
// Determine provider
prov := providerFlag
if prov == "" {
prov = cfg.Enrich.Provider
}
if prov == "" {
return errors.New("provider is required. Use --provider flag or configure in .spec-forge.yaml")
}
// Determine model
model := modelFlag
if model == "" {
model = cfg.Enrich.Model
}
if model == "" {
return errors.New("model is required. Use --model flag or configure in .spec-forge.yaml")
}
// Determine language
lang := languageFlag
if lang == "" {
lang = cfg.Enrich.Language
}
if lang == "" {
lang = "en"
}
// Create provider
p, err := createProvider(prov, model, cfg.Enrich, customBaseURLFlag, customAPIKeyEnvFlag)
if err != nil {
return err
}
cli.Statusf(os.Stderr, "Enriching OpenAPI spec (provider: %s, model: %s, language: %s)", prov, model, lang)
// Load spec
loader := openapi3.NewLoader()
loader.IsExternalRefsAllowed = true
spec, err := loader.LoadFromFile(specFile)
if err != nil {
return fmt.Errorf("failed to load spec: %w", err)
}
// Determine output file
//nolint:errcheck
outputFlag, _ := cmd.Flags().GetString("output")
outputFile := outputFlag
if outputFile == "" {
outputFile = specFile // Overwrite input by default
}
// Create enricher config
customBaseURL := customBaseURLFlag
if customBaseURL == "" {
customBaseURL = cfg.Enrich.BaseURL
}
customAPIKeyEnv := customAPIKeyEnvFlag
if customAPIKeyEnv == "" {
customAPIKeyEnv = cfg.Enrich.APIKeyEnv
}
enricherCfg := enricher.Config{
Provider: prov,
Model: model,
Language: lang,
Concurrency: concurrencyFlag,
Timeout: timeoutFlag,
CustomBaseURL: customBaseURL,
CustomAPIKeyEnv: customAPIKeyEnv,
}
enricherCfg = enricherCfg.MergeWithDefaults()
// Create enricher
e, err := enricher.NewEnricher(enricherCfg, p)
if err != nil {
return fmt.Errorf("failed to create enricher: %w", err)
}
// Enrich
streamEnabled := !noStreamFlag // Streaming enabled by default
result, err := e.Enrich(ctx, spec, &enricher.EnrichOptions{
Language: lang,
Stream: &streamEnabled,
Force: forceFlag,
})
if err != nil {
// Check if partial enrichment
if partialErr, ok := errors.AsType[*processor.PartialEnrichmentError](err); ok {
slog.WarnContext(ctx, "Partial enrichment completed",
"failed_batches", partialErr.FailedBatches,
"total_batches", partialErr.TotalBatches,
)
cli.Statusf(os.Stderr, "Partial enrichment: %d/%d batches succeeded", partialErr.TotalBatches-partialErr.FailedBatches, partialErr.TotalBatches)
} else {
return fmt.Errorf("enrichment failed: %w", err)
}
}
// Save enriched spec to file
var data []byte
if strings.ToLower(filepath.Ext(outputFile)) == ".json" {
data, err = result.MarshalJSON()
} else {
var yamlData any
yamlData, err = result.MarshalYAML()
if err == nil {
data, err = yaml.Marshal(yamlData)
}
}
if err != nil {
return fmt.Errorf("failed to marshal enriched spec: %w", err)
}
// Ensure output directory exists
if dir := filepath.Dir(outputFile); dir != "" && dir != "." {
if mkdirErr := os.MkdirAll(dir, 0o755); mkdirErr != nil {
return fmt.Errorf("failed to create output directory %q: %w", dir, mkdirErr)
}
}
if writeErr := os.WriteFile(outputFile, data, 0o600); writeErr != nil {
return fmt.Errorf("failed to write enriched spec: %w", writeErr)
}
cli.Successf(os.Stderr, "Enrichment complete: %s", outputFile)
return nil
}
// createProvider creates a provider based on the provider type
func createProvider(providerType, model string, enrichCfg config.EnrichConfig, customBaseURL, customAPIKeyEnv string) (provider.Provider, error) { //nolint:gocritic // copying config is acceptable
// Determine baseURL: flag > config > default
baseURL := customBaseURL
if baseURL == "" {
baseURL = enrichCfg.BaseURL
}
cfg := provider.Config{
Provider: providerType,
Model: model,
BaseURL: baseURL,
}
// Get API key based on provider type
switch providerType {
case "openai":
cfg.APIKey = os.Getenv("OPENAI_API_KEY")
if cfg.APIKey == "" {
return nil, errors.New("OPENAI_API_KEY environment variable not set")
}
case "anthropic":
cfg.APIKey = os.Getenv("ANTHROPIC_API_KEY")
if cfg.APIKey == "" {
return nil, errors.New("ANTHROPIC_API_KEY environment variable not set")
}
case "custom":
cfg.APIKey = getCustomAPIKey(&enrichCfg, customAPIKeyEnv)
if cfg.APIKey == "" {
return nil, fmt.Errorf("API key not found. Set %s environment variable", getCustomAPIKeyEnv(&enrichCfg, customAPIKeyEnv))
}
}
return provider.NewProvider(cfg)
}
func getCustomAPIKeyEnv(enrichCfg *config.EnrichConfig, flagValue string) string {
if flagValue != "" {
return flagValue
}
if enrichCfg.APIKeyEnv != "" {
return enrichCfg.APIKeyEnv
}
return "LLM_API_KEY"
}
func getCustomAPIKey(enrichCfg *config.EnrichConfig, flagValue string) string {
// Priority: env > config
// First check environment variable
if apiKey := os.Getenv(getCustomAPIKeyEnv(enrichCfg, flagValue)); apiKey != "" {
return apiKey
}
// Then check config file
return enrichCfg.APIKey
}
// newEnrichCmd creates a new enrich command instance for testing.
func newEnrichCmd() *cobra.Command {
c := &cobra.Command{
Use: "enrich \u003cspec-file\u003e",
Short: "Enrich OpenAPI spec with AI-generated descriptions",
Long: `Enrich OpenAPI specification by using LLM to generate missing descriptions
for APIs and fields.
Supports multiple LLM providers: OpenAI, Anthropic, Ollama, and custom OpenAI-compatible services.
Examples:
# Enrich with OpenAI
spec-forge enrich openapi.yaml --provider openai --model gpt-4o
# Enrich with Chinese descriptions
spec-forge enrich openapi.yaml --provider openai --language zh
# Use custom internal AI service
spec-forge enrich openapi.yaml \
--provider custom \
--custom-base-url https://ai.company.com/v1 \
--custom-api-key-env COMPANY_AI_KEY`,
Args: cobra.ExactArgs(1),
RunE: runEnrich,
}
c.Flags().String("provider", "", "LLM provider (openai, anthropic, ollama, custom)")
c.Flags().String("model", "", "LLM model name")
c.Flags().String("language", "en", "Output language for descriptions")
c.Flags().StringP("output", "o", "", "Output file (default: overwrite input)")
c.Flags().Int("concurrency", 3, "Max concurrent LLM calls (only effective with --no-stream)")
c.Flags().Duration("timeout", 30*time.Second, "Timeout for single LLM call")
c.Flags().String("custom-base-url", "", "Custom provider API URL")
c.Flags().String("custom-api-key-env", "LLM_API_KEY", "Environment variable for custom API key")
c.Flags().Bool("no-stream", false, "Disable streaming to enable concurrent processing (faster, but no real-time output)")
c.Flags().Bool("force", false, "Force regeneration of all descriptions, ignoring existing ones")
registerCompletion(c, "provider", []string{"openai", "anthropic", "ollama", "custom"})
registerCompletion(c, "language", []string{"en", "zh"})
registerCompletion(c, "output", []string{"yaml", "json"})
return c
}
// enrich command flag variables for global rootCmd only
var (
enrichProvider string
enrichModel string
enrichLanguage string
enrichOutput string
enrichConcurrency int
enrichTimeout time.Duration
enrichCustomBaseURL string
enrichCustomAPIKeyEnv string
enrichNoStream bool
enrichForce bool
)
func init() {
rootCmd.AddCommand(enrichCmd)
enrichCmd.Flags().StringVar(&enrichProvider, "provider", "", "LLM provider (openai, anthropic, ollama, custom)")
enrichCmd.Flags().StringVar(&enrichModel, "model", "", "LLM model name")
enrichCmd.Flags().StringVar(&enrichLanguage, "language", "en", "Output language for descriptions")
enrichCmd.Flags().StringVarP(&enrichOutput, "output", "o", "", "Output file (default: overwrite input)")
enrichCmd.Flags().IntVar(&enrichConcurrency, "concurrency", 3, "Max concurrent LLM calls (only with --no-stream)")
enrichCmd.Flags().DurationVar(&enrichTimeout, "timeout", 30*time.Second, "Timeout for single LLM call")
enrichCmd.Flags().StringVar(&enrichCustomBaseURL, "custom-base-url", "", "Custom provider API URL")
enrichCmd.Flags().StringVar(&enrichCustomAPIKeyEnv, "custom-api-key-env", "LLM_API_KEY", "Environment variable for custom API key")
enrichCmd.Flags().BoolVar(&enrichNoStream, "no-stream", false, "Disable streaming output to enable concurrent LLM calls (faster)")
enrichCmd.Flags().BoolVar(&enrichForce, "force", false, "Force regeneration of all descriptions, ignoring existing ones")
registerCompletion(enrichCmd, "provider", []string{"openai", "anthropic", "ollama", "custom"})
registerCompletion(enrichCmd, "language", []string{"en", "zh"})
registerCompletion(enrichCmd, "output", []string{"yaml", "json"})
}