mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-09-09 03:28:15 +00:00
feat(task): replace built-in task adaptors with a sandboxed JS plugin system (#7076)
This commit is contained in:
@@ -2,8 +2,14 @@ package billing_setting
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"sort"
|
||||
|
||||
"github.com/QuantumNous/new-api/common"
|
||||
"github.com/QuantumNous/new-api/relaykit/dto"
|
||||
"github.com/QuantumNous/new-api/pkg/billingexpr"
|
||||
"github.com/QuantumNous/new-api/pkg/jsplugin"
|
||||
relaycommon "github.com/QuantumNous/new-api/relay/common"
|
||||
"github.com/QuantumNous/new-api/setting/config"
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
@@ -13,6 +19,7 @@ const (
|
||||
BillingModeTieredExpr = "tiered_expr"
|
||||
BillingModeField = "billing_mode"
|
||||
BillingExprField = "billing_expr"
|
||||
maxTaskExprSmokeTests = 64
|
||||
)
|
||||
|
||||
// BillingSetting is managed by config.GlobalConfig.Register.
|
||||
@@ -75,13 +82,167 @@ func SmokeTestExpr(exprStr string) error {
|
||||
}
|
||||
|
||||
func smokeTestExpr(exprStr string) error {
|
||||
if _, err := billingexpr.CompileFromCache(exprStr); err != nil {
|
||||
return err
|
||||
}
|
||||
usageKeys := billingexpr.UsedUsageKeys(exprStr)
|
||||
if len(usageKeys) > 0 {
|
||||
sortedKeys := make([]string, 0, len(usageKeys))
|
||||
for key := range usageKeys {
|
||||
sortedKeys = append(sortedKeys, key)
|
||||
}
|
||||
sort.Strings(sortedKeys)
|
||||
return fmt.Errorf("expression references usage keys %v but the model has no task plugin usage schema", sortedKeys)
|
||||
}
|
||||
|
||||
vectors := []billingexpr.TokenParams{
|
||||
{P: 0, C: 0, Len: 0},
|
||||
{P: 1000, C: 1000, Len: 1000},
|
||||
{P: 100000, C: 100000, Len: 100000},
|
||||
{P: 1000000, C: 1000000, Len: 1000000},
|
||||
}
|
||||
requests := []billingexpr.RequestInput{
|
||||
|
||||
for _, v := range vectors {
|
||||
for _, request := range billingExprSmokeRequests() {
|
||||
result, _, err := billingexpr.RunExprWithRequest(exprStr, v, request)
|
||||
if err != nil {
|
||||
return fmt.Errorf("vector {p=%g, c=%g}: run failed: %w", v.P, v.C, err)
|
||||
}
|
||||
if math.IsNaN(result) || math.IsInf(result, 0) || result < 0 {
|
||||
return fmt.Errorf("vector {p=%g, c=%g}: result must be finite and non-negative, got %f", v.P, v.C, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SmokeTestTaskExpr validates a task usage expression against the usage facts
|
||||
// declared by its plugin. Literal u() keys must be declared; dynamic calls are
|
||||
// still exercised by the generated runtime vectors when possible.
|
||||
func SmokeTestTaskExpr(exprStr string, schema map[string]jsplugin.UsageFieldSchema) error {
|
||||
if _, err := billingexpr.CompileFromCache(exprStr); err != nil {
|
||||
return err
|
||||
}
|
||||
for key := range billingexpr.UsedUsageKeys(exprStr) {
|
||||
if _, declared := schema[key]; !declared {
|
||||
return fmt.Errorf("usage key %q is not declared by the task plugin", key)
|
||||
}
|
||||
}
|
||||
|
||||
for _, usage := range taskUsageSmokeVectors(schema) {
|
||||
for _, request := range billingExprSmokeRequests() {
|
||||
request.Usage = usage
|
||||
result, _, err := billingexpr.RunExprWithRequest(exprStr, billingexpr.TokenParams{}, request)
|
||||
if err != nil {
|
||||
return fmt.Errorf("usage vector %v: run failed: %w", usage, err)
|
||||
}
|
||||
if math.IsNaN(result) || math.IsInf(result, 0) || result < 0 {
|
||||
return fmt.Errorf("usage vector %v: result must be finite and non-negative, got %f", usage, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type usageSmokeDimension struct {
|
||||
name string
|
||||
values []any
|
||||
}
|
||||
|
||||
func taskUsageSmokeVectors(schema map[string]jsplugin.UsageFieldSchema) []map[string]any {
|
||||
names := make([]string, 0, len(schema))
|
||||
for name := range schema {
|
||||
names = append(names, name)
|
||||
}
|
||||
sort.Strings(names)
|
||||
|
||||
dimensions := make([]usageSmokeDimension, 0, len(names))
|
||||
for _, name := range names {
|
||||
field := schema[name]
|
||||
if len(field.Enum) > 0 {
|
||||
values := make([]any, len(field.Enum))
|
||||
for index, value := range field.Enum {
|
||||
values[index] = value
|
||||
}
|
||||
dimensions = append(dimensions, usageSmokeDimension{name: name, values: values})
|
||||
continue
|
||||
}
|
||||
if field.Type == "boolean" {
|
||||
dimensions = append(dimensions, usageSmokeDimension{name: name, values: []any{false, true}})
|
||||
continue
|
||||
}
|
||||
limit := relaycommon.MaxTaskDurationSeconds
|
||||
if field.Unit == "count" {
|
||||
limit = dto.MaxImageN
|
||||
}
|
||||
if field.Unit == "token" || field.Unit == "credit" {
|
||||
limit = common.MaxQuota
|
||||
}
|
||||
dimensions = append(dimensions, usageSmokeDimension{
|
||||
name: name,
|
||||
values: []any{float64(0), float64(1), float64(limit)},
|
||||
})
|
||||
}
|
||||
|
||||
if usageSmokeCombinationCount(dimensions, maxTaskExprSmokeTests) > maxTaskExprSmokeTests {
|
||||
for index := range dimensions {
|
||||
field := schema[dimensions[index].name]
|
||||
if len(field.Enum) <= 2 {
|
||||
continue
|
||||
}
|
||||
dimensions[index].values = []any{field.Enum[0], field.Enum[len(field.Enum)-1]}
|
||||
}
|
||||
}
|
||||
|
||||
vectors := make([]map[string]any, 0, maxTaskExprSmokeTests)
|
||||
var appendVectors func(int, map[string]any)
|
||||
appendVectors = func(index int, current map[string]any) {
|
||||
if len(vectors) >= maxTaskExprSmokeTests {
|
||||
return
|
||||
}
|
||||
if index == len(dimensions) {
|
||||
vector := make(map[string]any, len(current))
|
||||
for key, value := range current {
|
||||
vector[key] = value
|
||||
}
|
||||
vectors = append(vectors, vector)
|
||||
return
|
||||
}
|
||||
for _, value := range dimensions[index].values {
|
||||
current[dimensions[index].name] = value
|
||||
appendVectors(index+1, current)
|
||||
}
|
||||
delete(current, dimensions[index].name)
|
||||
}
|
||||
appendVectors(0, make(map[string]any, len(dimensions)))
|
||||
|
||||
combinationCount := usageSmokeCombinationCount(dimensions, maxTaskExprSmokeTests)
|
||||
if combinationCount > maxTaskExprSmokeTests && len(vectors) > 0 {
|
||||
last := make(map[string]any, len(dimensions))
|
||||
for _, dimension := range dimensions {
|
||||
last[dimension.name] = dimension.values[len(dimension.values)-1]
|
||||
}
|
||||
vectors[len(vectors)-1] = last
|
||||
}
|
||||
return vectors
|
||||
}
|
||||
|
||||
func usageSmokeCombinationCount(dimensions []usageSmokeDimension, stopAfter int) int {
|
||||
count := 1
|
||||
for _, dimension := range dimensions {
|
||||
if len(dimension.values) == 0 {
|
||||
return 0
|
||||
}
|
||||
if count > stopAfter/len(dimension.values) {
|
||||
return stopAfter + 1
|
||||
}
|
||||
count *= len(dimension.values)
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
func billingExprSmokeRequests() []billingexpr.RequestInput {
|
||||
return []billingexpr.RequestInput{
|
||||
{},
|
||||
{
|
||||
Headers: map[string]string{
|
||||
@@ -90,17 +251,4 @@ func smokeTestExpr(exprStr string) error {
|
||||
Body: []byte(`{"service_tier":"fast","stream_options":{"include_usage":true},"messages":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]}`),
|
||||
},
|
||||
}
|
||||
|
||||
for _, v := range vectors {
|
||||
for _, request := range requests {
|
||||
result, _, err := billingexpr.RunExprWithRequest(exprStr, v, request)
|
||||
if err != nil {
|
||||
return fmt.Errorf("vector {p=%g, c=%g}: run failed: %w", v.P, v.C, err)
|
||||
}
|
||||
if result < 0 {
|
||||
return fmt.Errorf("vector {p=%g, c=%g}: result %f < 0", v.P, v.C, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
package billing_setting
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/QuantumNous/new-api/common"
|
||||
"github.com/QuantumNous/new-api/relaykit/dto"
|
||||
"github.com/QuantumNous/new-api/pkg/jsplugin"
|
||||
relaycommon "github.com/QuantumNous/new-api/relay/common"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestSmokeTestTaskExprValidatesDeclaredUsageVectors(t *testing.T) {
|
||||
videoSchema := map[string]jsplugin.UsageFieldSchema{
|
||||
"seconds": {Type: "number", Unit: "second"},
|
||||
"mode": {Enum: []string{"std", "pro"}},
|
||||
"quality": {Enum: []string{"sd", "hd"}},
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
schema map[string]jsplugin.UsageFieldSchema
|
||||
expression string
|
||||
expectedError string
|
||||
}{
|
||||
{
|
||||
name: "declared numeric and enum facts",
|
||||
schema: videoSchema,
|
||||
expression: `u("mode") == "pro" ? tier("pro", u("seconds") * 0.8) : tier("std", u("seconds") * 0.4)`,
|
||||
},
|
||||
{
|
||||
name: "undeclared literal key",
|
||||
schema: videoSchema,
|
||||
expression: `tier("base", u("clips") * 0.1)`,
|
||||
expectedError: `usage key "clips" is not declared`,
|
||||
},
|
||||
{
|
||||
name: "negative duration boundary",
|
||||
schema: videoSchema,
|
||||
expression: fmt.Sprintf(`u("seconds") == %d ? -1 : 0`, relaycommon.MaxTaskDurationSeconds),
|
||||
expectedError: "result must be finite and non-negative",
|
||||
},
|
||||
{
|
||||
name: "negative count boundary",
|
||||
schema: map[string]jsplugin.UsageFieldSchema{"clips": {Type: "number", Unit: "count"}},
|
||||
expression: fmt.Sprintf(`u("clips") == %d ? -1 : 0`, dto.MaxImageN),
|
||||
expectedError: "result must be finite and non-negative",
|
||||
},
|
||||
{
|
||||
name: "negative token boundary",
|
||||
schema: map[string]jsplugin.UsageFieldSchema{"tokens": {Type: "number", Unit: "token"}},
|
||||
expression: fmt.Sprintf(`u("tokens") == %d ? -1 : 0`, common.MaxQuota),
|
||||
expectedError: "result must be finite and non-negative",
|
||||
},
|
||||
{
|
||||
name: "negative credit boundary",
|
||||
schema: map[string]jsplugin.UsageFieldSchema{"units": {Type: "number", Unit: "credit"}},
|
||||
expression: fmt.Sprintf(`u("units") == %d ? -1 : 0`, common.MaxQuota),
|
||||
expectedError: "result must be finite and non-negative",
|
||||
},
|
||||
{
|
||||
name: "negative enum combination",
|
||||
schema: videoSchema,
|
||||
expression: `u("mode") == "pro" && u("quality") == "hd" ? -1 : 0`,
|
||||
expectedError: "result must be finite and non-negative",
|
||||
},
|
||||
}
|
||||
|
||||
for _, testCase := range tests {
|
||||
t.Run(testCase.name, func(t *testing.T) {
|
||||
err := SmokeTestTaskExpr(testCase.expression, testCase.schema)
|
||||
if testCase.expectedError == "" {
|
||||
require.NoError(t, err)
|
||||
return
|
||||
}
|
||||
require.ErrorContains(t, err, testCase.expectedError)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSmokeTestTaskExprCapsOversizedEnumProductsAtLastCombination(t *testing.T) {
|
||||
schema := make(map[string]jsplugin.UsageFieldSchema, 7)
|
||||
condition := ""
|
||||
for index := 0; index < 7; index++ {
|
||||
schema[fmt.Sprintf("enum_%d", index)] = jsplugin.UsageFieldSchema{Enum: []string{"first", "middle", "last"}}
|
||||
if condition != "" {
|
||||
condition += " && "
|
||||
}
|
||||
condition += fmt.Sprintf(`u("enum_%d") == "last"`, index)
|
||||
}
|
||||
|
||||
err := SmokeTestTaskExpr(condition+" ? -1 : 0", schema)
|
||||
require.ErrorContains(t, err, "result must be finite and non-negative")
|
||||
}
|
||||
|
||||
func TestSmokeTestExprRejectsTaskUsageWithoutSchema(t *testing.T) {
|
||||
err := SmokeTestExpr(`u("mode") == "std" ? 1 : 2`)
|
||||
require.Error(t, err)
|
||||
assert.ErrorContains(t, err, "mode")
|
||||
assert.ErrorContains(t, err, "no task plugin usage schema")
|
||||
|
||||
require.NoError(t, SmokeTestExpr(`tier("base", p * 2 + c * 8)`))
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package system_setting
|
||||
|
||||
var ServerAddress = "http://localhost:3000"
|
||||
var TaskPublicAddress = ""
|
||||
var WorkerUrl = ""
|
||||
var WorkerValidKey = ""
|
||||
var WorkerAllowHttpImageRequestEnabled = false
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
package system_setting
|
||||
|
||||
import "github.com/QuantumNous/new-api/common"
|
||||
|
||||
const (
|
||||
DefaultTaskArtifactInvalidRateLimitPerMinute = 60
|
||||
DefaultTaskArtifactGlobalConcurrency = 128
|
||||
DefaultTaskArtifactIPConcurrency = 64
|
||||
DefaultTaskArtifactObjectConcurrency = 16
|
||||
)
|
||||
|
||||
const (
|
||||
TaskArtifactInvalidRateLimitEnv = "TASK_ARTIFACT_INVALID_RATE_LIMIT_PER_MINUTE"
|
||||
TaskArtifactGlobalLimitEnv = "TASK_ARTIFACT_GLOBAL_CONCURRENCY"
|
||||
TaskArtifactIPLimitEnv = "TASK_ARTIFACT_IP_CONCURRENCY"
|
||||
TaskArtifactObjectLimitEnv = "TASK_ARTIFACT_OBJECT_CONCURRENCY"
|
||||
)
|
||||
|
||||
type TaskArtifactAccessLimits struct {
|
||||
InvalidRatePerMinute int
|
||||
GlobalConcurrency int
|
||||
IPConcurrency int
|
||||
ObjectConcurrency int
|
||||
}
|
||||
|
||||
func positiveTaskArtifactLimit(env string, defaultValue int) int {
|
||||
value := common.GetEnvOrDefault(env, defaultValue)
|
||||
if value <= 0 {
|
||||
common.SysError(env + " must be positive; using default")
|
||||
return defaultValue
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
// LoadTaskArtifactAccessLimits reads startup configuration. Invalid and
|
||||
// non-positive values safely fall back to the documented defaults.
|
||||
func LoadTaskArtifactAccessLimits() TaskArtifactAccessLimits {
|
||||
return TaskArtifactAccessLimits{
|
||||
InvalidRatePerMinute: positiveTaskArtifactLimit(
|
||||
TaskArtifactInvalidRateLimitEnv,
|
||||
DefaultTaskArtifactInvalidRateLimitPerMinute,
|
||||
),
|
||||
GlobalConcurrency: positiveTaskArtifactLimit(
|
||||
TaskArtifactGlobalLimitEnv,
|
||||
DefaultTaskArtifactGlobalConcurrency,
|
||||
),
|
||||
IPConcurrency: positiveTaskArtifactLimit(
|
||||
TaskArtifactIPLimitEnv,
|
||||
DefaultTaskArtifactIPConcurrency,
|
||||
),
|
||||
ObjectConcurrency: positiveTaskArtifactLimit(
|
||||
TaskArtifactObjectLimitEnv,
|
||||
DefaultTaskArtifactObjectConcurrency,
|
||||
),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
package system_setting
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
"github.com/QuantumNous/new-api/common"
|
||||
)
|
||||
|
||||
const (
|
||||
TaskArtifactStoreModeUpstream = "upstream"
|
||||
TaskArtifactStoreModeS3 = "s3"
|
||||
|
||||
DefaultTaskArtifactStorePresignTTLSeconds = 900
|
||||
MaxTaskArtifactStorePresignTTLSeconds = 7 * 24 * 60 * 60
|
||||
)
|
||||
|
||||
const (
|
||||
TaskArtifactStoreModeEnv = "TASK_ARTIFACT_STORE_MODE"
|
||||
TaskArtifactStoreS3EndpointEnv = "TASK_ARTIFACT_STORE_S3_ENDPOINT"
|
||||
TaskArtifactStoreS3BucketEnv = "TASK_ARTIFACT_STORE_S3_BUCKET"
|
||||
TaskArtifactStoreS3RegionEnv = "TASK_ARTIFACT_STORE_S3_REGION"
|
||||
TaskArtifactStoreS3AccessKeyEnv = "TASK_ARTIFACT_STORE_S3_ACCESS_KEY"
|
||||
TaskArtifactStoreS3SecretKeyEnv = "TASK_ARTIFACT_STORE_S3_SECRET_KEY"
|
||||
TaskArtifactStoreS3PrefixEnv = "TASK_ARTIFACT_STORE_S3_PREFIX"
|
||||
TaskArtifactStoreS3PresignTTLEnv = "TASK_ARTIFACT_STORE_S3_PRESIGN_TTL"
|
||||
)
|
||||
|
||||
var (
|
||||
taskArtifactStoreBucketPattern = regexp.MustCompile(`^[a-z0-9][a-z0-9.-]{1,61}[a-z0-9]$`)
|
||||
taskArtifactStoreRegionPattern = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$`)
|
||||
)
|
||||
|
||||
// TaskArtifactStoreConfig reserves the configuration contract for a future S3
|
||||
// implementation. The current release always falls back to upstream proxying.
|
||||
type TaskArtifactStoreConfig struct {
|
||||
Mode string
|
||||
S3Endpoint string
|
||||
S3Bucket string
|
||||
S3Region string
|
||||
S3AccessKey string
|
||||
S3SecretKey string
|
||||
S3Prefix string
|
||||
S3PresignTTLSeconds int
|
||||
}
|
||||
|
||||
// LoadTaskArtifactStoreConfig reads and validates startup-only configuration.
|
||||
// S3 mode is deliberately disabled until a storage implementation is shipped.
|
||||
func LoadTaskArtifactStoreConfig() TaskArtifactStoreConfig {
|
||||
config := TaskArtifactStoreConfig{
|
||||
Mode: common.GetEnvOrDefaultString(TaskArtifactStoreModeEnv, TaskArtifactStoreModeUpstream),
|
||||
S3Endpoint: common.GetEnvOrDefaultString(TaskArtifactStoreS3EndpointEnv, ""),
|
||||
S3Bucket: common.GetEnvOrDefaultString(TaskArtifactStoreS3BucketEnv, ""),
|
||||
S3Region: common.GetEnvOrDefaultString(TaskArtifactStoreS3RegionEnv, ""),
|
||||
S3AccessKey: common.GetEnvOrDefaultString(TaskArtifactStoreS3AccessKeyEnv, ""),
|
||||
S3SecretKey: common.GetEnvOrDefaultString(TaskArtifactStoreS3SecretKeyEnv, ""),
|
||||
S3Prefix: common.GetEnvOrDefaultString(TaskArtifactStoreS3PrefixEnv, ""),
|
||||
S3PresignTTLSeconds: common.GetEnvOrDefault(TaskArtifactStoreS3PresignTTLEnv, DefaultTaskArtifactStorePresignTTLSeconds),
|
||||
}
|
||||
if err := ValidateTaskArtifactStoreConfig(config); err != nil {
|
||||
common.SysError("invalid task artifact store configuration: " + err.Error() + "; using upstream mode")
|
||||
config.Mode = TaskArtifactStoreModeUpstream
|
||||
return config
|
||||
}
|
||||
if config.Mode == TaskArtifactStoreModeS3 {
|
||||
common.SysError("task artifact S3 storage is not implemented; using upstream mode")
|
||||
config.Mode = TaskArtifactStoreModeUpstream
|
||||
}
|
||||
return config
|
||||
}
|
||||
|
||||
// ValidateTaskArtifactStoreConfig performs syntax checks only. It never
|
||||
// resolves hosts, contacts an endpoint, or verifies credentials.
|
||||
func ValidateTaskArtifactStoreConfig(config TaskArtifactStoreConfig) error {
|
||||
if config.Mode != TaskArtifactStoreModeUpstream && config.Mode != TaskArtifactStoreModeS3 {
|
||||
return fmt.Errorf("unsupported mode %q", config.Mode)
|
||||
}
|
||||
if config.S3PresignTTLSeconds <= 0 || config.S3PresignTTLSeconds > MaxTaskArtifactStorePresignTTLSeconds {
|
||||
return fmt.Errorf("S3 presign TTL must be between 1 and %d seconds", MaxTaskArtifactStorePresignTTLSeconds)
|
||||
}
|
||||
|
||||
requireS3Fields := config.Mode == TaskArtifactStoreModeS3
|
||||
if requireS3Fields && config.S3Endpoint == "" {
|
||||
return errors.New("S3 endpoint is required")
|
||||
}
|
||||
if config.S3Endpoint != "" {
|
||||
if config.S3Endpoint != strings.TrimSpace(config.S3Endpoint) {
|
||||
return errors.New("S3 endpoint must not contain surrounding whitespace")
|
||||
}
|
||||
endpoint, err := url.Parse(config.S3Endpoint)
|
||||
if err != nil || endpoint == nil || endpoint.Host == "" || endpoint.User != nil || endpoint.Opaque != "" {
|
||||
return errors.New("S3 endpoint must be an absolute URL without userinfo")
|
||||
}
|
||||
if endpoint.Scheme != "http" && endpoint.Scheme != "https" {
|
||||
return errors.New("S3 endpoint must use http or https")
|
||||
}
|
||||
if endpoint.RawQuery != "" || endpoint.ForceQuery || endpoint.Fragment != "" {
|
||||
return errors.New("S3 endpoint must not contain a query or fragment")
|
||||
}
|
||||
}
|
||||
|
||||
if requireS3Fields && config.S3Bucket == "" {
|
||||
return errors.New("S3 bucket is required")
|
||||
}
|
||||
if config.S3Bucket != "" {
|
||||
if !taskArtifactStoreBucketPattern.MatchString(config.S3Bucket) ||
|
||||
strings.Contains(config.S3Bucket, "..") || net.ParseIP(config.S3Bucket) != nil {
|
||||
return errors.New("S3 bucket syntax is invalid")
|
||||
}
|
||||
}
|
||||
|
||||
if requireS3Fields && config.S3Region == "" {
|
||||
return errors.New("S3 region is required")
|
||||
}
|
||||
if config.S3Region != "" && !taskArtifactStoreRegionPattern.MatchString(config.S3Region) {
|
||||
return errors.New("S3 region syntax is invalid")
|
||||
}
|
||||
if err := validateTaskArtifactStoreCredential("access key", config.S3AccessKey, 256, requireS3Fields); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := validateTaskArtifactStoreCredential("secret key", config.S3SecretKey, 1024, requireS3Fields); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if config.S3Prefix != "" {
|
||||
if config.S3Prefix != strings.TrimSpace(config.S3Prefix) || len(config.S3Prefix) > 512 ||
|
||||
strings.HasPrefix(config.S3Prefix, "/") || strings.Contains(config.S3Prefix, "\\") {
|
||||
return errors.New("S3 prefix syntax is invalid")
|
||||
}
|
||||
for _, part := range strings.Split(config.S3Prefix, "/") {
|
||||
if part == "." || part == ".." {
|
||||
return errors.New("S3 prefix must not contain dot segments")
|
||||
}
|
||||
}
|
||||
for _, character := range config.S3Prefix {
|
||||
if unicode.IsControl(character) {
|
||||
return errors.New("S3 prefix must not contain control characters")
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateTaskArtifactStoreCredential(name, value string, maxLength int, required bool) error {
|
||||
if required && value == "" {
|
||||
return fmt.Errorf("S3 %s is required", name)
|
||||
}
|
||||
if value == "" {
|
||||
return nil
|
||||
}
|
||||
if value != strings.TrimSpace(value) || len(value) > maxLength {
|
||||
return fmt.Errorf("S3 %s syntax is invalid", name)
|
||||
}
|
||||
for _, character := range value {
|
||||
if unicode.IsControl(character) {
|
||||
return fmt.Errorf("S3 %s syntax is invalid", name)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package system_setting
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestValidateTaskArtifactStoreConfig(t *testing.T) {
|
||||
valid := TaskArtifactStoreConfig{
|
||||
Mode: TaskArtifactStoreModeS3,
|
||||
S3Endpoint: "https://objects.example.com/storage",
|
||||
S3Bucket: "task-artifacts",
|
||||
S3Region: "us-east-1",
|
||||
S3AccessKey: "access-key",
|
||||
S3SecretKey: "secret-key",
|
||||
S3Prefix: "tasks/v1/",
|
||||
S3PresignTTLSeconds: 900,
|
||||
}
|
||||
require.NoError(t, ValidateTaskArtifactStoreConfig(valid))
|
||||
require.NoError(t, ValidateTaskArtifactStoreConfig(TaskArtifactStoreConfig{
|
||||
Mode: TaskArtifactStoreModeUpstream,
|
||||
S3PresignTTLSeconds: DefaultTaskArtifactStorePresignTTLSeconds,
|
||||
}))
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
mutate func(*TaskArtifactStoreConfig)
|
||||
match string
|
||||
}{
|
||||
{name: "mode", mutate: func(config *TaskArtifactStoreConfig) { config.Mode = "filesystem" }, match: "unsupported mode"},
|
||||
{name: "endpoint scheme", mutate: func(config *TaskArtifactStoreConfig) { config.S3Endpoint = "ftp://objects.example.com" }, match: "http or https"},
|
||||
{name: "endpoint credentials", mutate: func(config *TaskArtifactStoreConfig) { config.S3Endpoint = "https://user:pass@objects.example.com" }, match: "without userinfo"},
|
||||
{name: "endpoint query", mutate: func(config *TaskArtifactStoreConfig) { config.S3Endpoint = "https://objects.example.com?token=secret" }, match: "query or fragment"},
|
||||
{name: "bucket", mutate: func(config *TaskArtifactStoreConfig) { config.S3Bucket = "Invalid_Bucket" }, match: "bucket syntax"},
|
||||
{name: "IP bucket", mutate: func(config *TaskArtifactStoreConfig) { config.S3Bucket = "192.168.1.1" }, match: "bucket syntax"},
|
||||
{name: "region", mutate: func(config *TaskArtifactStoreConfig) { config.S3Region = "bad region" }, match: "region syntax"},
|
||||
{name: "access key", mutate: func(config *TaskArtifactStoreConfig) { config.S3AccessKey = " access-key" }, match: "access key syntax"},
|
||||
{name: "secret key", mutate: func(config *TaskArtifactStoreConfig) { config.S3SecretKey = "secret\nkey" }, match: "secret key syntax"},
|
||||
{name: "prefix root", mutate: func(config *TaskArtifactStoreConfig) { config.S3Prefix = "/tasks" }, match: "prefix syntax"},
|
||||
{name: "prefix traversal", mutate: func(config *TaskArtifactStoreConfig) { config.S3Prefix = "tasks/../private" }, match: "dot segments"},
|
||||
{name: "TTL zero", mutate: func(config *TaskArtifactStoreConfig) { config.S3PresignTTLSeconds = 0 }, match: "presign TTL"},
|
||||
{name: "TTL too long", mutate: func(config *TaskArtifactStoreConfig) {
|
||||
config.S3PresignTTLSeconds = MaxTaskArtifactStorePresignTTLSeconds + 1
|
||||
}, match: "presign TTL"},
|
||||
}
|
||||
for _, testCase := range tests {
|
||||
t.Run(testCase.name, func(t *testing.T) {
|
||||
config := valid
|
||||
testCase.mutate(&config)
|
||||
assert.ErrorContains(t, ValidateTaskArtifactStoreConfig(config), testCase.match)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadTaskArtifactStoreConfigFallsBackToUpstream(t *testing.T) {
|
||||
t.Setenv(TaskArtifactStoreModeEnv, "filesystem")
|
||||
t.Setenv(TaskArtifactStoreS3PresignTTLEnv, "900")
|
||||
config := LoadTaskArtifactStoreConfig()
|
||||
assert.Equal(t, TaskArtifactStoreModeUpstream, config.Mode)
|
||||
|
||||
t.Setenv(TaskArtifactStoreModeEnv, TaskArtifactStoreModeS3)
|
||||
t.Setenv(TaskArtifactStoreS3EndpointEnv, "https://objects.example.com")
|
||||
t.Setenv(TaskArtifactStoreS3BucketEnv, "task-artifacts")
|
||||
t.Setenv(TaskArtifactStoreS3RegionEnv, "us-east-1")
|
||||
t.Setenv(TaskArtifactStoreS3AccessKeyEnv, "access-key")
|
||||
t.Setenv(TaskArtifactStoreS3SecretKeyEnv, "secret-key")
|
||||
t.Setenv(TaskArtifactStoreS3PrefixEnv, "tasks/v1")
|
||||
t.Setenv(TaskArtifactStoreS3PresignTTLEnv, "600")
|
||||
config = LoadTaskArtifactStoreConfig()
|
||||
|
||||
assert.Equal(t, TaskArtifactStoreModeUpstream, config.Mode)
|
||||
assert.Equal(t, "https://objects.example.com", config.S3Endpoint)
|
||||
assert.Equal(t, 600, config.S3PresignTTLSeconds)
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package system_setting
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestLoadTaskArtifactAccessLimitsUsesPositiveEnvironmentValues(t *testing.T) {
|
||||
t.Setenv(TaskArtifactInvalidRateLimitEnv, "17")
|
||||
t.Setenv(TaskArtifactGlobalLimitEnv, "23")
|
||||
t.Setenv(TaskArtifactIPLimitEnv, "11")
|
||||
t.Setenv(TaskArtifactObjectLimitEnv, "7")
|
||||
|
||||
limits := LoadTaskArtifactAccessLimits()
|
||||
assert.Equal(t, 17, limits.InvalidRatePerMinute)
|
||||
assert.Equal(t, 23, limits.GlobalConcurrency)
|
||||
assert.Equal(t, 11, limits.IPConcurrency)
|
||||
assert.Equal(t, 7, limits.ObjectConcurrency)
|
||||
}
|
||||
|
||||
func TestLoadTaskArtifactAccessLimitsFallsBackForInvalidValues(t *testing.T) {
|
||||
t.Setenv(TaskArtifactInvalidRateLimitEnv, "0")
|
||||
t.Setenv(TaskArtifactGlobalLimitEnv, "-1")
|
||||
t.Setenv(TaskArtifactIPLimitEnv, "invalid")
|
||||
t.Setenv(TaskArtifactObjectLimitEnv, "")
|
||||
|
||||
limits := LoadTaskArtifactAccessLimits()
|
||||
assert.Equal(t, DefaultTaskArtifactInvalidRateLimitPerMinute, limits.InvalidRatePerMinute)
|
||||
assert.Equal(t, DefaultTaskArtifactGlobalConcurrency, limits.GlobalConcurrency)
|
||||
assert.Equal(t, DefaultTaskArtifactIPConcurrency, limits.IPConcurrency)
|
||||
assert.Equal(t, DefaultTaskArtifactObjectConcurrency, limits.ObjectConcurrency)
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
package setting
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/QuantumNous/new-api/common"
|
||||
)
|
||||
|
||||
const (
|
||||
TaskPluginMarketplaceSourcesKey = "TaskPluginMarketplaceSources"
|
||||
TaskPluginDisabledFactoryKeysKey = "TaskPluginDisabledFactoryKeys"
|
||||
|
||||
officialTaskPluginMarketplaceIndexURL = "https://www.newapi.ai/api/v1/plugins/index.json"
|
||||
githubTaskPluginMarketplaceIndexURL = "https://raw.githubusercontent.com/QuantumNous/new-api-plugins/main/index.json"
|
||||
)
|
||||
|
||||
type TaskPluginMarketplaceSource struct {
|
||||
Name string `json:"name"`
|
||||
IndexURL string `json:"index_url"`
|
||||
}
|
||||
|
||||
func defaultTaskPluginMarketplaceSources() []TaskPluginMarketplaceSource {
|
||||
return []TaskPluginMarketplaceSource{
|
||||
{Name: "Official", IndexURL: officialTaskPluginMarketplaceIndexURL},
|
||||
{Name: "GitHub", IndexURL: githubTaskPluginMarketplaceIndexURL},
|
||||
}
|
||||
}
|
||||
|
||||
func GetTaskPluginMarketplaceSources() []TaskPluginMarketplaceSource {
|
||||
common.OptionMapRWMutex.RLock()
|
||||
raw := ""
|
||||
if common.OptionMap != nil {
|
||||
raw = common.OptionMap[TaskPluginMarketplaceSourcesKey]
|
||||
}
|
||||
common.OptionMapRWMutex.RUnlock()
|
||||
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" {
|
||||
return defaultTaskPluginMarketplaceSources()
|
||||
}
|
||||
var sources []TaskPluginMarketplaceSource
|
||||
if err := common.UnmarshalJsonStr(raw, &sources); err != nil {
|
||||
return defaultTaskPluginMarketplaceSources()
|
||||
}
|
||||
if sources == nil {
|
||||
return []TaskPluginMarketplaceSource{}
|
||||
}
|
||||
return sources
|
||||
}
|
||||
|
||||
func TaskPluginMarketplaceSources2JsonString() string {
|
||||
encoded, err := common.Marshal(defaultTaskPluginMarketplaceSources())
|
||||
if err != nil {
|
||||
return "[]"
|
||||
}
|
||||
return string(encoded)
|
||||
}
|
||||
|
||||
func ParseTaskPluginDisabledFactoryKeys(raw string) []string {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" {
|
||||
return []string{}
|
||||
}
|
||||
var keys []string
|
||||
if err := common.Unmarshal([]byte(raw), &keys); err != nil {
|
||||
return []string{}
|
||||
}
|
||||
if keys == nil {
|
||||
return []string{}
|
||||
}
|
||||
return keys
|
||||
}
|
||||
|
||||
func GetTaskPluginDisabledFactoryKeys() []string {
|
||||
common.OptionMapRWMutex.RLock()
|
||||
raw := ""
|
||||
if common.OptionMap != nil {
|
||||
raw = common.OptionMap[TaskPluginDisabledFactoryKeysKey]
|
||||
}
|
||||
common.OptionMapRWMutex.RUnlock()
|
||||
return ParseTaskPluginDisabledFactoryKeys(raw)
|
||||
}
|
||||
|
||||
func SetTaskPluginDisabledFactoryKeysOption(keys []string) error {
|
||||
normalized := make([]string, 0, len(keys))
|
||||
seen := make(map[string]struct{}, len(keys))
|
||||
for _, key := range keys {
|
||||
key = strings.TrimSpace(key)
|
||||
if key == "" {
|
||||
continue
|
||||
}
|
||||
if _, exists := seen[key]; exists {
|
||||
continue
|
||||
}
|
||||
seen[key] = struct{}{}
|
||||
normalized = append(normalized, key)
|
||||
}
|
||||
sort.Strings(normalized)
|
||||
encoded, err := common.Marshal(normalized)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
common.OptionMapRWMutex.Lock()
|
||||
if common.OptionMap == nil {
|
||||
common.OptionMap = make(map[string]string)
|
||||
}
|
||||
common.OptionMap[TaskPluginDisabledFactoryKeysKey] = string(encoded)
|
||||
common.OptionMapRWMutex.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
func IsTaskPluginFactoryDisabled(key string) bool {
|
||||
for _, item := range GetTaskPluginDisabledFactoryKeys() {
|
||||
if item == key {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package setting
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/QuantumNous/new-api/common"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func setupTaskPluginDisabledFactoryKeysTest(t *testing.T) {
|
||||
t.Helper()
|
||||
originalMap := common.OptionMap
|
||||
common.OptionMapRWMutex.Lock()
|
||||
common.OptionMap = map[string]string{}
|
||||
common.OptionMapRWMutex.Unlock()
|
||||
t.Cleanup(func() {
|
||||
common.OptionMapRWMutex.Lock()
|
||||
common.OptionMap = originalMap
|
||||
common.OptionMapRWMutex.Unlock()
|
||||
})
|
||||
}
|
||||
|
||||
func TestTaskPluginDisabledFactoryKeysRoundTripAndDedupe(t *testing.T) {
|
||||
setupTaskPluginDisabledFactoryKeysTest(t)
|
||||
|
||||
assert.Empty(t, GetTaskPluginDisabledFactoryKeys())
|
||||
assert.False(t, IsTaskPluginFactoryDisabled("kling"))
|
||||
|
||||
require.NoError(t, SetTaskPluginDisabledFactoryKeysOption([]string{"kling", "sora", "kling", " hailuo "}))
|
||||
assert.Equal(t, []string{"hailuo", "kling", "sora"}, GetTaskPluginDisabledFactoryKeys())
|
||||
assert.Equal(t, `["hailuo","kling","sora"]`, common.OptionMap[TaskPluginDisabledFactoryKeysKey])
|
||||
assert.True(t, IsTaskPluginFactoryDisabled("kling"))
|
||||
assert.True(t, IsTaskPluginFactoryDisabled("hailuo"))
|
||||
assert.False(t, IsTaskPluginFactoryDisabled("google"))
|
||||
}
|
||||
|
||||
func TestTaskPluginDisabledFactoryKeysBadJSONReturnsEmpty(t *testing.T) {
|
||||
setupTaskPluginDisabledFactoryKeysTest(t)
|
||||
|
||||
for _, testCase := range []struct {
|
||||
name string
|
||||
raw string
|
||||
}{
|
||||
{name: "absent", raw: ""},
|
||||
{name: "null", raw: "null"},
|
||||
{name: "object", raw: "{}"},
|
||||
{name: "number", raw: "1"},
|
||||
{name: "truncated", raw: `["kling"`},
|
||||
{name: "not json", raw: "kling"},
|
||||
} {
|
||||
t.Run(testCase.name, func(t *testing.T) {
|
||||
common.OptionMapRWMutex.Lock()
|
||||
if testCase.raw == "" {
|
||||
delete(common.OptionMap, TaskPluginDisabledFactoryKeysKey)
|
||||
} else {
|
||||
common.OptionMap[TaskPluginDisabledFactoryKeysKey] = testCase.raw
|
||||
}
|
||||
common.OptionMapRWMutex.Unlock()
|
||||
|
||||
assert.Empty(t, GetTaskPluginDisabledFactoryKeys())
|
||||
assert.False(t, IsTaskPluginFactoryDisabled("kling"))
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package task_pricing_setting
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/QuantumNous/new-api/setting/config"
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
type TaskPricingSetting struct {
|
||||
SoraSizeRatio map[string]float64 `json:"sora_size_ratio"`
|
||||
VertexResolution4K map[string]float64 `json:"vertex_resolution_4k_ratio"`
|
||||
}
|
||||
|
||||
var taskPricingSetting = TaskPricingSetting{
|
||||
SoraSizeRatio: map[string]float64{
|
||||
"1792x1024": 1.666667,
|
||||
"1024x1792": 1.666667,
|
||||
},
|
||||
VertexResolution4K: map[string]float64{
|
||||
"veo-3.1-fast-generate": 2.333333,
|
||||
"veo-3.1-generate": 1.5,
|
||||
"veo-3.1": 1.5,
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
config.GlobalConfig.Register("task_pricing_setting", &taskPricingSetting)
|
||||
}
|
||||
|
||||
func SoraSizeRatio(size string) float64 {
|
||||
if ratio, ok := taskPricingSetting.SoraSizeRatio[size]; ok && ratio > 0 {
|
||||
return ratio
|
||||
}
|
||||
return 1
|
||||
}
|
||||
|
||||
func VertexResolutionRatio(model, resolution string) float64 {
|
||||
if !strings.EqualFold(resolution, "4k") {
|
||||
return 1
|
||||
}
|
||||
matchedPattern := ""
|
||||
matchedRatio := 1.0
|
||||
for pattern, ratio := range taskPricingSetting.VertexResolution4K {
|
||||
if !strings.Contains(model, pattern) || ratio <= 0 {
|
||||
continue
|
||||
}
|
||||
if len(pattern) > len(matchedPattern) || (len(pattern) == len(matchedPattern) && pattern < matchedPattern) {
|
||||
matchedPattern = pattern
|
||||
matchedRatio = ratio
|
||||
}
|
||||
}
|
||||
return matchedRatio
|
||||
}
|
||||
|
||||
func GetCopy() TaskPricingSetting {
|
||||
return TaskPricingSetting{
|
||||
SoraSizeRatio: lo.Assign(taskPricingSetting.SoraSizeRatio),
|
||||
VertexResolution4K: lo.Assign(taskPricingSetting.VertexResolution4K),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package task_pricing_setting
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/QuantumNous/new-api/setting/config"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestTaskPricingDefaultsAndOptionUpdate(t *testing.T) {
|
||||
original := GetCopy()
|
||||
t.Cleanup(func() { taskPricingSetting = original })
|
||||
assert.InDelta(t, 1.666667, SoraSizeRatio("1792x1024"), 0.000001)
|
||||
assert.InDelta(t, 2.333333, VertexResolutionRatio("veo-3.1-fast-generate-preview", "4K"), 0.000001)
|
||||
require.NoError(t, config.UpdateConfigFromMap(&taskPricingSetting, map[string]string{"sora_size_ratio": `{"1792x1024":2}`}))
|
||||
assert.Equal(t, 2.0, SoraSizeRatio("1792x1024"))
|
||||
assert.Equal(t, 1.0, SoraSizeRatio("720x1280"))
|
||||
}
|
||||
|
||||
func TestVertexResolutionRatioPrefersMostSpecificModelPattern(t *testing.T) {
|
||||
original := GetCopy()
|
||||
t.Cleanup(func() { taskPricingSetting = original })
|
||||
taskPricingSetting.VertexResolution4K = map[string]float64{
|
||||
"veo-3.1": 1.5,
|
||||
"veo-3.1-fast-generate": 2.333333,
|
||||
"veo-3.1-fast-generate-preview": 3,
|
||||
}
|
||||
|
||||
assert.Equal(t, 3.0, VertexResolutionRatio("veo-3.1-fast-generate-preview", "4K"))
|
||||
assert.Equal(t, 2.333333, VertexResolutionRatio("veo-3.1-fast-generate", "4k"))
|
||||
assert.Equal(t, 1.5, VertexResolutionRatio("veo-3.1-generate", "4K"))
|
||||
}
|
||||
Reference in New Issue
Block a user