修复展示遮罩挡住 修复最大字数

This commit is contained in:
2026-07-01 16:44:29 +08:00
parent 9be5c1b31f
commit 57a59c7c76
9 changed files with 75 additions and 78 deletions
@@ -70,7 +70,7 @@ func (h *UserGenerationHandler) Generate(c *gin.Context) {
switch {
case errors.Is(err, service.ErrUnknownModel):
c.JSON(http.StatusNotFound, gin.H{"detail": err.Error()})
case errors.Is(err, service.ErrUnsupportedParams):
case errors.Is(err, service.ErrUnsupportedParams), errors.Is(err, service.ErrPromptTooLong):
c.JSON(http.StatusBadRequest, gin.H{"detail": err.Error()})
case errors.Is(err, service.ErrInsufficientFunds):
c.JSON(http.StatusPaymentRequired, gin.H{"detail": "积分不足"})
@@ -132,7 +132,7 @@ func (h *UserGenerationHandler) Test(c *gin.Context) {
switch {
case errors.Is(err, service.ErrUnknownModel):
c.JSON(http.StatusNotFound, gin.H{"detail": err.Error()})
case errors.Is(err, service.ErrUnsupportedParams):
case errors.Is(err, service.ErrUnsupportedParams), errors.Is(err, service.ErrPromptTooLong):
c.JSON(http.StatusBadRequest, gin.H{"detail": err.Error()})
case errors.Is(err, service.ErrProviderQuota):
c.JSON(http.StatusTooManyRequests, gin.H{"detail": err.Error()})
+1 -1
View File
@@ -316,7 +316,7 @@ func (h *V1Handler) writeV1Error(c *gin.Context, err error, payload map[string]a
switch {
case errors.Is(err, service.ErrUnknownModel):
c.JSON(http.StatusNotFound, gin.H{"detail": err.Error()})
case errors.Is(err, service.ErrUnsupportedParams):
case errors.Is(err, service.ErrUnsupportedParams), errors.Is(err, service.ErrPromptTooLong):
c.JSON(http.StatusBadRequest, gin.H{"detail": err.Error()})
case errors.Is(err, service.ErrInsufficientFunds):
c.JSON(http.StatusPaymentRequired, gin.H{"detail": err.Error()})
+12
View File
@@ -14,6 +14,7 @@ import (
"sync"
"sync/atomic"
"time"
"unicode/utf8"
"strconv"
@@ -37,6 +38,7 @@ var (
ErrInvalidAPIKey = errors.New("invalid api key")
ErrUnknownModel = errors.New("unknown model")
ErrUnsupportedParams = errors.New("unsupported or unpriced parameters for this model")
ErrPromptTooLong = errors.New("prompt too long (max 2000 characters)")
ErrInsufficientFunds = errors.New("insufficient credits")
ErrGenerationPending = errors.New("generation executor not implemented yet")
ErrProviderAuth = errors.New("provider token invalid or expired")
@@ -57,6 +59,10 @@ var (
ErrVideoNotReady = errors.New("video is not ready yet")
)
// maxPromptRunes caps prompt length (counted as characters, so Chinese = 1),
// matching the 画图台 counter. Enforced for both 画图台 and API-key calls.
const maxPromptRunes = 2000
// maxReferenceImageBytes bounds a single decoded reference image. 8 MB
// comfortably covers real photos/screenshots; anything larger is almost
// certainly abuse or a mistake. Mirrors Python core/refs.py.
@@ -939,6 +945,9 @@ func (s *V1Service) prepareImage(ctx context.Context, principal *APIPrincipal, i
if modelID == "" || prompt == "" {
return nil, "", "", 0, errors.New("model and prompt required")
}
if utf8.RuneCountInString(prompt) > maxPromptRunes {
return nil, "", "", 0, ErrPromptTooLong
}
modelItem, err := s.models.Get(ctx, modelID)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
@@ -1002,6 +1011,9 @@ func (s *V1Service) prepareVideo(ctx context.Context, principal *APIPrincipal, i
if modelID == "" || prompt == "" {
return nil, "", "", "", 0, errors.New("model and prompt required")
}
if utf8.RuneCountInString(prompt) > maxPromptRunes {
return nil, "", "", "", 0, ErrPromptTooLong
}
if duration == "" {
return nil, "", "", "", 0, errors.New("duration required")
}