2024-02-29 01:08:18 +08:00
|
|
|
package channel
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"io"
|
|
|
|
|
"net/http"
|
2025-10-11 15:30:09 +08:00
|
|
|
|
|
|
|
|
"github.com/QuantumNous/new-api/dto"
|
|
|
|
|
"github.com/QuantumNous/new-api/model"
|
|
|
|
|
relaycommon "github.com/QuantumNous/new-api/relay/common"
|
|
|
|
|
"github.com/QuantumNous/new-api/types"
|
2025-05-02 13:59:46 +08:00
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2024-02-29 01:08:18 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Adaptor interface {
|
|
|
|
|
// Init IsStream bool
|
2024-07-16 22:07:10 +08:00
|
|
|
Init(info *relaycommon.RelayInfo)
|
2024-02-29 01:08:18 +08:00
|
|
|
GetRequestURL(info *relaycommon.RelayInfo) (string, error)
|
2024-10-04 16:08:18 +08:00
|
|
|
SetupRequestHeader(c *gin.Context, req *http.Header, info *relaycommon.RelayInfo) error
|
2025-03-13 19:32:08 +08:00
|
|
|
ConvertOpenAIRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.GeneralOpenAIRequest) (any, error)
|
2024-07-06 17:09:22 +08:00
|
|
|
ConvertRerankRequest(c *gin.Context, relayMode int, request dto.RerankRequest) (any, error)
|
2025-01-23 05:54:39 +08:00
|
|
|
ConvertEmbeddingRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.EmbeddingRequest) (any, error)
|
2024-07-16 22:07:10 +08:00
|
|
|
ConvertAudioRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.AudioRequest) (io.Reader, error)
|
|
|
|
|
ConvertImageRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.ImageRequest) (any, error)
|
2025-05-02 13:59:46 +08:00
|
|
|
ConvertOpenAIResponsesRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.OpenAIResponsesRequest) (any, error)
|
2024-10-04 16:08:18 +08:00
|
|
|
DoRequest(c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (any, error)
|
2025-07-10 15:02:40 +08:00
|
|
|
DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *types.NewAPIError)
|
2024-02-29 01:08:18 +08:00
|
|
|
GetModelList() []string
|
|
|
|
|
GetChannelName() string
|
2025-03-12 21:31:46 +08:00
|
|
|
ConvertClaudeRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.ClaudeRequest) (any, error)
|
2025-08-01 22:23:35 +08:00
|
|
|
ConvertGeminiRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.GeminiChatRequest) (any, error)
|
2024-02-29 01:08:18 +08:00
|
|
|
}
|
2024-06-12 20:37:42 +08:00
|
|
|
|
|
|
|
|
type TaskAdaptor interface {
|
2025-08-25 18:01:10 +08:00
|
|
|
Init(info *relaycommon.RelayInfo)
|
2024-06-12 20:37:42 +08:00
|
|
|
|
2025-08-25 18:01:10 +08:00
|
|
|
ValidateRequestAndSetAction(c *gin.Context, info *relaycommon.RelayInfo) *dto.TaskError
|
2024-06-12 20:37:42 +08:00
|
|
|
|
2026-02-10 21:15:09 +08:00
|
|
|
// ── Billing ──────────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
// EstimateBilling returns OtherRatios for pre-charge based on user request.
|
|
|
|
|
// Called after ValidateRequestAndSetAction, before price calculation.
|
|
|
|
|
// Adaptors should extract duration, resolution, etc. from the parsed request
|
|
|
|
|
// and return them as ratio multipliers (e.g. {"seconds": 5, "size": 1.666}).
|
|
|
|
|
// Return nil to use the base model price without extra ratios.
|
|
|
|
|
EstimateBilling(c *gin.Context, info *relaycommon.RelayInfo) map[string]float64
|
|
|
|
|
|
|
|
|
|
// AdjustBillingOnSubmit returns adjusted OtherRatios from the upstream
|
|
|
|
|
// submit response. Called after a successful DoResponse.
|
|
|
|
|
// If the upstream returned actual parameters that differ from the estimate
|
|
|
|
|
// (e.g. actual seconds), return updated ratios so the caller can recalculate
|
|
|
|
|
// the quota and settle the delta with the pre-charge.
|
|
|
|
|
// Return nil if no adjustment is needed.
|
|
|
|
|
AdjustBillingOnSubmit(info *relaycommon.RelayInfo, taskData []byte) map[string]float64
|
|
|
|
|
|
|
|
|
|
// AdjustBillingOnComplete returns the actual quota when a task reaches a
|
|
|
|
|
// terminal state (success/failure) during polling.
|
|
|
|
|
// Called by the polling loop after ParseTaskResult.
|
|
|
|
|
// Return a positive value to trigger delta settlement (supplement / refund).
|
|
|
|
|
// Return 0 to keep the pre-charged amount unchanged.
|
|
|
|
|
AdjustBillingOnComplete(task *model.Task, taskResult *relaycommon.TaskInfo) int
|
|
|
|
|
|
|
|
|
|
// ── Request / Response ───────────────────────────────────────────
|
|
|
|
|
|
2025-08-25 18:01:10 +08:00
|
|
|
BuildRequestURL(info *relaycommon.RelayInfo) (string, error)
|
|
|
|
|
BuildRequestHeader(c *gin.Context, req *http.Request, info *relaycommon.RelayInfo) error
|
|
|
|
|
BuildRequestBody(c *gin.Context, info *relaycommon.RelayInfo) (io.Reader, error)
|
2024-06-12 20:37:42 +08:00
|
|
|
|
2025-08-25 18:01:10 +08:00
|
|
|
DoRequest(c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (*http.Response, error)
|
|
|
|
|
DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (taskID string, taskData []byte, err *dto.TaskError)
|
2024-06-12 20:37:42 +08:00
|
|
|
|
|
|
|
|
GetModelList() []string
|
|
|
|
|
GetChannelName() string
|
|
|
|
|
|
2026-02-10 21:15:09 +08:00
|
|
|
// ── Polling ──────────────────────────────────────────────────────
|
2025-06-08 21:40:57 +08:00
|
|
|
|
2026-02-10 21:15:09 +08:00
|
|
|
FetchTask(baseUrl, key string, body map[string]any, proxy string) (*http.Response, error)
|
2025-06-20 15:50:00 +08:00
|
|
|
ParseTaskResult(respBody []byte) (*relaycommon.TaskInfo, error)
|
2024-06-12 20:37:42 +08:00
|
|
|
}
|
2025-10-10 23:27:12 +08:00
|
|
|
|
|
|
|
|
type OpenAIVideoConverter interface {
|
2025-10-14 23:03:17 +08:00
|
|
|
ConvertToOpenAIVideo(originTask *model.Task) ([]byte, error)
|
2025-10-10 23:27:12 +08:00
|
|
|
}
|