File size: 2,112 Bytes
1de7911
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package handler

import (
	"crypto/rand"
	"encoding/hex"
	"errors"
	"fmt"
	"io"
	"net/http"
	"strings"

	"github.com/gin-gonic/gin"
	"zencoder-2api/internal/service"
)

type GeminiHandler struct {
	svc *service.GeminiService
}

func NewGeminiHandler() *GeminiHandler {
	return &GeminiHandler{svc: service.NewGeminiService()}
}

// generateTraceID 生成一个随机的 trace ID
func generateGeminiTraceID() string {
	b := make([]byte, 16)
	rand.Read(b)
	return hex.EncodeToString(b)
}

// HandleRequest 处理 POST /v1beta/models/*path
// 路径格式: /model:action 例如 /gemini-3-flash-preview:streamGenerateContent
func (h *GeminiHandler) HandleRequest(c *gin.Context) {
	path := c.Param("path")
	// 去掉开头的斜杠
	path = strings.TrimPrefix(path, "/")

	// 解析 model:action
	parts := strings.SplitN(path, ":", 2)
	if len(parts) != 2 {
		c.JSON(http.StatusBadRequest, gin.H{"error": "invalid path format"})
		return
	}

	modelName := parts[0]
	action := parts[1]

	body, err := io.ReadAll(c.Request.Body)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	switch action {
	case "generateContent":
		if err := h.svc.GenerateContentProxy(c.Request.Context(), c.Writer, modelName, body); err != nil {
			h.handleError(c, err)
		}
	case "streamGenerateContent":
		if err := h.svc.StreamGenerateContentProxy(c.Request.Context(), c.Writer, modelName, body); err != nil {
			h.handleError(c, err)
		}
	default:
		c.JSON(http.StatusBadRequest, gin.H{"error": "unsupported action: " + action})
	}
}

// handleError 统一处理错误,特别是没有可用账号的错误
func (h *GeminiHandler) handleError(c *gin.Context, err error) {
	if errors.Is(err, service.ErrNoAvailableAccount) || errors.Is(err, service.ErrNoPermission) {
		traceID := generateGeminiTraceID()
		errMsg := fmt.Sprintf("没有可用token(traceid: %s)", traceID)
		c.JSON(http.StatusServiceUnavailable, gin.H{"error": errMsg})
		return
	}
	c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
}