// Filename: internal/channel/channel.go package channel import ( "context" "gemini-balancer/internal/errors" "gemini-balancer/internal/models" "net/http" "time" "github.com/gin-gonic/gin" ) // ChannelProxy 是所有协议通道必须实现的统一接口 type ChannelProxy interface { // RewritePath 路径重写的核心接口,负责将客户端路径转换为上游期望的路径 RewritePath(basePath, originalPath string) string TransformRequest(c *gin.Context, requestBody []byte) (newBody []byte, modelName string, err error) // IsStreamRequest 检查请求是否为流式 IsStreamRequest(c *gin.Context, bodyBytes []byte) bool // IsOpenAICompatibleRequest 检查请求是否使用了OpenAI兼容路径 IsOpenAICompatibleRequest(c *gin.Context) bool // ExtractModel 从请求中提取模型名称 ExtractModel(c *gin.Context, bodyBytes []byte) string // ValidateKey 验证API Key的有效性 ValidateKey(ctx context.Context, apiKey *models.APIKey, targetURL string, timeout time.Duration) *errors.APIError // ModifyRequest 在将请求发往上游前对其进行修改(如添加认证) ModifyRequest(req *http.Request, apiKey *models.APIKey) // ProcessSmartStreamRequest 处理核心的流式代理请求 ProcessSmartStreamRequest(c *gin.Context, params SmartRequestParams) // 其他非流式处理方法等... } // SmartRequestParams 是一个参数容器,用于将所有高层依赖,一次性、干净地传递到底层。 type SmartRequestParams struct { CorrelationID string APIKey *models.APIKey UpstreamURL string RequestBody []byte OriginalRequest models.GeminiRequest EventLogger *models.RequestFinishedEvent MaxRetries int RetryDelay time.Duration LogTruncationLimit int StreamingRetryPrompt string // <--- 传递续传指令 }