feat(upstream): 自定义上游支持 Vivid 协议(/v1/generate NDJSON)

- custom 上游新增 protocol 选项(openai|vivid);vivid 走
  POST /v1/generate NDJSON 流:跳过心跳空行,取末行终态,
  断流携 jobId 重连不重复扣费;参考图先 POST /v1/uploads
  拿预签名再 PUT 直传;错误码 4011/4002/4293/5001/4001 分类
- 添加上游弹窗加协议二选一;账号 meta 存 protocol 并可回显
- 附 vivid 链路单测(成功流/URL 透传/失败帧/错误码/两步上传)

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
2026-10-14 12:18:26 +08:00
co-authored by Devin
parent ae49b95e0f
commit babfc71f13
6 changed files with 513 additions and 26 deletions
+26 -15
View File
@@ -2,24 +2,24 @@ package service
import (
"context"
"sync"
"sync/atomic"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"log"
"strings"
"sync"
"sync/atomic"
"time"
"backend/internal/model"
"backend/internal/provider/adobe"
"backend/internal/provider/chatgpt"
"backend/internal/provider/creativefabrica"
"backend/internal/provider/grok"
"backend/internal/provider/imagine"
"backend/internal/provider/krea"
"backend/internal/provider/leonardo"
"backend/internal/provider/grok"
"backend/internal/provider/runway"
"backend/internal/repo"
@@ -28,14 +28,14 @@ import (
)
var validTokenPools = map[string]string{
"chatgpt": "openai",
"adobe": "adobe",
"runway": "runway",
"leonardo": "leonardo",
"krea": "krea",
"imagine": "imagine",
"grok": "grok",
"custom": "custom",
"chatgpt": "openai",
"adobe": "adobe",
"runway": "runway",
"leonardo": "leonardo",
"krea": "krea",
"imagine": "imagine",
"grok": "grok",
"custom": "custom",
"creativefabrica": "creativefabrica",
}
@@ -1036,7 +1036,6 @@ func (s *TokenService) ImportGrokToken(ctx context.Context, ssoToken, tokenID st
return item, nil
}
func (s *TokenService) checkPendingGrok(tokenID, ssoToken string) {
defer func() {
if r := recover(); r != nil {
@@ -1217,9 +1216,10 @@ func (s *TokenService) checkPendingCreativeFabrica(tokenID, cookie string) {
// csv list of model ids it serves (empty = all), plus optional weight and
// per-account concurrency. No probe — the account goes active immediately and is
// matched to custom models by id at generation time. Calls go direct (no proxy).
func (s *TokenService) ImportCustomAccount(ctx context.Context, baseURL, apiKey, models, name string, weight, concurrency int, tokenID string) (*model.TokenAccount, error) {
func (s *TokenService) ImportCustomAccount(ctx context.Context, baseURL, apiKey, models, name, protocol string, weight, concurrency int, tokenID string) (*model.TokenAccount, error) {
baseURL = strings.TrimRight(strings.TrimSpace(baseURL), "/")
apiKey = strings.TrimSpace(apiKey)
protocol = strings.ToLower(strings.TrimSpace(protocol))
// Edit mode: tokenID points at an existing custom account. base_url required;
// a blank key keeps the stored one.
if strings.TrimSpace(tokenID) != "" {
@@ -1230,7 +1230,14 @@ func (s *TokenService) ImportCustomAccount(ctx context.Context, baseURL, apiKey,
if baseURL == "" {
return nil, errors.New("base_url required")
}
meta := datatypes.JSONMap{"base_url": baseURL}
proto := protocol
if proto == "" {
proto = strings.ToLower(strings.TrimSpace(stringValue(existing.Meta["protocol"])))
}
if proto != "vivid" {
proto = "openai"
}
meta := datatypes.JSONMap{"base_url": baseURL, "protocol": proto}
if m := strings.TrimSpace(models); m != "" {
meta["models"] = m
}
@@ -1248,7 +1255,10 @@ func (s *TokenService) ImportCustomAccount(ctx context.Context, baseURL, apiKey,
if baseURL == "" || apiKey == "" {
return nil, errors.New("base_url and key required")
}
meta := datatypes.JSONMap{"base_url": baseURL}
if protocol != "vivid" {
protocol = "openai"
}
meta := datatypes.JSONMap{"base_url": baseURL, "protocol": protocol}
if m := strings.TrimSpace(models); m != "" {
meta["models"] = m
}
@@ -1952,6 +1962,7 @@ func accountRow(item model.TokenAccount, inFlight int64) map[string]any {
"weight": item.Weight,
"concurrency": item.Concurrency,
"base_url": emptyToNil(strings.TrimSpace(stringValue(item.Meta["base_url"]))),
"protocol": emptyToNil(strings.TrimSpace(stringValue(item.Meta["protocol"]))),
"models": strings.TrimSpace(stringValue(item.Meta["models"])),
}
}