mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-09-13 15:54:34 +00:00
feat(task): replace built-in task adaptors with a sandboxed JS plugin system (#7076)
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
relaycommon "github.com/QuantumNous/new-api/relay/common"
|
||||
"github.com/QuantumNous/new-api/relaykit/dto"
|
||||
"github.com/QuantumNous/new-api/relaykit/types"
|
||||
hosttypes "github.com/QuantumNous/new-api/types"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
@@ -47,7 +48,7 @@ type TaskAdaptor interface {
|
||||
EstimateBilling(c *gin.Context, info *relaycommon.RelayInfo) map[string]float64
|
||||
|
||||
// AdjustBillingOnSubmit returns adjusted OtherRatios from the upstream
|
||||
// submit response. Called after a successful DoResponse.
|
||||
// submit response. Called after a successful ParseResponse.
|
||||
// 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.
|
||||
@@ -68,7 +69,7 @@ type TaskAdaptor interface {
|
||||
BuildRequestBody(c *gin.Context, info *relaycommon.RelayInfo) (io.Reader, error)
|
||||
|
||||
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 *taskdto.TaskError)
|
||||
ParseResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (*TaskSubmitResponse, *taskdto.TaskError)
|
||||
|
||||
GetModelList() []string
|
||||
GetChannelName() string
|
||||
@@ -79,6 +80,55 @@ type TaskAdaptor interface {
|
||||
ParseTaskResult(respBody []byte) (*relaycommon.TaskInfo, error)
|
||||
}
|
||||
|
||||
// TaskSubmitResponse is the transport-independent result of parsing an
|
||||
// upstream task submission. Parsing must not write to the client response.
|
||||
type TaskSubmitResponse struct {
|
||||
UpstreamTaskID string
|
||||
TaskData []byte
|
||||
ClientResponse any
|
||||
Immediate *relaycommon.TaskInfo
|
||||
}
|
||||
|
||||
type OpenAIVideoConverter interface {
|
||||
ConvertToOpenAIVideo(originTask *model.Task) ([]byte, error)
|
||||
}
|
||||
|
||||
type TaskArtifact = hosttypes.TaskArtifact
|
||||
|
||||
type TaskArtifactClientRequest struct {
|
||||
Method string `json:"method"`
|
||||
Headers map[string]string `json:"headers,omitempty"`
|
||||
}
|
||||
|
||||
type TaskArtifactProvider interface {
|
||||
ListArtifacts(task *model.Task) ([]TaskArtifact, error)
|
||||
}
|
||||
|
||||
type TaskContentRequest struct {
|
||||
URL string
|
||||
Method string
|
||||
Headers map[string]string
|
||||
Body []byte
|
||||
Credentialless bool
|
||||
}
|
||||
|
||||
type TaskContentRequestProvider interface {
|
||||
BuildContentRequest(task *model.Task, artifactKey string, clientRequest TaskArtifactClientRequest) (*TaskContentRequest, error)
|
||||
}
|
||||
|
||||
type TaskUsageFactsProvider interface {
|
||||
ExtractUsageFacts(c *gin.Context, info *relaycommon.RelayInfo) map[string]any
|
||||
}
|
||||
|
||||
// TaskValidatedBillingProvider lets an adaptor reject invalid usage facts at
|
||||
// the existing estimate point, after model mapping and before quota
|
||||
// multiplication. Non-plugin task adaptors keep using EstimateBilling.
|
||||
type TaskValidatedBillingProvider interface {
|
||||
EstimateBillingValidated(c *gin.Context, info *relaycommon.RelayInfo) (map[string]float64, error)
|
||||
}
|
||||
|
||||
// TaskValidatedUsageFactsProvider is the tiered-billing counterpart to
|
||||
// TaskValidatedBillingProvider.
|
||||
type TaskValidatedUsageFactsProvider interface {
|
||||
ExtractUsageFactsValidated(c *gin.Context, info *relaycommon.RelayInfo) (map[string]any, error)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user