并发分组(新功能): - 新表 concurrency_groups(名称/上限/默认),用户加 concurrency_group_id - 启动自动建「默认并发」组(上限10、默认),老用户回填、新注册自动绑定 - 并发计数改用 Redis(自愈 sorted-set + TTL + fail-open): · 用户并发(画图台 + API key 合计)受其分组上限限制,0=不限制 → 超返回 429 · 账号级并发也从内存 gate 换成同一套 Redis(6 处调用点) · 移除旧的「已有正在生成的任务」单任务锁 - 后台「并发分组」新菜单:增删改、设默认、用户数;默认组不可删(删别的组成员转默认) - 用户管理:并发列 + 新建/编辑可选分组 - 个人设置页:账户信息卡(用户名/邮箱/角色/余额/并发);/me 暴露 concurrency_group/limit 品牌 / Logo(上传到 RustFS): - Logo 改成拖拽/点击上传,点保存才上传;替换自动删旧;branding/ 设为公开且被清理任务 pin 住(永不删) - 有自定义就用:前台左侧 nav + 后台侧栏 + favicon(浏览器标签);没有则默认 V 图标 - 前台页头还原成文字;首页 Hero 子标题用 site.subtitle(默认那句宣传语,设置页预填) - 邮件验证码标题用站点名;新增 POST/DELETE /settings/logo + POST /settings/asset(首页底图上传) 兑换码开关: - 系统设置→积分 新增「开启兑换码」(默认开);关闭后后端拒绝兑换、前台隐藏兑换入口(/site 暴露 cdk_redeem_enabled) 文档 / 分辨率: - 去掉 quality 参数:size(宽x高)同时决定比例 + 分辨率档(长边映射 1K/2K/4K) - 文档加「分辨率对照表」(14 个比例 × 1K/2K/4K → size 该传的值);guessRatio 与自定义模型 RATIO_OPTS 对齐到 14 个 其它: - 删模型时同步清掉各上游账号「支持模型」里的该 id - 首页设置/兑换码弹窗去固定高度滚动条;展示位弹窗浅色主题适配 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
196 lines
5.2 KiB
Go
196 lines
5.2 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"strings"
|
|
|
|
"backend/internal/model"
|
|
"backend/internal/repo"
|
|
)
|
|
|
|
type UserGenerationService struct {
|
|
v1 *V1Service
|
|
events *repo.EventRepository
|
|
users *repo.UserRepository
|
|
models *repo.ModelRepository
|
|
}
|
|
|
|
func NewUserGenerationService(v1 *V1Service, events *repo.EventRepository, users *repo.UserRepository, models *repo.ModelRepository) *UserGenerationService {
|
|
return &UserGenerationService{
|
|
v1: v1,
|
|
events: events,
|
|
users: users,
|
|
models: models,
|
|
}
|
|
}
|
|
|
|
type UserGenerateRequest struct {
|
|
Model string
|
|
Prompt string
|
|
Ratio string
|
|
Resolution string
|
|
Duration string
|
|
ReferenceImages []string
|
|
}
|
|
|
|
func (s *UserGenerationService) Generate(ctx context.Context, user *model.User, in UserGenerateRequest) (map[string]any, error) {
|
|
if user == nil || strings.TrimSpace(user.ID) == "" {
|
|
return nil, errors.New("未登录或会话已过期")
|
|
}
|
|
// No single-job lock anymore — concurrent generations are allowed, capped by
|
|
// the user's concurrency group (enforced in prepareImageExecution/Video).
|
|
|
|
modelItem, err := s.models.Get(ctx, strings.TrimSpace(in.Model))
|
|
if err != nil {
|
|
return nil, ErrUnknownModel
|
|
}
|
|
|
|
principal := &APIPrincipal{
|
|
User: user,
|
|
TokenType: "session",
|
|
}
|
|
|
|
switch modelItem.Type {
|
|
case "video":
|
|
resp, err := s.v1.prepareSessionVideo(ctx, principal, V1VideoRequest{
|
|
Model: in.Model,
|
|
Prompt: in.Prompt,
|
|
Duration: in.Duration,
|
|
AspectRatio: in.Ratio,
|
|
Resolution: in.Resolution,
|
|
ReferenceImages: in.ReferenceImages,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return resp, nil
|
|
default:
|
|
resp, err := s.v1.prepareSessionImage(ctx, principal, V1ImageRequest{
|
|
Model: in.Model,
|
|
Prompt: in.Prompt,
|
|
AspectRatio: in.Ratio,
|
|
Resolution: in.Resolution,
|
|
ReferenceImages: in.ReferenceImages,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return resp, nil
|
|
}
|
|
}
|
|
|
|
func (s *UserGenerationService) AdminTest(ctx context.Context, user *model.User, in UserGenerateRequest) (map[string]any, error) {
|
|
if user == nil || strings.TrimSpace(user.ID) == "" {
|
|
return nil, errors.New("未登录或会话已过期")
|
|
}
|
|
modelItem, err := s.models.Get(ctx, strings.TrimSpace(in.Model))
|
|
if err != nil {
|
|
return nil, ErrUnknownModel
|
|
}
|
|
principal := &APIPrincipal{
|
|
User: user,
|
|
TokenType: "session",
|
|
}
|
|
switch modelItem.Type {
|
|
case "video":
|
|
return s.v1.prepareAdminTestVideo(ctx, principal, V1VideoRequest{
|
|
Model: in.Model,
|
|
Prompt: in.Prompt,
|
|
Duration: in.Duration,
|
|
AspectRatio: in.Ratio,
|
|
Resolution: in.Resolution,
|
|
ReferenceImages: in.ReferenceImages,
|
|
})
|
|
default:
|
|
return s.v1.prepareAdminTestImage(ctx, principal, V1ImageRequest{
|
|
Model: in.Model,
|
|
Prompt: in.Prompt,
|
|
AspectRatio: in.Ratio,
|
|
Resolution: in.Resolution,
|
|
ReferenceImages: in.ReferenceImages,
|
|
})
|
|
}
|
|
}
|
|
|
|
func (s *UserGenerationService) MyJobs(ctx context.Context, user *model.User, source string) (map[string]any, error) {
|
|
if user == nil || strings.TrimSpace(user.ID) == "" {
|
|
return map[string]any{"pending": nil, "latest": nil}, nil
|
|
}
|
|
// source scopes the lookup: "user" = 画图台(默认),"admin" = 后台测试模型。
|
|
// Both are this caller's own events; the admin-test poll uses "admin" so a
|
|
// gateway-timed-out (524) test can still recover its result.
|
|
if source != "admin" {
|
|
source = "user"
|
|
}
|
|
pending, err := s.events.PendingByUser(ctx, user.ID, source)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
latest, err := s.events.LatestByUser(ctx, user.ID, source)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return map[string]any{
|
|
"pending": shapeJobEvent(pending),
|
|
"latest": shapeJobEvent(latest),
|
|
}, nil
|
|
}
|
|
|
|
func shapeJobEvent(item *model.EventLog) map[string]any {
|
|
if item == nil {
|
|
return nil
|
|
}
|
|
status := item.Status
|
|
url := ""
|
|
if strings.TrimSpace(item.File) != "" {
|
|
url = "/images/" + strings.ReplaceAll(strings.TrimSpace(item.File), "\\", "/")
|
|
}
|
|
return map[string]any{
|
|
"id": item.ID,
|
|
"kind": item.Kind,
|
|
"model": item.Model,
|
|
"prompt": item.Prompt,
|
|
"ratio": item.Ratio,
|
|
"resolution": item.Resolution,
|
|
"duration": item.Duration,
|
|
"status": status,
|
|
"file": emptyOrNil(item.File),
|
|
"url": emptyOrNil(url),
|
|
"reference_urls": referenceURLs(item.RefFiles),
|
|
"elapsed_ms": item.ElapsedMS,
|
|
"error": emptyOrNil(item.Error),
|
|
"charged": item.Cost,
|
|
"cost": item.Cost,
|
|
"ts": item.TS.Unix(),
|
|
}
|
|
}
|
|
|
|
// referenceURLs turns the stored relative reference paths into /images URLs so
|
|
// the playground can re-display the uploaded reference image(s) after a reload.
|
|
func referenceURLs(raw []byte) []string {
|
|
if len(raw) == 0 {
|
|
return []string{}
|
|
}
|
|
var paths []string
|
|
if err := json.Unmarshal(raw, &paths); err != nil {
|
|
return []string{}
|
|
}
|
|
out := make([]string, 0, len(paths))
|
|
for _, p := range paths {
|
|
p = strings.ReplaceAll(strings.TrimSpace(p), "\\", "/")
|
|
if p != "" {
|
|
out = append(out, "/images/"+p)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func emptyOrNil(v string) any {
|
|
if strings.TrimSpace(v) == "" {
|
|
return nil
|
|
}
|
|
return v
|
|
}
|