-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
166 lines (141 loc) · 4.35 KB
/
main.go
File metadata and controls
166 lines (141 loc) · 4.35 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
package main
import (
"context"
"fmt"
"os"
"path/filepath"
)
// Version information (set via ldflags during build)
var (
Version = "dev"
Commit = "none"
Date = "unknown"
)
func main() {
// Handle --version and --help flags
if len(os.Args) > 1 {
switch os.Args[1] {
case "--version", "-V":
fmt.Printf("bjarne %s (%s, built %s)\n", Version, Commit, Date)
fmt.Println("AI-assisted C/C++ code generation with mandatory validation")
os.Exit(0)
case "--help", "-h":
printHelp()
os.Exit(0)
case "--validate", "-v":
// Validate-only mode
if len(os.Args) < 3 {
fmt.Fprintln(os.Stderr, "Usage: bjarne --validate <file1.cpp> [file2.cpp ...]")
os.Exit(1)
}
os.Exit(runValidateOnly(os.Args[2:]))
}
}
// Start the TUI
if err := StartTUI(); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}
// runValidateOnly validates files without entering the REPL
func runValidateOnly(files []string) int {
ctx := context.Background()
// Initialize container runtime
container, err := DetectContainerRuntime()
if err != nil {
fmt.Print(FormatUserError(err))
return 1
}
fmt.Printf("Using container runtime: %s\n", container.GetBinary())
// Check if validation image exists
if !container.ImageExists(ctx) {
fmt.Printf("\033[91mError:\033[0m Validation container not found.\n")
fmt.Printf(" Run 'bjarne' interactively to pull the container first.\n")
return 1
}
allPassed := true
for _, filename := range files {
// Read the file
content, err := os.ReadFile(filename)
if err != nil {
fmt.Printf("\033[91mERROR %s:\033[0m %v\n", filename, err)
allPassed = false
continue
}
code := string(content)
if code == "" {
fmt.Printf("\033[91mERROR %s:\033[0m File is empty\n", filename)
allPassed = false
continue
}
fmt.Printf("\n\033[93mValidating %s...\033[0m\n", filename)
// Get base filename for container
baseName := filepath.Base(filename)
// Run validation pipeline
results, err := container.ValidateCode(ctx, code, baseName)
if err != nil {
fmt.Printf("\033[91mERROR %s:\033[0m %v\n", filename, err)
allPassed = false
continue
}
fmt.Print(FormatResults(results))
// Check if all passed
filePassed := true
for _, r := range results {
if !r.Success {
filePassed = false
allPassed = false
break
}
}
if filePassed {
fmt.Printf("\033[92m%s passed all validation!\033[0m\n", filename)
}
}
if allPassed {
fmt.Printf("\n\033[92mAll files passed validation!\033[0m\n")
return 0
}
fmt.Printf("\n\033[91mSome files failed validation.\033[0m\n")
return 1
}
func printHelp() {
fmt.Println(`bjarne - AI-assisted C/C++ code generation with mandatory validation
Usage:
bjarne [flags]
bjarne --validate <file1.cpp> [file2.cpp ...]
Flags:
-h, --help Show this help message
-V, --version Show version information
-v, --validate Validate files without entering REPL
Interactive Commands (in REPL):
/help Show available commands
/save <file> Save last generated code to file
/validate <file> Validate existing file without generation
/clear Clear conversation history
/quit Exit bjarne
Environment Variables:
BJARNE_PROVIDER LLM provider: bedrock|anthropic|openai|gemini (default: bedrock)
BJARNE_API_KEY API key for Anthropic/OpenAI/Gemini providers
AWS_ACCESS_KEY_ID AWS credentials for Bedrock
AWS_SECRET_ACCESS_KEY AWS credentials for Bedrock
AWS_REGION AWS region (default: us-east-1)
BJARNE_MODEL Model name: haiku|sonnet|opus or specific model ID
BJARNE_VALIDATOR_IMAGE Custom validator container image
BJARNE_MAX_ITERATIONS Max validation retry attempts (default: 3)
BJARNE_MAX_TOKENS Max tokens per response (default: 8192)
BJARNE_MAX_TOTAL_TOKENS Session token budget (default: 150000, 0=unlimited)
LLMGUARD_URL LLM Guard API URL for security scanning (optional)
LLMGUARD_TOKEN LLM Guard API token for authentication (optional)
Examples:
# Interactive mode
$ bjarne
> write a thread-safe counter in C++
bjarne: Generating... Validating... done
[validated code displayed]
> /save counter.cpp
# Validate-only mode
$ bjarne --validate mycode.cpp
$ bjarne -v file1.cpp file2.cpp file3.cpp
For more information: https://github.com/3rg0n/bjarne`)
}