Proxy translating OpenAI/Anthropic Messages API to OpenCode Zen's free models. Pure stdlib Go 1.22, no external dependencies. - OpenAI Chat Completions (/v1/chat/completions) with streaming - Anthropic Messages API (/v1/messages) with streaming - Model aliases for 5 free models - Auth middleware support - Session rotation (30min per user key)
56 lines
1.2 KiB
Go
56 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
|
|
"opencode-proxy/internal/proxy"
|
|
"opencode-proxy/internal/server"
|
|
)
|
|
|
|
const appVersion = "1.0.0"
|
|
|
|
func main() {
|
|
port := flag.String("port", "", "Server port (default: 6446)")
|
|
host := flag.String("host", "", "Server host (default: 127.0.0.1)")
|
|
apiKey := flag.String("api-key", "", "API key to protect the proxy (optional, can also be set via OPENCODE_PROXY_KEY env var)")
|
|
showVersion := flag.Bool("version", false, "Show version and exit")
|
|
flag.Parse()
|
|
|
|
if *showVersion {
|
|
fmt.Printf("opencode-proxy v%s\n", appVersion)
|
|
return
|
|
}
|
|
|
|
// Resolve API key: flag takes precedence, then env var
|
|
key := *apiKey
|
|
if key == "" {
|
|
key = os.Getenv("OPENCODE_PROXY_KEY")
|
|
}
|
|
|
|
p := proxy.NewProxy(key)
|
|
srv := server.NewServer(p)
|
|
|
|
if *port != "" {
|
|
srv.SetPort(*port)
|
|
}
|
|
if *host != "" {
|
|
srv.SetHost(*host)
|
|
}
|
|
|
|
fmt.Printf("OpenCode Zen Proxy v%s\n", appVersion)
|
|
fmt.Printf("Starting on %s:%s\n", srv.Host(), srv.Port())
|
|
if key != "" {
|
|
fmt.Println("API key protection: ENABLED")
|
|
} else {
|
|
fmt.Println("API key protection: DISABLED (no key set)")
|
|
}
|
|
fmt.Printf("Free models available: %d\n", len(p.Models()))
|
|
|
|
if err := srv.Start(); err != nil {
|
|
fmt.Fprintf(os.Stderr, "Server error: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|