added gemini cli for endpoint /runGemini
This commit is contained in:
137
main.go
137
main.go
@ -3,11 +3,15 @@ package main
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type runRequest struct {
|
||||
@ -21,15 +25,37 @@ type runResponse struct {
|
||||
Stderr string `json:"stderr"`
|
||||
}
|
||||
|
||||
type geminiResponse struct {
|
||||
Success bool `json:"success"`
|
||||
Answer *string `json:"answer"`
|
||||
Usage interface{} `json:"usage"`
|
||||
Stdout string `json:"stdout"`
|
||||
Stderr string `json:"stderr"`
|
||||
ExitCode int `json:"exitCode"`
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
type event struct {
|
||||
Type string `json:"type"`
|
||||
Item map[string]interface{} `json:"item"`
|
||||
Usage interface{} `json:"usage"`
|
||||
}
|
||||
|
||||
type commandResult struct {
|
||||
Stdout string
|
||||
Stderr string
|
||||
ExitCode int
|
||||
Err error
|
||||
TimedOut bool
|
||||
}
|
||||
|
||||
const geminiTimeout = 5 * time.Minute
|
||||
|
||||
func main() {
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/run", runHandler)
|
||||
mux.HandleFunc("/run", runCodexHandler)
|
||||
mux.HandleFunc("/runCodex", runCodexHandler)
|
||||
mux.HandleFunc("/runGemini", runGeminiHandler)
|
||||
|
||||
addr := ":8000"
|
||||
if port := os.Getenv("PORT"); port != "" {
|
||||
@ -42,7 +68,7 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
func runHandler(w http.ResponseWriter, r *http.Request) {
|
||||
func runCodexHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
@ -112,3 +138,110 @@ func runHandler(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, encodeErr.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func runGeminiHandler(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
if r.Method != http.MethodPost {
|
||||
writeGeminiError(w, http.StatusMethodNotAllowed, "method not allowed")
|
||||
return
|
||||
}
|
||||
|
||||
var req runRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
writeGeminiError(w, http.StatusBadRequest, "invalid json body")
|
||||
return
|
||||
}
|
||||
|
||||
prompt := strings.TrimSpace(req.Prompt)
|
||||
if prompt == "" {
|
||||
writeGeminiError(w, http.StatusBadRequest, "prompt must not be empty")
|
||||
return
|
||||
}
|
||||
|
||||
result := runCommand(r.Context(), geminiTimeout, "gemini", "-p", req.Prompt)
|
||||
answer := result.Stdout
|
||||
resp := geminiResponse{
|
||||
Success: result.Err == nil,
|
||||
Answer: &answer,
|
||||
Usage: nil,
|
||||
Stdout: result.Stdout,
|
||||
Stderr: result.Stderr,
|
||||
ExitCode: result.ExitCode,
|
||||
Error: commandError(result),
|
||||
}
|
||||
|
||||
if result.Err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
if encodeErr := json.NewEncoder(w).Encode(resp); encodeErr != nil {
|
||||
http.Error(w, encodeErr.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func runCommand(parent context.Context, timeout time.Duration, name string, args ...string) commandResult {
|
||||
ctx, cancel := context.WithTimeout(parent, timeout)
|
||||
defer cancel()
|
||||
|
||||
cmd := exec.CommandContext(ctx, name, args...)
|
||||
|
||||
var stdout bytes.Buffer
|
||||
var stderr bytes.Buffer
|
||||
cmd.Stdout = &stdout
|
||||
cmd.Stderr = &stderr
|
||||
|
||||
err := cmd.Run()
|
||||
result := commandResult{
|
||||
Stdout: stdout.String(),
|
||||
Stderr: stderr.String(),
|
||||
ExitCode: 0,
|
||||
Err: err,
|
||||
TimedOut: ctx.Err() == context.DeadlineExceeded,
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
result.ExitCode = -1
|
||||
var exitErr *exec.ExitError
|
||||
if errors.As(err, &exitErr) {
|
||||
result.ExitCode = exitErr.ExitCode()
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func commandError(result commandResult) string {
|
||||
if result.TimedOut {
|
||||
return "process timed out"
|
||||
}
|
||||
if result.Err == nil {
|
||||
return ""
|
||||
}
|
||||
if errors.Is(result.Err, exec.ErrNotFound) {
|
||||
return "gemini CLI not found in PATH"
|
||||
}
|
||||
|
||||
var execErr *exec.Error
|
||||
if errors.As(result.Err, &execErr) {
|
||||
return "gemini CLI not found in PATH"
|
||||
}
|
||||
|
||||
return result.Err.Error()
|
||||
}
|
||||
|
||||
func writeGeminiError(w http.ResponseWriter, status int, message string) {
|
||||
w.WriteHeader(status)
|
||||
resp := geminiResponse{
|
||||
Success: false,
|
||||
Answer: nil,
|
||||
Usage: nil,
|
||||
Stdout: "",
|
||||
Stderr: "",
|
||||
ExitCode: -1,
|
||||
Error: message,
|
||||
}
|
||||
if err := json.NewEncoder(w).Encode(resp); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user