Compare commits
1 Commits
main
...
codex/fix-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
12cb6e0a09 |
12
.env.example
12
.env.example
@ -9,13 +9,6 @@
|
|||||||
# Generate with: openssl rand -base64 32
|
# Generate with: openssl rand -base64 32
|
||||||
AUTH_SECRET=replace-with-random-secret
|
AUTH_SECRET=replace-with-random-secret
|
||||||
|
|
||||||
# Stable pepper for user Access Key hashes. Generate independently from AUTH_SECRET.
|
|
||||||
ACCESS_KEY_PEPPER=replace-with-random-secret
|
|
||||||
|
|
||||||
# Authorizes trusted third-party services to provision user-scoped MCP keys.
|
|
||||||
# Must contain at least 32 characters. Never expose it to end users or browsers.
|
|
||||||
MCP_KEY_PROVISIONING_SECRET=replace-with-random-secret
|
|
||||||
|
|
||||||
# Public application URL. Required for production and self-hosted Auth.js.
|
# Public application URL. Required for production and self-hosted Auth.js.
|
||||||
AUTH_URL=https://your-domain.com
|
AUTH_URL=https://your-domain.com
|
||||||
NEXTAUTH_URL=https://your-domain.com
|
NEXTAUTH_URL=https://your-domain.com
|
||||||
@ -153,9 +146,8 @@ PRODUCT_PLANNING_DB_SSL=false
|
|||||||
# Home Performance / Queues
|
# Home Performance / Queues
|
||||||
# -----------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------
|
||||||
|
|
||||||
# Legacy image queue fields: 0 = unrestricted; model/user admission caps are disabled.
|
CANVAS_IMAGE_TASK_CONCURRENCY=3
|
||||||
CANVAS_IMAGE_TASK_CONCURRENCY=0
|
CANVAS_IMAGE_TASK_BATCH_DISPATCH_INTERVAL_MS=1000
|
||||||
CANVAS_IMAGE_TASK_BATCH_DISPATCH_INTERVAL_MS=0
|
|
||||||
EXTERNAL_IMAGE_API_MAX_CONCURRENT=3
|
EXTERNAL_IMAGE_API_MAX_CONCURRENT=3
|
||||||
EXTERNAL_IMAGE_API_MAX_QUEUE=20
|
EXTERNAL_IMAGE_API_MAX_QUEUE=20
|
||||||
EXTERNAL_IMAGE_API_QUEUE_TIMEOUT_MS=120000
|
EXTERNAL_IMAGE_API_QUEUE_TIMEOUT_MS=120000
|
||||||
|
|||||||
20
.gitattributes
vendored
20
.gitattributes
vendored
@ -1,20 +0,0 @@
|
|||||||
# Keep source checkouts consistent across Windows and Linux.
|
|
||||||
.gitattributes text eol=lf
|
|
||||||
.gitignore text eol=lf
|
|
||||||
*.ts text eol=lf
|
|
||||||
*.tsx text eol=lf
|
|
||||||
*.js text eol=lf
|
|
||||||
*.jsx text eol=lf
|
|
||||||
*.mjs text eol=lf
|
|
||||||
*.cjs text eol=lf
|
|
||||||
*.json text eol=lf
|
|
||||||
*.jsonc text eol=lf
|
|
||||||
*.yaml text eol=lf
|
|
||||||
*.yml text eol=lf
|
|
||||||
*.css text eol=lf
|
|
||||||
*.html text eol=lf
|
|
||||||
*.md text eol=lf
|
|
||||||
*.sh text eol=lf
|
|
||||||
*.py text eol=lf
|
|
||||||
*.bat text eol=crlf
|
|
||||||
*.cmd text eol=crlf
|
|
||||||
40
.gitignore
vendored
40
.gitignore
vendored
@ -11,9 +11,6 @@ coverage
|
|||||||
|
|
||||||
# next.js
|
# next.js
|
||||||
.next/
|
.next/
|
||||||
.next-build/
|
|
||||||
.next-previous/
|
|
||||||
/.next-*/
|
|
||||||
out/
|
out/
|
||||||
build
|
build
|
||||||
|
|
||||||
@ -51,7 +48,6 @@ yarn-error.log*
|
|||||||
/playwright-report/
|
/playwright-report/
|
||||||
/blob-report/
|
/blob-report/
|
||||||
/playwright/*
|
/playwright/*
|
||||||
/output/
|
|
||||||
deploy.tar.gz
|
deploy.tar.gz
|
||||||
libvips-8.14.5-linux-x64.tar.br
|
libvips-8.14.5-linux-x64.tar.br
|
||||||
.temp-packages/
|
.temp-packages/
|
||||||
@ -68,19 +64,10 @@ migration-backup-complete/
|
|||||||
migration-*.log
|
migration-*.log
|
||||||
migration-*.txt
|
migration-*.txt
|
||||||
|
|
||||||
# Local release backups, model credentials and migration scratch data.
|
|
||||||
/.model-config-backups/
|
|
||||||
/backups/
|
|
||||||
/tmp/
|
|
||||||
|
|
||||||
# 本地抓包/竞品参考产物,不能提交
|
# 本地抓包/竞品参考产物,不能提交
|
||||||
artifacts/liblib-*/
|
artifacts/liblib-*/
|
||||||
analysis/qiaodou-extract/
|
analysis/qiaodou-extract/
|
||||||
|
|
||||||
# 本地运行时数据库与上传文件
|
|
||||||
.local/
|
|
||||||
public/uploads/local/
|
|
||||||
|
|
||||||
# 模型文件
|
# 模型文件
|
||||||
*.model
|
*.model
|
||||||
*.pth
|
*.pth
|
||||||
@ -111,30 +98,3 @@ public/vendor/zoo-modeling-app/
|
|||||||
|
|
||||||
# Drizzle migrations must be versioned (override the global *.sql ignore above)
|
# Drizzle migrations must be versioned (override the global *.sql ignore above)
|
||||||
!lib/db/migrations/*.sql
|
!lib/db/migrations/*.sql
|
||||||
|
|
||||||
# Private smart edit runtime and media/model caches
|
|
||||||
.venv-smart-edit/
|
|
||||||
.smart-edit-models/
|
|
||||||
.cache/smart-edit/
|
|
||||||
|
|
||||||
services/video-render/dist/
|
|
||||||
|
|
||||||
# Downloaded and normalized subtitle fonts are runtime assets.
|
|
||||||
.cache/smart-edit-fonts/
|
|
||||||
services/smart-edit/__pycache__/
|
|
||||||
|
|
||||||
# Local verification artefacts (also excluded from Tailwind's automatic source scan).
|
|
||||||
.qa/
|
|
||||||
.next-video-*/
|
|
||||||
.next-tvstar-*/
|
|
||||||
|
|
||||||
services/video-upscale/dist/
|
|
||||||
services/video-upscale/__pycache__/
|
|
||||||
services/video-upscale/vendor/__pycache__/
|
|
||||||
|
|
||||||
/services/audio-processing/dist/
|
|
||||||
/services/audio-processing/__pycache__/
|
|
||||||
|
|
||||||
# Python release-tool and test bytecode.
|
|
||||||
__pycache__/
|
|
||||||
*.pyc
|
|
||||||
|
|||||||
@ -94,7 +94,7 @@ pnpm db:generate
|
|||||||
推荐提交前至少运行:
|
推荐提交前至少运行:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
git fetch origin --prune
|
git fetch github main --prune
|
||||||
pnpm check:branch
|
pnpm check:branch
|
||||||
pnpm check:quality
|
pnpm check:quality
|
||||||
pnpm audit:security
|
pnpm audit:security
|
||||||
|
|||||||
@ -75,8 +75,6 @@ export function LocalAiImagePipelineSections({
|
|||||||
aiWashConfig,
|
aiWashConfig,
|
||||||
setAiWashConfig,
|
setAiWashConfig,
|
||||||
aiWashModeIconUploading,
|
aiWashModeIconUploading,
|
||||||
canvasLocalEditConfig,
|
|
||||||
setCanvasLocalEditConfig,
|
|
||||||
extendConfig,
|
extendConfig,
|
||||||
setExtendConfig,
|
setExtendConfig,
|
||||||
watermarkRemoveConfig,
|
watermarkRemoveConfig,
|
||||||
@ -105,7 +103,6 @@ export function LocalAiImagePipelineSections({
|
|||||||
handleMoveEnhanceProPreset,
|
handleMoveEnhanceProPreset,
|
||||||
handleAiWashModeIconUpload,
|
handleAiWashModeIconUpload,
|
||||||
handleSaveAiWashConfig,
|
handleSaveAiWashConfig,
|
||||||
handleSaveCanvasLocalEditConfig,
|
|
||||||
handleSaveExtendConfig,
|
handleSaveExtendConfig,
|
||||||
handleSaveWatermarkRemoveConfig,
|
handleSaveWatermarkRemoveConfig,
|
||||||
handleSaveAngleEditConfig,
|
handleSaveAngleEditConfig,
|
||||||
@ -159,15 +156,12 @@ export function LocalAiImagePipelineSections({
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
label:
|
label:
|
||||||
rmbgConfig.provider === "vip98k"
|
rmbgConfig.apiKey ||
|
||||||
? "服务商 Key 已配置"
|
|
||||||
: rmbgConfig.apiKey ||
|
|
||||||
rmbgConfig.photoroomApiKey ||
|
rmbgConfig.photoroomApiKey ||
|
||||||
rmbgConfig.aosApiKey
|
rmbgConfig.aosApiKey
|
||||||
? "Key 已配置"
|
? "Key 已配置"
|
||||||
: "Key 待配置",
|
: "Key 待配置",
|
||||||
tone:
|
tone:
|
||||||
rmbgConfig.provider === "vip98k" ||
|
|
||||||
rmbgConfig.apiKey ||
|
rmbgConfig.apiKey ||
|
||||||
rmbgConfig.photoroomApiKey ||
|
rmbgConfig.photoroomApiKey ||
|
||||||
rmbgConfig.aosApiKey
|
rmbgConfig.aosApiKey
|
||||||
@ -182,26 +176,7 @@ export function LocalAiImagePipelineSections({
|
|||||||
{/* 服务提供商选择 */}
|
{/* 服务提供商选择 */}
|
||||||
<div className="space-y-3">
|
<div className="space-y-3">
|
||||||
<Label>抠图服务提供商</Label>
|
<Label>抠图服务提供商</Label>
|
||||||
<div className="grid grid-cols-1 gap-4 md:grid-cols-4">
|
<div className="grid grid-cols-1 gap-4 md:grid-cols-3">
|
||||||
<div
|
|
||||||
className={`relative flex cursor-pointer items-center gap-3 rounded-lg border p-4 transition-all hover:bg-accent ${
|
|
||||||
rmbgConfig.provider === "vip98k"
|
|
||||||
? "border-primary bg-accent ring-1 ring-primary"
|
|
||||||
: ""
|
|
||||||
}`}
|
|
||||||
onClick={() => setRmbgConfig({ ...rmbgConfig, provider: "vip98k" })}
|
|
||||||
>
|
|
||||||
<div
|
|
||||||
className={`flex h-4 w-4 shrink-0 items-center justify-center rounded-full border border-primary ${
|
|
||||||
rmbgConfig.provider === "vip98k"
|
|
||||||
? "bg-primary text-primary-foreground"
|
|
||||||
: "bg-transparent"
|
|
||||||
}`}
|
|
||||||
>
|
|
||||||
{rmbgConfig.provider === "vip98k" && <Check className="h-3 w-3" />}
|
|
||||||
</div>
|
|
||||||
<p className="font-medium text-sm leading-none">VIP98K 统一服务</p>
|
|
||||||
</div>
|
|
||||||
<div
|
<div
|
||||||
className={`relative flex cursor-pointer items-center gap-3 rounded-lg border p-4 transition-all hover:bg-accent ${
|
className={`relative flex cursor-pointer items-center gap-3 rounded-lg border p-4 transition-all hover:bg-accent ${
|
||||||
rmbgConfig.provider === "rmbg"
|
rmbgConfig.provider === "rmbg"
|
||||||
@ -288,13 +263,6 @@ export function LocalAiImagePipelineSections({
|
|||||||
|
|
||||||
<Separator />
|
<Separator />
|
||||||
|
|
||||||
{rmbgConfig.provider === "vip98k" && (
|
|
||||||
<div className="rounded-lg border bg-muted/40 p-4 text-sm">
|
|
||||||
使用 VIP98K 的 remove-background 模型。API Key 从“AI 模型设置”中的
|
|
||||||
VIP98K 服务商读取,不会在此重复保存。
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{/* RMBG 配置 */}
|
{/* RMBG 配置 */}
|
||||||
{rmbgConfig.provider === "rmbg" && (
|
{rmbgConfig.provider === "rmbg" && (
|
||||||
<>
|
<>
|
||||||
@ -500,7 +468,7 @@ export function LocalAiImagePipelineSections({
|
|||||||
<LocalAiSectionShell
|
<LocalAiSectionShell
|
||||||
description={
|
description={
|
||||||
<>
|
<>
|
||||||
配置图片变清晰服务,用于画图标注功能中的图片增强处理。
|
配置图片变清晰API,用于画图标注功能中的图片增强处理。支持主API和备用API切换。
|
||||||
</>
|
</>
|
||||||
}
|
}
|
||||||
expanded={isLocalAiSectionExpanded("enhance")}
|
expanded={isLocalAiSectionExpanded("enhance")}
|
||||||
@ -510,21 +478,17 @@ export function LocalAiImagePipelineSections({
|
|||||||
<LocalAiSectionSummary
|
<LocalAiSectionSummary
|
||||||
items={[
|
items={[
|
||||||
{
|
{
|
||||||
label: enhanceConfig.provider === "vip98k"
|
label: enhanceConfig.useBackupApi
|
||||||
? "VIP98K"
|
|
||||||
: enhanceConfig.useBackupApi
|
|
||||||
? "备用 Real-ESRGAN"
|
? "备用 Real-ESRGAN"
|
||||||
: "主 API",
|
: "主 API",
|
||||||
tone: "neutral",
|
tone: "neutral",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label:
|
label:
|
||||||
enhanceConfig.provider === "vip98k" ||
|
|
||||||
enhanceConfig.apiKey || enhanceConfig.backupApiKey
|
enhanceConfig.apiKey || enhanceConfig.backupApiKey
|
||||||
? "Key 已配置"
|
? "Key 已配置"
|
||||||
: "Key 待配置",
|
: "Key 待配置",
|
||||||
tone:
|
tone:
|
||||||
enhanceConfig.provider === "vip98k" ||
|
|
||||||
enhanceConfig.apiKey || enhanceConfig.backupApiKey
|
enhanceConfig.apiKey || enhanceConfig.backupApiKey
|
||||||
? "enabled"
|
? "enabled"
|
||||||
: "warning",
|
: "warning",
|
||||||
@ -534,13 +498,6 @@ export function LocalAiImagePipelineSections({
|
|||||||
}
|
}
|
||||||
title="变清晰API配置"
|
title="变清晰API配置"
|
||||||
>
|
>
|
||||||
{enhanceConfig.provider === "vip98k" ? (
|
|
||||||
<div className="rounded-lg border bg-muted/40 p-4 text-sm">
|
|
||||||
使用 VIP98K 的 enhance 模型,默认 4 倍增强。API Key 从“AI 模型设置”中的
|
|
||||||
VIP98K 服务商读取。
|
|
||||||
</div>
|
|
||||||
) : (
|
|
||||||
<>
|
|
||||||
{/* API切换开关 */}
|
{/* API切换开关 */}
|
||||||
<div className="flex items-center justify-between rounded-lg border p-4">
|
<div className="flex items-center justify-between rounded-lg border p-4">
|
||||||
<div className="space-y-0.5">
|
<div className="space-y-0.5">
|
||||||
@ -665,7 +622,7 @@ export function LocalAiImagePipelineSections({
|
|||||||
</p>
|
</p>
|
||||||
<p>• 支持的图片格式:JPG, JPEG, PNG, BMP, WEBP, TIFF等</p>
|
<p>• 支持的图片格式:JPG, JPEG, PNG, BMP, WEBP, TIFF等</p>
|
||||||
<p>
|
<p>
|
||||||
• 图片大小:不超过20MB,不限制分辨率(建议按业务需求控制尺寸)
|
• 图片大小:不超过50MB,不限制分辨率(建议按业务需求控制尺寸)
|
||||||
</p>
|
</p>
|
||||||
<p>• 返回的链接有效期为1小时,系统会自动下载并存储到S3</p>
|
<p>• 返回的链接有效期为1小时,系统会自动下载并存储到S3</p>
|
||||||
</div>
|
</div>
|
||||||
@ -784,13 +741,11 @@ export function LocalAiImagePipelineSections({
|
|||||||
<p>• 支持2x、4x、8x放大倍数</p>
|
<p>• 支持2x、4x、8x放大倍数</p>
|
||||||
<p>• 处理时间:通常1-10秒,取决于图片大小</p>
|
<p>• 处理时间:通常1-10秒,取决于图片大小</p>
|
||||||
<p>• 支持格式:JPG, PNG, JPEG等常见图片格式</p>
|
<p>• 支持格式:JPG, PNG, JPEG等常见图片格式</p>
|
||||||
<p>• 文件限制:最大20MB</p>
|
<p>• 文件限制:最大50MB</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
</>
|
|
||||||
)}
|
|
||||||
|
|
||||||
<Button onClick={handleSaveEnhanceConfig}>
|
<Button onClick={handleSaveEnhanceConfig}>
|
||||||
<Save className="mr-2 h-4 w-4" /> 保存配置
|
<Save className="mr-2 h-4 w-4" /> 保存配置
|
||||||
@ -1466,112 +1421,6 @@ export function LocalAiImagePipelineSections({
|
|||||||
</Button>
|
</Button>
|
||||||
</LocalAiSectionShell>
|
</LocalAiSectionShell>
|
||||||
|
|
||||||
<LocalAiSectionShell
|
|
||||||
description={
|
|
||||||
<>
|
|
||||||
配置无限画布图片“局部编辑”和“比例调整”后端优先使用的生图模型。为空时仍沿用旧的 AI详情默认模型或用户当前选择,避免影响已有流程。
|
|
||||||
</>
|
|
||||||
}
|
|
||||||
expanded={isLocalAiSectionExpanded("canvas-local-edit")}
|
|
||||||
id="local-ai-section-canvas-local-edit"
|
|
||||||
onToggle={() => toggleLocalAiSection("canvas-local-edit")}
|
|
||||||
summary={
|
|
||||||
<LocalAiSectionSummary
|
|
||||||
items={[
|
|
||||||
{
|
|
||||||
label: canvasLocalEditConfig.modelId
|
|
||||||
? "普通局部编辑已指定"
|
|
||||||
: "普通局部编辑继承默认",
|
|
||||||
tone: canvasLocalEditConfig.modelId ? "enabled" : "neutral",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
label: canvasLocalEditConfig.scaleAdjustModelId
|
|
||||||
? "比例调整已指定"
|
|
||||||
: "比例调整兼容旧默认",
|
|
||||||
tone: canvasLocalEditConfig.scaleAdjustModelId
|
|
||||||
? "enabled"
|
|
||||||
: "neutral",
|
|
||||||
},
|
|
||||||
]}
|
|
||||||
/>
|
|
||||||
}
|
|
||||||
title="局部编辑模型配置"
|
|
||||||
>
|
|
||||||
<div className="grid gap-4 md:grid-cols-2">
|
|
||||||
<div className="space-y-2">
|
|
||||||
<Label htmlFor="canvasLocalEditModel">普通局部编辑默认模型</Label>
|
|
||||||
<Select
|
|
||||||
onValueChange={(value) =>
|
|
||||||
setCanvasLocalEditConfig((prev) => ({
|
|
||||||
...prev,
|
|
||||||
modelId: value === "__none__" ? "" : value,
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
value={canvasLocalEditConfig.modelId || "__none__"}
|
|
||||||
>
|
|
||||||
<SelectTrigger id="canvasLocalEditModel">
|
|
||||||
<SelectValue placeholder="不指定时使用当前模型或系统默认" />
|
|
||||||
</SelectTrigger>
|
|
||||||
<SelectContent>
|
|
||||||
<SelectItem value="__none__">不指定,沿用当前模型/系统默认</SelectItem>
|
|
||||||
{availableImageModels.length > 0 ? (
|
|
||||||
availableImageModels.map((model) => (
|
|
||||||
<SelectItem key={model.id} value={model.modelId}>
|
|
||||||
{formatAiWashModelOptionLabel(model)}
|
|
||||||
</SelectItem>
|
|
||||||
))
|
|
||||||
) : (
|
|
||||||
<SelectItem disabled value="__empty__">
|
|
||||||
暂无可用生图模型,请先到 AI 模型里启用
|
|
||||||
</SelectItem>
|
|
||||||
)}
|
|
||||||
</SelectContent>
|
|
||||||
</Select>
|
|
||||||
<div className="text-muted-foreground text-xs">
|
|
||||||
适用于手动画框并填写提示词的局部编辑。
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="space-y-2">
|
|
||||||
<Label htmlFor="canvasScaleAdjustModel">比例调整/目标框模型</Label>
|
|
||||||
<Select
|
|
||||||
onValueChange={(value) =>
|
|
||||||
setCanvasLocalEditConfig((prev) => ({
|
|
||||||
...prev,
|
|
||||||
scaleAdjustModelId: value === "__none__" ? "" : value,
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
value={canvasLocalEditConfig.scaleAdjustModelId || "__none__"}
|
|
||||||
>
|
|
||||||
<SelectTrigger id="canvasScaleAdjustModel">
|
|
||||||
<SelectValue placeholder="不指定时兼容旧 AI详情默认模型" />
|
|
||||||
</SelectTrigger>
|
|
||||||
<SelectContent>
|
|
||||||
<SelectItem value="__none__">不指定,兼容旧 AI详情默认模型</SelectItem>
|
|
||||||
{availableImageModels.length > 0 ? (
|
|
||||||
availableImageModels.map((model) => (
|
|
||||||
<SelectItem key={model.id} value={model.modelId}>
|
|
||||||
{formatAiWashModelOptionLabel(model)}
|
|
||||||
</SelectItem>
|
|
||||||
))
|
|
||||||
) : (
|
|
||||||
<SelectItem disabled value="__empty__">
|
|
||||||
暂无可用生图模型,请先到 AI 模型里启用
|
|
||||||
</SelectItem>
|
|
||||||
)}
|
|
||||||
</SelectContent>
|
|
||||||
</Select>
|
|
||||||
<div className="text-muted-foreground text-xs">
|
|
||||||
适用于截图中“局部编辑/比例”按钮,把商品缩放到青色目标框的流程。
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<Button onClick={handleSaveCanvasLocalEditConfig}>
|
|
||||||
<Save className="mr-2 h-4 w-4" /> 保存局部编辑模型配置
|
|
||||||
</Button>
|
|
||||||
</LocalAiSectionShell>
|
|
||||||
|
|
||||||
<LocalAiSectionShell
|
<LocalAiSectionShell
|
||||||
description={
|
description={
|
||||||
<>
|
<>
|
||||||
@ -2366,7 +2215,7 @@ export function LocalAiImagePipelineSections({
|
|||||||
<LocalAiSectionShell
|
<LocalAiSectionShell
|
||||||
description={
|
description={
|
||||||
<>
|
<>
|
||||||
配置图像拓展服务,用于画图标注功能中的图像智能扩展处理
|
配置阿里云Dashscope图像拓展API,用于画图标注功能中的图像智能扩展处理
|
||||||
</>
|
</>
|
||||||
}
|
}
|
||||||
expanded={isLocalAiSectionExpanded("extend")}
|
expanded={isLocalAiSectionExpanded("extend")}
|
||||||
@ -2376,11 +2225,8 @@ export function LocalAiImagePipelineSections({
|
|||||||
<LocalAiSectionSummary
|
<LocalAiSectionSummary
|
||||||
items={[
|
items={[
|
||||||
{
|
{
|
||||||
label:
|
label: extendConfig.apiKey ? "Key 已配置" : "Key 待配置",
|
||||||
extendConfig.provider === "vip98k"
|
tone: extendConfig.apiKey ? "enabled" : "warning",
|
||||||
? "VIP98K 服务商"
|
|
||||||
: "阿里云服务商",
|
|
||||||
tone: "enabled",
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: `${extendConfig.xScale}x / ${extendConfig.yScale}x`,
|
label: `${extendConfig.xScale}x / ${extendConfig.yScale}x`,
|
||||||
@ -2391,18 +2237,22 @@ export function LocalAiImagePipelineSections({
|
|||||||
}
|
}
|
||||||
title="图像拓展API配置"
|
title="图像拓展API配置"
|
||||||
>
|
>
|
||||||
{extendConfig.provider === "vip98k" ? (
|
<div className="space-y-2">
|
||||||
<div className="rounded-lg border bg-muted/40 p-4 text-sm">
|
<Label htmlFor="extendApiKey">API密钥 *</Label>
|
||||||
使用 VIP98K 的 image-expansion 模型。API Key 从“AI 模型设置”中的
|
<Input
|
||||||
VIP98K 服务商读取,仅在这里维护扩展比例。
|
id="extendApiKey"
|
||||||
|
onChange={(e) =>
|
||||||
|
setExtendConfig({ ...extendConfig, apiKey: e.target.value })
|
||||||
|
}
|
||||||
|
placeholder="sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
|
||||||
|
type="password"
|
||||||
|
value={extendConfig.apiKey}
|
||||||
|
/>
|
||||||
|
<div className="text-muted-foreground text-xs">
|
||||||
|
请输入阿里云Dashscope API密钥,例如:
|
||||||
|
sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
||||||
</div>
|
</div>
|
||||||
) : (
|
</div>
|
||||||
<div className="rounded-lg border bg-muted/40 p-4 text-sm">
|
|
||||||
使用阿里云 DashScope 的 image-out-painting 模型。API Key
|
|
||||||
从“AI 模型设置”中已启用的阿里云官方生图服务商读取,
|
|
||||||
仅在这里维护拓展比例。
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
<div className="grid grid-cols-2 gap-4">
|
<div className="grid grid-cols-2 gap-4">
|
||||||
<div className="space-y-2">
|
<div className="space-y-2">
|
||||||
<Label htmlFor="extendXScale">X轴放大倍数 *</Label>
|
<Label htmlFor="extendXScale">X轴放大倍数 *</Label>
|
||||||
@ -2456,7 +2306,7 @@ export function LocalAiImagePipelineSections({
|
|||||||
https://dashscope.aliyuncs.com/api/v1/services/aigc/image2image/out-painting
|
https://dashscope.aliyuncs.com/api/v1/services/aigc/image2image/out-painting
|
||||||
</p>
|
</p>
|
||||||
<p>• 支持的图片格式:JPG, PNG, WebP等常见格式</p>
|
<p>• 支持的图片格式:JPG, PNG, WebP等常见格式</p>
|
||||||
<p>• 最大图片大小:10MB</p>
|
<p>• 最大图片大小:4MB</p>
|
||||||
<p>• 处理时间:约5-15秒/张</p>
|
<p>• 处理时间:约5-15秒/张</p>
|
||||||
<p>• 放大倍数范围:1.0-2.0(推荐1.5倍等比例扩展)</p>
|
<p>• 放大倍数范围:1.0-2.0(推荐1.5倍等比例扩展)</p>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@ -1078,15 +1078,139 @@ export function LocalAiRuntimeSections({
|
|||||||
</Button>
|
</Button>
|
||||||
</LocalAiSectionShell>
|
</LocalAiSectionShell>
|
||||||
|
|
||||||
|
{/* 全站生图单模型并发 */}
|
||||||
<LocalAiSectionShell
|
<LocalAiSectionShell
|
||||||
description="生图任务直接发起,图片先显示,后台转存到存储桶。"
|
description={
|
||||||
|
<>
|
||||||
|
控制全站生图链路中,同一个模型允许同时运行的任务数,以及排队后下一批任务的发起间隔。配置保存到数据库,异步队列和直调生成都会自动读取,无需重启服务。
|
||||||
|
</>
|
||||||
|
}
|
||||||
expanded={isLocalAiSectionExpanded("canvas-image-task-queue")}
|
expanded={isLocalAiSectionExpanded("canvas-image-task-queue")}
|
||||||
id="local-ai-section-canvas-image-task-queue"
|
id="local-ai-section-canvas-image-task-queue"
|
||||||
onToggle={() => toggleLocalAiSection("canvas-image-task-queue")}
|
onToggle={() => toggleLocalAiSection("canvas-image-task-queue")}
|
||||||
summary={<LocalAiSectionSummary items={[{ label: "并发不限", tone: "enabled" }, { label: "即时发起", tone: "enabled" }]} />}
|
summary={
|
||||||
title={<span className="flex items-center gap-2"><ImageIcon className="h-5 w-5" />生图任务执行</span>}
|
<LocalAiSectionSummary
|
||||||
|
items={[
|
||||||
|
{
|
||||||
|
label: `${canvasImageTaskQueueConfig.concurrency} 并发/模型`,
|
||||||
|
tone: "neutral",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: `${(
|
||||||
|
canvasImageTaskQueueConfig.batchDispatchIntervalMs / 1000
|
||||||
|
).toFixed(1)} 秒补位`,
|
||||||
|
tone: "neutral",
|
||||||
|
},
|
||||||
|
{ label: "全站生图生效", tone: "enabled" },
|
||||||
|
]}
|
||||||
|
/>
|
||||||
|
}
|
||||||
|
title={
|
||||||
|
<span className="flex items-center gap-2">
|
||||||
|
<ImageIcon className="h-5 w-5" />
|
||||||
|
全站生图单模型并发与批次间隔
|
||||||
|
</span>
|
||||||
|
}
|
||||||
>
|
>
|
||||||
<p className="text-muted-foreground text-sm">已取消模型及单用户并发限制,任务无需等待其他图片生成完成。任务状态、超时处理和失败重试继续保留。</p>
|
<div className="grid gap-6 lg:grid-cols-[minmax(0,1fr)_minmax(220px,320px)]">
|
||||||
|
<div className="rounded-lg border border-amber-200 bg-amber-50 p-4 text-amber-900 text-sm">
|
||||||
|
<div className="font-medium">生效范围</div>
|
||||||
|
<div className="mt-2 space-y-1 text-amber-800 text-xs leading-5">
|
||||||
|
<p>
|
||||||
|
•
|
||||||
|
影响画布异步生图、智能体直调生图、视频分镜高清预览等全站生图入口。
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
•
|
||||||
|
多用户共享同一套并发限制,但会按模型分别计数,不同模型之间不再互相阻塞。
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
• 当前配置的是单模型并发,不是全站总并发,也不是单用户并发。
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
•
|
||||||
|
批次间隔只控制排队补位的“下一批”发起节奏,不影响首批任务即时启动。
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
• 数值过高可能触发上游模型 429、超时或 504,建议配合模型 Key
|
||||||
|
池继续细分。
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="space-y-4">
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label htmlFor="canvasImageTaskQueueConcurrency">
|
||||||
|
单模型最大同时运行任务数
|
||||||
|
</Label>
|
||||||
|
<Input
|
||||||
|
id="canvasImageTaskQueueConcurrency"
|
||||||
|
max={MAX_CANVAS_IMAGE_TASK_CONCURRENCY}
|
||||||
|
min={MIN_CANVAS_IMAGE_TASK_CONCURRENCY}
|
||||||
|
onChange={(e) =>
|
||||||
|
setCanvasImageTaskQueueConfig(
|
||||||
|
normalizeCanvasImageTaskQueueConfig({
|
||||||
|
...canvasImageTaskQueueConfig,
|
||||||
|
concurrency: e.target.value,
|
||||||
|
})
|
||||||
|
)
|
||||||
|
}
|
||||||
|
type="number"
|
||||||
|
value={canvasImageTaskQueueConfig.concurrency}
|
||||||
|
/>
|
||||||
|
<div className="text-muted-foreground text-xs">
|
||||||
|
可配置范围 {MIN_CANVAS_IMAGE_TASK_CONCURRENCY}-
|
||||||
|
{MAX_CANVAS_IMAGE_TASK_CONCURRENCY},当前为{" "}
|
||||||
|
{canvasImageTaskQueueConfig.concurrency} 并发,默认值为{" "}
|
||||||
|
{DEFAULT_CANVAS_IMAGE_TASK_CONCURRENCY}。
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label htmlFor="canvasImageTaskBatchDispatchIntervalSeconds">
|
||||||
|
下一批排队任务发起间隔(秒)
|
||||||
|
</Label>
|
||||||
|
<Input
|
||||||
|
id="canvasImageTaskBatchDispatchIntervalSeconds"
|
||||||
|
max={MAX_CANVAS_IMAGE_BATCH_DISPATCH_INTERVAL_MS / 1000}
|
||||||
|
min={MIN_CANVAS_IMAGE_BATCH_DISPATCH_INTERVAL_MS / 1000}
|
||||||
|
onChange={(e) =>
|
||||||
|
setCanvasImageTaskQueueConfig(
|
||||||
|
normalizeCanvasImageTaskQueueConfig({
|
||||||
|
...canvasImageTaskQueueConfig,
|
||||||
|
batchDispatchIntervalMs: Math.round(
|
||||||
|
Number.parseFloat(e.target.value || "0") * 1000
|
||||||
|
),
|
||||||
|
})
|
||||||
|
)
|
||||||
|
}
|
||||||
|
step="0.5"
|
||||||
|
type="number"
|
||||||
|
value={String(
|
||||||
|
canvasImageTaskQueueConfig.batchDispatchIntervalMs / 1000
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
<div className="text-muted-foreground text-xs">
|
||||||
|
可配置范围 {MIN_CANVAS_IMAGE_BATCH_DISPATCH_INTERVAL_MS / 1000}-
|
||||||
|
{MAX_CANVAS_IMAGE_BATCH_DISPATCH_INTERVAL_MS / 1000} 秒,当前为{" "}
|
||||||
|
{(
|
||||||
|
canvasImageTaskQueueConfig.batchDispatchIntervalMs / 1000
|
||||||
|
).toFixed(1)}{" "}
|
||||||
|
秒,默认值为{" "}
|
||||||
|
{(
|
||||||
|
DEFAULT_CANVAS_IMAGE_BATCH_DISPATCH_INTERVAL_MS / 1000
|
||||||
|
).toFixed(1)}{" "}
|
||||||
|
秒。
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="flex justify-end">
|
||||||
|
<Button onClick={handleSaveCanvasImageTaskQueueConfig}>
|
||||||
|
<Save className="mr-2 h-4 w-4" /> 保存并发与间隔
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
</LocalAiSectionShell>
|
</LocalAiSectionShell>
|
||||||
|
|
||||||
{/* 批量处理配置 */}
|
{/* 批量处理配置 */}
|
||||||
|
|||||||
@ -12,7 +12,6 @@ export const DEFAULT_LOCAL_AI_EXPANDED_SECTIONS: Record<string, boolean> = {
|
|||||||
rmbg: false,
|
rmbg: false,
|
||||||
enhance: false,
|
enhance: false,
|
||||||
"enhance-pro": false,
|
"enhance-pro": false,
|
||||||
"canvas-local-edit": false,
|
|
||||||
"ai-wash": false,
|
"ai-wash": false,
|
||||||
extend: false,
|
extend: false,
|
||||||
"watermark-remove": false,
|
"watermark-remove": false,
|
||||||
|
|||||||
@ -1,7 +1,6 @@
|
|||||||
import type { LucideIcon } from "lucide-react";
|
import type { LucideIcon } from "lucide-react";
|
||||||
import type { Dispatch, DragEvent, ReactNode, SetStateAction } from "react";
|
import type { Dispatch, DragEvent, ReactNode, SetStateAction } from "react";
|
||||||
import type { CanvasImageTaskQueueConfig } from "@/lib/canvas/image-task-config";
|
import type { CanvasImageTaskQueueConfig } from "@/lib/canvas/image-task-config";
|
||||||
import type { CanvasLocalEditConfig } from "@/lib/canvas/local-edit-config";
|
|
||||||
import type { AiWashConfig } from "@/lib/ai-wash/config";
|
import type { AiWashConfig } from "@/lib/ai-wash/config";
|
||||||
import type {
|
import type {
|
||||||
EnhanceProKreaConfig,
|
EnhanceProKreaConfig,
|
||||||
@ -90,9 +89,6 @@ export type AdminAiProvider = {
|
|||||||
| "snapgen-image"
|
| "snapgen-image"
|
||||||
| "openai-image"
|
| "openai-image"
|
||||||
| "bananaimage"
|
| "bananaimage"
|
||||||
| "visionary"
|
|
||||||
| "atlascloud"
|
|
||||||
| "vip98k"
|
|
||||||
| "stepfun-image"
|
| "stepfun-image"
|
||||||
| "midjourney"
|
| "midjourney"
|
||||||
| "fal-video"
|
| "fal-video"
|
||||||
@ -249,7 +245,7 @@ export type EnhanceProKeyRow = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export type RmbgConfig = {
|
export type RmbgConfig = {
|
||||||
provider: "vip98k" | "rmbg" | "photoroom" | "aoscdn";
|
provider: "rmbg" | "photoroom" | "aoscdn";
|
||||||
apiUrl: string;
|
apiUrl: string;
|
||||||
apiKey: string;
|
apiKey: string;
|
||||||
photoroomApiKey: string;
|
photoroomApiKey: string;
|
||||||
@ -261,7 +257,6 @@ export type RmbgConfig = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export type EnhanceConfig = {
|
export type EnhanceConfig = {
|
||||||
provider: "vip98k" | "legacy";
|
|
||||||
apiUrl: string;
|
apiUrl: string;
|
||||||
apiKey: string;
|
apiKey: string;
|
||||||
type: string;
|
type: string;
|
||||||
@ -274,7 +269,7 @@ export type EnhanceConfig = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export type ExtendConfig = {
|
export type ExtendConfig = {
|
||||||
provider: "vip98k" | "dashscope";
|
apiKey: string;
|
||||||
xScale: string;
|
xScale: string;
|
||||||
yScale: string;
|
yScale: string;
|
||||||
};
|
};
|
||||||
@ -333,8 +328,6 @@ export type LocalAiImagePipelineSectionsProps = LocalAiSectionControlProps & {
|
|||||||
aiWashConfig: AiWashConfig;
|
aiWashConfig: AiWashConfig;
|
||||||
setAiWashConfig: Dispatch<SetStateAction<AiWashConfig>>;
|
setAiWashConfig: Dispatch<SetStateAction<AiWashConfig>>;
|
||||||
aiWashModeIconUploading: boolean;
|
aiWashModeIconUploading: boolean;
|
||||||
canvasLocalEditConfig: CanvasLocalEditConfig;
|
|
||||||
setCanvasLocalEditConfig: Dispatch<SetStateAction<CanvasLocalEditConfig>>;
|
|
||||||
extendConfig: ExtendConfig;
|
extendConfig: ExtendConfig;
|
||||||
setExtendConfig: Dispatch<SetStateAction<ExtendConfig>>;
|
setExtendConfig: Dispatch<SetStateAction<ExtendConfig>>;
|
||||||
watermarkRemoveConfig: WatermarkRemoveConfig;
|
watermarkRemoveConfig: WatermarkRemoveConfig;
|
||||||
@ -377,7 +370,6 @@ export type LocalAiImagePipelineSectionsProps = LocalAiSectionControlProps & {
|
|||||||
handleMoveEnhanceProPreset: (fromIndex: number, toIndex: number) => void;
|
handleMoveEnhanceProPreset: (fromIndex: number, toIndex: number) => void;
|
||||||
handleAiWashModeIconUpload: (file: File) => Promise<void>;
|
handleAiWashModeIconUpload: (file: File) => Promise<void>;
|
||||||
handleSaveAiWashConfig: AsyncAction;
|
handleSaveAiWashConfig: AsyncAction;
|
||||||
handleSaveCanvasLocalEditConfig: AsyncAction;
|
|
||||||
handleSaveExtendConfig: AsyncAction;
|
handleSaveExtendConfig: AsyncAction;
|
||||||
handleSaveWatermarkRemoveConfig: AsyncAction;
|
handleSaveWatermarkRemoveConfig: AsyncAction;
|
||||||
handleSaveAngleEditConfig: AsyncAction;
|
handleSaveAngleEditConfig: AsyncAction;
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@ -1,5 +1,4 @@
|
|||||||
"use client";
|
"use client";
|
||||||
import { filterCanvasModels } from "@/lib/video-canvas/model-matching";
|
|
||||||
|
|
||||||
import Link from "next/link";
|
import Link from "next/link";
|
||||||
import { ArrowLeft, Plus, Trash2 } from "lucide-react";
|
import { ArrowLeft, Plus, Trash2 } from "lucide-react";
|
||||||
@ -25,6 +24,10 @@ import {
|
|||||||
} from "@/components/ui/select";
|
} from "@/components/ui/select";
|
||||||
import { Switch } from "@/components/ui/switch";
|
import { Switch } from "@/components/ui/switch";
|
||||||
import { Textarea } from "@/components/ui/textarea";
|
import { Textarea } from "@/components/ui/textarea";
|
||||||
|
import {
|
||||||
|
isImageGenerationModelDescriptor,
|
||||||
|
isVideoModelDescriptor,
|
||||||
|
} from "@/lib/ai/model-classification";
|
||||||
import { fetcher } from "@/lib/utils";
|
import { fetcher } from "@/lib/utils";
|
||||||
import {
|
import {
|
||||||
getVideoCanvasFunctionBillingCopy,
|
getVideoCanvasFunctionBillingCopy,
|
||||||
@ -84,23 +87,56 @@ function getModelOptionLabel(model: AiModelOption) {
|
|||||||
return `${name}${id}`;
|
return `${name}${id}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isImageModelOption(model: AiModelOption) {
|
||||||
|
return isImageGenerationModelDescriptor({
|
||||||
|
providerType: model.providerType,
|
||||||
|
modelId: model.modelId,
|
||||||
|
capabilities: model.capabilities,
|
||||||
|
supportsImageGen: model.supportsImageGen,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function isVideoModelOption(model: AiModelOption) {
|
||||||
|
return isVideoModelDescriptor({
|
||||||
|
providerType: model.providerType,
|
||||||
|
modelId: model.modelId,
|
||||||
|
capabilities: model.capabilities,
|
||||||
|
supportsImageGen: model.supportsImageGen,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function filterModelsForFunctionKind(
|
||||||
|
models: AiModelOption[],
|
||||||
|
kind: VideoCanvasFunctionDefinition["kind"],
|
||||||
|
) {
|
||||||
|
const enabled = models.filter((model) => model.isEnabled !== false && getModelOptionValue(model));
|
||||||
|
let filtered = enabled;
|
||||||
|
if (kind === "image" || kind === "image-edit") {
|
||||||
|
filtered = enabled.filter(isImageModelOption);
|
||||||
|
} else if (kind === "video") {
|
||||||
|
filtered = enabled.filter(isVideoModelOption);
|
||||||
|
} else if (kind === "multimodal") {
|
||||||
|
filtered = enabled.filter(
|
||||||
|
(model) => model.isMultimodal || model.supportsVision || (!isImageModelOption(model) && !isVideoModelOption(model)),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return filtered.length ? filtered : enabled;
|
||||||
|
}
|
||||||
|
|
||||||
function ModelSelect({
|
function ModelSelect({
|
||||||
allowInherit = false,
|
allowInherit = false,
|
||||||
kind,
|
kind,
|
||||||
functionKey,
|
|
||||||
models,
|
models,
|
||||||
onChange,
|
onChange,
|
||||||
value,
|
value,
|
||||||
}: {
|
}: {
|
||||||
allowInherit?: boolean;
|
allowInherit?: boolean;
|
||||||
kind: VideoCanvasFunctionDefinition["kind"];
|
kind: VideoCanvasFunctionDefinition["kind"];
|
||||||
functionKey?: string;
|
|
||||||
models: AiModelOption[];
|
models: AiModelOption[];
|
||||||
onChange: (value: string) => void;
|
onChange: (value: string) => void;
|
||||||
value: string;
|
value: string;
|
||||||
}) {
|
}) {
|
||||||
const modelKind = functionKey === "audio-to-video" ? "audio-video" : kind === "image" || kind === "image-edit" || kind === "video" || kind === "multimodal" ? kind : "text";
|
const filteredModels = filterModelsForFunctionKind(models, kind);
|
||||||
const filteredModels = filterCanvasModels(models, modelKind);
|
|
||||||
const normalizedValue = value?.trim() || (allowInherit ? INHERIT_MODEL_VALUE : "auto");
|
const normalizedValue = value?.trim() || (allowInherit ? INHERIT_MODEL_VALUE : "auto");
|
||||||
const hasCurrent =
|
const hasCurrent =
|
||||||
normalizedValue === "auto" ||
|
normalizedValue === "auto" ||
|
||||||
@ -815,14 +851,13 @@ export function VideoCanvasSettingsClient() {
|
|||||||
<div className="grid gap-4 md:grid-cols-1">
|
<div className="grid gap-4 md:grid-cols-1">
|
||||||
<div className="space-y-2">
|
<div className="space-y-2">
|
||||||
<Label htmlFor={`model-${item.key}`}>模型覆盖</Label>
|
<Label htmlFor={`model-${item.key}`}>模型覆盖</Label>
|
||||||
{["canvas-generate-image", "canvas-generate-video", "canvas-workflow-analyze", "pick-object"].includes(item.route) ? <ModelSelect
|
<ModelSelect
|
||||||
allowInherit
|
allowInherit
|
||||||
functionKey={item.key}
|
|
||||||
kind={item.kind}
|
kind={item.kind}
|
||||||
models={modelOptions}
|
models={modelOptions}
|
||||||
onChange={(value) => updateFunction(item.key, { model: value })}
|
onChange={(value) => updateFunction(item.key, { model: value })}
|
||||||
value={item.model || ""}
|
value={item.model || ""}
|
||||||
/> : <p className="text-sm text-muted-foreground">{item.route === "local" ? "本地处理,无需生成模型" : "使用此功能的专用服务接口"}</p>}
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div className="rounded-lg border border-dashed border-border/70 bg-muted/20 px-3 py-2">
|
<div className="rounded-lg border border-dashed border-border/70 bg-muted/20 px-3 py-2">
|
||||||
<div className="text-xs font-medium text-foreground">
|
<div className="text-xs font-medium text-foreground">
|
||||||
|
|||||||
@ -4,8 +4,7 @@ import type { Metadata } from "next";
|
|||||||
import { redirect } from "next/navigation";
|
import { redirect } from "next/navigation";
|
||||||
import { auth } from "@/app/(auth)/auth";
|
import { auth } from "@/app/(auth)/auth";
|
||||||
|
|
||||||
const ZOO_MODELING_APP_SRC =
|
const ZOO_MODELING_APP_SRC = "/vendor/zoo-modeling-app/index.html";
|
||||||
"/vendor/zoo-modeling-app/index.html?v=f2b345db-eng1";
|
|
||||||
|
|
||||||
export const dynamic = "force-dynamic";
|
export const dynamic = "force-dynamic";
|
||||||
export const metadata: Metadata = {
|
export const metadata: Metadata = {
|
||||||
|
|||||||
@ -2,31 +2,9 @@ import { createLogger } from "@/lib/observability/logger";
|
|||||||
import { generateText } from "ai";
|
import { generateText } from "ai";
|
||||||
import { fetchImageBufferFromUrl } from "@/lib/ai/rmbg/service";
|
import { fetchImageBufferFromUrl } from "@/lib/ai/rmbg/service";
|
||||||
import { getLanguageModelById } from "@/lib/ai/dynamic-provider";
|
import { getLanguageModelById } from "@/lib/ai/dynamic-provider";
|
||||||
import {
|
|
||||||
DETAIL_REPLACEMENT_IMAGE_MODEL_CONFIG_KEY,
|
|
||||||
normalizeDetailReplacementImageModelConfig,
|
|
||||||
} from "@/lib/ai/detail-replacement-image-model-config";
|
|
||||||
import {
|
|
||||||
compositeReplacementWithReferenceLocks,
|
|
||||||
parseReplacementLayoutLockRegions,
|
|
||||||
type NormalizedReplacementBox,
|
|
||||||
} from "@/lib/ai/replacement-layout-lock";
|
|
||||||
import {
|
|
||||||
buildReplacementProductIdentityAnalysisPrompt,
|
|
||||||
buildReplacementProductStructureLockLines,
|
|
||||||
formatReplacementProductIdentityProfile,
|
|
||||||
normalizeReplacementProductImageIndexes,
|
|
||||||
parseReplacementProductIdentityProfile,
|
|
||||||
type ReplacementProductIdentityProfile,
|
|
||||||
} from "@/lib/ai/replacement-product-identity";
|
|
||||||
import { resolveDetailAgentLocalMoonshotModel } from "@/lib/ai/detail-agent-model-runtime";
|
import { resolveDetailAgentLocalMoonshotModel } from "@/lib/ai/detail-agent-model-runtime";
|
||||||
import { resolveDetailReplacementVisionRuntime } from "@/lib/ai/detail-replacement-vision-runtime";
|
|
||||||
import { inlineVisionImages } from "@/lib/ai/vision-inline";
|
import { inlineVisionImages } from "@/lib/ai/vision-inline";
|
||||||
import {
|
import { getEnabledAiModelsWithProvider, getSiteSettings } from "@/lib/db/queries";
|
||||||
getEnabledAiModelsWithProvider,
|
|
||||||
getSiteSettings,
|
|
||||||
getSystemSetting,
|
|
||||||
} from "@/lib/db/queries";
|
|
||||||
import { uploadFile } from "@/lib/storage/service";
|
import { uploadFile } from "@/lib/storage/service";
|
||||||
import { bufferToArrayBuffer } from "./persistence";
|
import { bufferToArrayBuffer } from "./persistence";
|
||||||
import { isVideoProviderType } from "./video-models";
|
import { isVideoProviderType } from "./video-models";
|
||||||
@ -256,8 +234,6 @@ export async function normalizeReplacementResultImage(params: {
|
|||||||
referenceImageUrl: string;
|
referenceImageUrl: string;
|
||||||
referenceGeometry?: ReplacementReferenceGeometry | null;
|
referenceGeometry?: ReplacementReferenceGeometry | null;
|
||||||
scenePlan?: ReplacementScenePlan | null;
|
scenePlan?: ReplacementScenePlan | null;
|
||||||
preserveReferenceOutsideSubject?: boolean;
|
|
||||||
preserveReferenceText?: boolean;
|
|
||||||
uploadPrefix: string;
|
uploadPrefix: string;
|
||||||
}): Promise<{
|
}): Promise<{
|
||||||
originalUrl: string;
|
originalUrl: string;
|
||||||
@ -266,11 +242,6 @@ export async function normalizeReplacementResultImage(params: {
|
|||||||
}> {
|
}> {
|
||||||
const sharp = (await import("sharp")).default;
|
const sharp = (await import("sharp")).default;
|
||||||
const generatedBuffer = await fetchImageBufferFromUrl(params.generatedImageUrl);
|
const generatedBuffer = await fetchImageBufferFromUrl(params.generatedImageUrl);
|
||||||
let referenceBuffer: Buffer | null = null;
|
|
||||||
const getReferenceBuffer = async () => {
|
|
||||||
referenceBuffer ||= await fetchImageBufferFromUrl(params.referenceImageUrl);
|
|
||||||
return referenceBuffer;
|
|
||||||
};
|
|
||||||
let targetWidth = params.referenceGeometry?.width;
|
let targetWidth = params.referenceGeometry?.width;
|
||||||
let targetHeight = params.referenceGeometry?.height;
|
let targetHeight = params.referenceGeometry?.height;
|
||||||
const generatedMetadata = await sharp(generatedBuffer, { failOn: "none" })
|
const generatedMetadata = await sharp(generatedBuffer, { failOn: "none" })
|
||||||
@ -279,8 +250,8 @@ export async function normalizeReplacementResultImage(params: {
|
|||||||
|
|
||||||
if (!targetWidth || !targetHeight) {
|
if (!targetWidth || !targetHeight) {
|
||||||
try {
|
try {
|
||||||
const resolvedReferenceBuffer = await getReferenceBuffer();
|
const referenceBuffer = await fetchImageBufferFromUrl(params.referenceImageUrl);
|
||||||
const referenceMetadata = await sharp(resolvedReferenceBuffer, { failOn: "none" })
|
const referenceMetadata = await sharp(referenceBuffer, { failOn: "none" })
|
||||||
.rotate()
|
.rotate()
|
||||||
.metadata();
|
.metadata();
|
||||||
targetWidth = referenceMetadata.width;
|
targetWidth = referenceMetadata.width;
|
||||||
@ -327,7 +298,7 @@ export async function normalizeReplacementResultImage(params: {
|
|||||||
: shouldPreserveFullGeneratedCanvas
|
: shouldPreserveFullGeneratedCanvas
|
||||||
? "contain"
|
? "contain"
|
||||||
: "cover";
|
: "cover";
|
||||||
let normalizedBuffer =
|
const normalizedBuffer =
|
||||||
targetWidth && targetHeight
|
targetWidth && targetHeight
|
||||||
? await imagePipeline
|
? await imagePipeline
|
||||||
.resize(targetWidth, targetHeight, {
|
.resize(targetWidth, targetHeight, {
|
||||||
@ -341,82 +312,6 @@ export async function normalizeReplacementResultImage(params: {
|
|||||||
.png()
|
.png()
|
||||||
.toBuffer()
|
.toBuffer()
|
||||||
: await imagePipeline.png().toBuffer();
|
: await imagePipeline.png().toBuffer();
|
||||||
|
|
||||||
if (
|
|
||||||
targetWidth &&
|
|
||||||
targetHeight &&
|
|
||||||
(params.preserveReferenceOutsideSubject || params.preserveReferenceText)
|
|
||||||
) {
|
|
||||||
let layoutLocks = {
|
|
||||||
subjectBoundingBox: params.scenePlan?.subjectBoundingBox || null,
|
|
||||||
textBoundingBoxes: params.scenePlan?.textBoundingBoxes || [],
|
|
||||||
analyzed: params.scenePlan?.layoutLockAnalyzed === true,
|
|
||||||
};
|
|
||||||
const needsLayoutLockFallback =
|
|
||||||
!layoutLocks.analyzed ||
|
|
||||||
(params.preserveReferenceOutsideSubject && !layoutLocks.subjectBoundingBox);
|
|
||||||
|
|
||||||
if (needsLayoutLockFallback) {
|
|
||||||
const fallbackLocks = await resolveReplacementLayoutLockRegions({
|
|
||||||
referenceImageUrl: params.referenceImageUrl,
|
|
||||||
referenceBuffer: await getReferenceBuffer(),
|
|
||||||
});
|
|
||||||
layoutLocks = {
|
|
||||||
subjectBoundingBox:
|
|
||||||
layoutLocks.subjectBoundingBox || fallbackLocks.subjectBoundingBox,
|
|
||||||
textBoundingBoxes:
|
|
||||||
layoutLocks.textBoundingBoxes.length > 0
|
|
||||||
? layoutLocks.textBoundingBoxes
|
|
||||||
: fallbackLocks.textBoundingBoxes,
|
|
||||||
analyzed: layoutLocks.analyzed || fallbackLocks.analyzed,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
const shouldUseSafeReferenceFallback = Boolean(
|
|
||||||
(params.preserveReferenceOutsideSubject && !layoutLocks.subjectBoundingBox) ||
|
|
||||||
(params.preserveReferenceText && !layoutLocks.analyzed),
|
|
||||||
);
|
|
||||||
normalizedBuffer = shouldUseSafeReferenceFallback
|
|
||||||
? await sharp(await getReferenceBuffer(), { failOn: "none" })
|
|
||||||
.rotate()
|
|
||||||
.resize(targetWidth, targetHeight, { fit: "fill" })
|
|
||||||
.png()
|
|
||||||
.toBuffer()
|
|
||||||
: await compositeReplacementWithReferenceLocks({
|
|
||||||
generatedBuffer: normalizedBuffer,
|
|
||||||
referenceBuffer: await getReferenceBuffer(),
|
|
||||||
width: targetWidth,
|
|
||||||
height: targetHeight,
|
|
||||||
preserveReferenceOutsideSubject: Boolean(
|
|
||||||
params.preserveReferenceOutsideSubject && layoutLocks.subjectBoundingBox,
|
|
||||||
),
|
|
||||||
subjectBoundingBox: layoutLocks.subjectBoundingBox,
|
|
||||||
textBoundingBoxes: params.preserveReferenceText
|
|
||||||
? layoutLocks.textBoundingBoxes
|
|
||||||
: [],
|
|
||||||
});
|
|
||||||
if (shouldUseSafeReferenceFallback) {
|
|
||||||
chatApiLogger.warn(
|
|
||||||
"[Chat API] Replacement layout lock analysis unavailable; preserved the reference image instead of returning a layout-damaged result",
|
|
||||||
{
|
|
||||||
preserveReferenceOutsideSubject: params.preserveReferenceOutsideSubject,
|
|
||||||
preserveReferenceText: params.preserveReferenceText,
|
|
||||||
hasSubjectBoundingBox: Boolean(layoutLocks.subjectBoundingBox),
|
|
||||||
layoutLockAnalyzed: layoutLocks.analyzed,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
chatApiLogger.info("[Chat API] Replacement reference layout locks applied:", {
|
|
||||||
preserveReferenceOutsideSubject: Boolean(
|
|
||||||
params.preserveReferenceOutsideSubject && layoutLocks.subjectBoundingBox,
|
|
||||||
),
|
|
||||||
preservedTextRegionCount: params.preserveReferenceText
|
|
||||||
? layoutLocks.textBoundingBoxes.length
|
|
||||||
: 0,
|
|
||||||
usedFallbackAnalysis: needsLayoutLockFallback,
|
|
||||||
usedSafeReferenceFallback: shouldUseSafeReferenceFallback,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
const thumbnailBuffer = await createAgentImageThumbnailBuffer(normalizedBuffer);
|
const thumbnailBuffer = await createAgentImageThumbnailBuffer(normalizedBuffer);
|
||||||
|
|
||||||
const uniqueSuffix = `${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
|
const uniqueSuffix = `${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
|
||||||
@ -504,12 +399,6 @@ export type ReplacementScenePlan = {
|
|||||||
generationPrompt: string;
|
generationPrompt: string;
|
||||||
selectedProductImageIndexes: number[];
|
selectedProductImageIndexes: number[];
|
||||||
productSelectionReason: string;
|
productSelectionReason: string;
|
||||||
productIdentitySummary?: string;
|
|
||||||
productStructureSummary?: string;
|
|
||||||
productStructuralRisks?: string;
|
|
||||||
subjectBoundingBox?: NormalizedReplacementBox | null;
|
|
||||||
textBoundingBoxes?: NormalizedReplacementBox[];
|
|
||||||
layoutLockAnalyzed?: boolean;
|
|
||||||
analysisModelName?: string | null;
|
analysisModelName?: string | null;
|
||||||
analysisSource: "local_moonshot" | "configured_vision" | "heuristic";
|
analysisSource: "local_moonshot" | "configured_vision" | "heuristic";
|
||||||
};
|
};
|
||||||
@ -550,188 +439,6 @@ export type ReplacementScenePlanningRuntime =
|
|||||||
|
|
||||||
const QWEN_VL_MODEL_MATCHER = /(qwen|千问|通义).*(vl|vision)|qwen3-vl|qwen-vl/i;
|
const QWEN_VL_MODEL_MATCHER = /(qwen|千问|通义).*(vl|vision)|qwen3-vl|qwen-vl/i;
|
||||||
|
|
||||||
const replacementProductIdentityProfileCache = new Map<
|
|
||||||
string,
|
|
||||||
{
|
|
||||||
expiresAt: number;
|
|
||||||
promise: Promise<ReplacementProductIdentityProfile | null>;
|
|
||||||
}
|
|
||||||
>();
|
|
||||||
|
|
||||||
async function resolveReplacementProductIdentityProfile(params: {
|
|
||||||
productImageUrls: string[];
|
|
||||||
planningRuntime: Exclude<ReplacementScenePlanningRuntime, null>;
|
|
||||||
}) {
|
|
||||||
const productImageUrls = Array.from(
|
|
||||||
new Set(
|
|
||||||
params.productImageUrls
|
|
||||||
.map((url) => String(url || "").trim())
|
|
||||||
.filter((url) => /^https?:\/\//i.test(url)),
|
|
||||||
),
|
|
||||||
).slice(0, 8);
|
|
||||||
if (productImageUrls.length === 0) return null;
|
|
||||||
|
|
||||||
const cacheKey = [
|
|
||||||
params.planningRuntime.kind,
|
|
||||||
params.planningRuntime.modelName,
|
|
||||||
...productImageUrls,
|
|
||||||
].join("|");
|
|
||||||
const now = Date.now();
|
|
||||||
const cached = replacementProductIdentityProfileCache.get(cacheKey);
|
|
||||||
if (cached && cached.expiresAt > now) {
|
|
||||||
return cached.promise;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (replacementProductIdentityProfileCache.size >= 100) {
|
|
||||||
const oldestKey = replacementProductIdentityProfileCache.keys().next().value;
|
|
||||||
if (oldestKey) replacementProductIdentityProfileCache.delete(oldestKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
const promise = (async () => {
|
|
||||||
try {
|
|
||||||
const { inlineDataUrls } = await inlineVisionImages(productImageUrls, {
|
|
||||||
maxImages: productImageUrls.length,
|
|
||||||
maxSide: 800,
|
|
||||||
timeoutMs: 12_000,
|
|
||||||
retries: 2,
|
|
||||||
loggerPrefix: "[Chat API] Replacement product identity",
|
|
||||||
trimTransparentPadding: true,
|
|
||||||
});
|
|
||||||
const effectiveImages =
|
|
||||||
inlineDataUrls.length === productImageUrls.length
|
|
||||||
? inlineDataUrls
|
|
||||||
: productImageUrls;
|
|
||||||
const { text } = await generateText({
|
|
||||||
model: params.planningRuntime.model,
|
|
||||||
messages: [
|
|
||||||
{
|
|
||||||
role: "user",
|
|
||||||
content: [
|
|
||||||
{
|
|
||||||
type: "text",
|
|
||||||
text: buildReplacementProductIdentityAnalysisPrompt(
|
|
||||||
productImageUrls.length,
|
|
||||||
),
|
|
||||||
},
|
|
||||||
...effectiveImages.map((image) => ({
|
|
||||||
type: "image" as const,
|
|
||||||
image,
|
|
||||||
})),
|
|
||||||
],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
system:
|
|
||||||
"你是高精度商品结构分析器。多张图片属于同一商品;请跨视角还原唯一结构身份,并明确每个视角最清楚的构件。",
|
|
||||||
temperature: params.planningRuntime.kind === "local_moonshot" ? 1 : 0.1,
|
|
||||||
maxTokens: 1800,
|
|
||||||
abortSignal: AbortSignal.timeout(
|
|
||||||
params.planningRuntime.kind === "local_moonshot" ? 90_000 : 60_000,
|
|
||||||
),
|
|
||||||
} as any);
|
|
||||||
const profile = parseReplacementProductIdentityProfile(
|
|
||||||
text,
|
|
||||||
productImageUrls.length,
|
|
||||||
);
|
|
||||||
if (!profile) {
|
|
||||||
throw new Error("商品结构画像缺少必需字段");
|
|
||||||
}
|
|
||||||
return profile;
|
|
||||||
} catch (error) {
|
|
||||||
chatApiLogger.warn("[Chat API] Replacement product identity analysis failed:", {
|
|
||||||
productImageCount: productImageUrls.length,
|
|
||||||
modelName: params.planningRuntime.modelName,
|
|
||||||
error: error instanceof Error ? error.message : "unknown",
|
|
||||||
});
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
})();
|
|
||||||
|
|
||||||
replacementProductIdentityProfileCache.set(cacheKey, {
|
|
||||||
expiresAt: now + 30 * 60 * 1000,
|
|
||||||
promise,
|
|
||||||
});
|
|
||||||
return promise;
|
|
||||||
}
|
|
||||||
|
|
||||||
const replacementLayoutLockCache = new Map<
|
|
||||||
string,
|
|
||||||
{
|
|
||||||
expiresAt: number;
|
|
||||||
promise: Promise<ReturnType<typeof parseReplacementLayoutLockRegions>>;
|
|
||||||
}
|
|
||||||
>();
|
|
||||||
|
|
||||||
async function resolveReplacementLayoutLockRegions(params: {
|
|
||||||
referenceImageUrl: string;
|
|
||||||
referenceBuffer: Buffer;
|
|
||||||
}) {
|
|
||||||
const now = Date.now();
|
|
||||||
const cached = replacementLayoutLockCache.get(params.referenceImageUrl);
|
|
||||||
if (cached && cached.expiresAt > now) {
|
|
||||||
return cached.promise;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (replacementLayoutLockCache.size >= 200) {
|
|
||||||
const oldestKey = replacementLayoutLockCache.keys().next().value;
|
|
||||||
if (oldestKey) {
|
|
||||||
replacementLayoutLockCache.delete(oldestKey);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const promise = (async () => {
|
|
||||||
try {
|
|
||||||
const sharp = (await import("sharp")).default;
|
|
||||||
const inlineBuffer = await sharp(params.referenceBuffer, { failOn: "none" })
|
|
||||||
.rotate()
|
|
||||||
.resize(1600, 1600, {
|
|
||||||
fit: "inside",
|
|
||||||
withoutEnlargement: true,
|
|
||||||
})
|
|
||||||
.flatten({ background: "#ffffff" })
|
|
||||||
.jpeg({ quality: 88 })
|
|
||||||
.toBuffer();
|
|
||||||
const { callQwen3Vl } = await import("@/lib/ai/qwen3-vl");
|
|
||||||
const result = await callQwen3Vl({
|
|
||||||
imageUrl: `data:image/jpeg;base64,${inlineBuffer.toString("base64")}`,
|
|
||||||
prompt: [
|
|
||||||
"你是电商详情图的布局区域定位器。",
|
|
||||||
"只分析当前图像,返回严格 JSON,不要解释。",
|
|
||||||
"所有坐标使用 0到1000 的归一化坐标,格式必须是 [x1,y1,x2,y2],绝对不是 x/y/宽/高。",
|
|
||||||
"subjectBoundingBox:只框出图中当前旧商品主体的可视外轮廓区域,包含完整商品和它的自然阴影,不要包含文字;没有商品则为 null。",
|
|
||||||
"textBoundingBoxes:框出所有可见文案块,包括中英文标题、卖点、参数、标签、数字、品牌字样、按钮文字和紧邻的图标;没有则返回空数组。",
|
|
||||||
'{"subjectBoundingBox":[0,0,1000,1000],"textBoundingBoxes":[[0,0,1000,1000]]}',
|
|
||||||
].join("\n"),
|
|
||||||
temperature: 0,
|
|
||||||
maxTokens: 1600,
|
|
||||||
timeoutMs: 60_000,
|
|
||||||
retries: 1,
|
|
||||||
});
|
|
||||||
const parsedLocks = parseReplacementLayoutLockRegions(result.text);
|
|
||||||
if (!parsedLocks.analyzed) {
|
|
||||||
throw new Error("布局区域定位结果缺少必需字段");
|
|
||||||
}
|
|
||||||
return parsedLocks;
|
|
||||||
} catch (error) {
|
|
||||||
replacementLayoutLockCache.delete(params.referenceImageUrl);
|
|
||||||
chatApiLogger.warn("[Chat API] Replacement layout lock fallback analysis failed:", {
|
|
||||||
referenceImageUrl: params.referenceImageUrl,
|
|
||||||
error: error instanceof Error ? error.message : "unknown",
|
|
||||||
});
|
|
||||||
return {
|
|
||||||
subjectBoundingBox: null,
|
|
||||||
textBoundingBoxes: [],
|
|
||||||
analyzed: false,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
})();
|
|
||||||
|
|
||||||
replacementLayoutLockCache.set(params.referenceImageUrl, {
|
|
||||||
expiresAt: now + 30 * 60 * 1000,
|
|
||||||
promise,
|
|
||||||
});
|
|
||||||
return promise;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function normalizeDetailReplacementStrategy(
|
export function normalizeDetailReplacementStrategy(
|
||||||
value: unknown,
|
value: unknown,
|
||||||
): AgentDetailReplacementStrategy {
|
): AgentDetailReplacementStrategy {
|
||||||
@ -1734,27 +1441,7 @@ export function parseReplacementScenePlan(
|
|||||||
visualProfile: params.visualProfile,
|
visualProfile: params.visualProfile,
|
||||||
});
|
});
|
||||||
|
|
||||||
const defaultProductImageSelection = buildDefaultProductImageSelection(
|
const productImageSelection = buildDefaultProductImageSelection(params.productImageCount);
|
||||||
params.productImageCount,
|
|
||||||
);
|
|
||||||
const selectedProductImageIndexes = normalizeReplacementProductImageIndexes(
|
|
||||||
parsed.selectedProductImageIndexes ??
|
|
||||||
parsed.selectedProductIndexes ??
|
|
||||||
parsed.productImageIndexes,
|
|
||||||
params.productImageCount,
|
|
||||||
);
|
|
||||||
const productImageSelection =
|
|
||||||
selectedProductImageIndexes.length > 0
|
|
||||||
? {
|
|
||||||
selectedProductImageIndexes,
|
|
||||||
productSelectionReason:
|
|
||||||
truncateReplacementAnalysisText(
|
|
||||||
parsed.productSelectionReason ?? parsed.selectionReason,
|
|
||||||
240,
|
|
||||||
) || "按当前详情底板机位选择最匹配的产品视角。",
|
|
||||||
}
|
|
||||||
: defaultProductImageSelection;
|
|
||||||
const layoutLocks = parseReplacementLayoutLockRegions(parsed);
|
|
||||||
let scenePlan: ReplacementScenePlan = {
|
let scenePlan: ReplacementScenePlan = {
|
||||||
pageKind,
|
pageKind,
|
||||||
generationRoute,
|
generationRoute,
|
||||||
@ -1799,9 +1486,6 @@ export function parseReplacementScenePlan(
|
|||||||
generationPrompt,
|
generationPrompt,
|
||||||
selectedProductImageIndexes: productImageSelection.selectedProductImageIndexes,
|
selectedProductImageIndexes: productImageSelection.selectedProductImageIndexes,
|
||||||
productSelectionReason: productImageSelection.productSelectionReason,
|
productSelectionReason: productImageSelection.productSelectionReason,
|
||||||
subjectBoundingBox: layoutLocks.subjectBoundingBox,
|
|
||||||
textBoundingBoxes: layoutLocks.textBoundingBoxes,
|
|
||||||
layoutLockAnalyzed: layoutLocks.analyzed,
|
|
||||||
analysisModelName: null,
|
analysisModelName: null,
|
||||||
analysisSource: "heuristic",
|
analysisSource: "heuristic",
|
||||||
};
|
};
|
||||||
@ -2160,27 +1844,6 @@ export async function resolveDirectReplacementModelRef(params: {
|
|||||||
finalModelId?: string | null;
|
finalModelId?: string | null;
|
||||||
models?: Awaited<ReturnType<typeof getEnabledAiModelsWithProvider>>;
|
models?: Awaited<ReturnType<typeof getEnabledAiModelsWithProvider>>;
|
||||||
}) {
|
}) {
|
||||||
const models = params.models || (await getEnabledAiModelsWithProvider(false));
|
|
||||||
const imageModels = models.filter(
|
|
||||||
({ model, provider }) =>
|
|
||||||
model.supportsImageGen &&
|
|
||||||
!model.isSubModel &&
|
|
||||||
!isVideoProviderType(provider?.type, model),
|
|
||||||
);
|
|
||||||
const configuredModelSetting = await getSystemSetting({
|
|
||||||
key: DETAIL_REPLACEMENT_IMAGE_MODEL_CONFIG_KEY,
|
|
||||||
}).catch(() => null);
|
|
||||||
const configuredModelId = normalizeDetailReplacementImageModelConfig(
|
|
||||||
configuredModelSetting?.value,
|
|
||||||
).modelId;
|
|
||||||
const configuredModel = imageModels.find(
|
|
||||||
({ model }) =>
|
|
||||||
model.id === configuredModelId || model.modelId === configuredModelId,
|
|
||||||
);
|
|
||||||
if (configuredModel) {
|
|
||||||
return configuredModel.model.id;
|
|
||||||
}
|
|
||||||
|
|
||||||
const explicitModel = String(params.selectedChatModel || "").trim();
|
const explicitModel = String(params.selectedChatModel || "").trim();
|
||||||
if (explicitModel && explicitModel !== "auto") {
|
if (explicitModel && explicitModel !== "auto") {
|
||||||
return explicitModel;
|
return explicitModel;
|
||||||
@ -2190,6 +1853,13 @@ export async function resolveDirectReplacementModelRef(params: {
|
|||||||
return routedModel;
|
return routedModel;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const models = params.models || (await getEnabledAiModelsWithProvider(false));
|
||||||
|
const imageModels = models.filter(
|
||||||
|
({ model, provider }) =>
|
||||||
|
model.supportsImageGen &&
|
||||||
|
!model.isSubModel &&
|
||||||
|
!isVideoProviderType(provider?.type, model),
|
||||||
|
);
|
||||||
const preferred =
|
const preferred =
|
||||||
imageModels.find(({ model }) => model.isDefault) ||
|
imageModels.find(({ model }) => model.isDefault) ||
|
||||||
imageModels.find(({ provider }) => String(provider?.type || "").includes("image")) ||
|
imageModels.find(({ provider }) => String(provider?.type || "").includes("image")) ||
|
||||||
@ -2451,16 +2121,6 @@ export async function analyzeReplacementReferenceImage(params: {
|
|||||||
export async function resolveReplacementScenePlanningRuntime(params: {
|
export async function resolveReplacementScenePlanningRuntime(params: {
|
||||||
models?: Awaited<ReturnType<typeof getEnabledAiModelsWithProvider>>;
|
models?: Awaited<ReturnType<typeof getEnabledAiModelsWithProvider>>;
|
||||||
}): Promise<ReplacementScenePlanningRuntime> {
|
}): Promise<ReplacementScenePlanningRuntime> {
|
||||||
try {
|
|
||||||
const dedicatedVisionRuntime = await resolveDetailReplacementVisionRuntime();
|
|
||||||
if (dedicatedVisionRuntime) return dedicatedVisionRuntime;
|
|
||||||
} catch (error) {
|
|
||||||
chatApiLogger.warn(
|
|
||||||
"[Chat API] Failed to resolve dedicated detail replacement vision model:",
|
|
||||||
error,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const siteSettings = await getSiteSettings().catch(() => null);
|
const siteSettings = await getSiteSettings().catch(() => null);
|
||||||
const localDetailAgentResolved = await resolveDetailAgentLocalMoonshotModel({
|
const localDetailAgentResolved = await resolveDetailAgentLocalMoonshotModel({
|
||||||
@ -2531,13 +2191,6 @@ export async function analyzeReplacementExecutionPlan(params: {
|
|||||||
const referenceVisualProfile = isDetailReplacement
|
const referenceVisualProfile = isDetailReplacement
|
||||||
? await resolveReplacementReferenceVisualProfile(params.referenceImageUrl)
|
? await resolveReplacementReferenceVisualProfile(params.referenceImageUrl)
|
||||||
: null;
|
: null;
|
||||||
const productIdentityProfile =
|
|
||||||
params.planningRuntime && params.productImageUrls.length > 0
|
|
||||||
? await resolveReplacementProductIdentityProfile({
|
|
||||||
productImageUrls: params.productImageUrls,
|
|
||||||
planningRuntime: params.planningRuntime,
|
|
||||||
})
|
|
||||||
: null;
|
|
||||||
if (params.planningRuntime) {
|
if (params.planningRuntime) {
|
||||||
try {
|
try {
|
||||||
const replacementScenePlanningTimeoutMs =
|
const replacementScenePlanningTimeoutMs =
|
||||||
@ -2568,10 +2221,7 @@ export async function analyzeReplacementExecutionPlan(params: {
|
|||||||
? "你是电商详情参考图背景替换流程中的“版式与背景区域规划器”。"
|
? "你是电商详情参考图背景替换流程中的“版式与背景区域规划器”。"
|
||||||
: "你是电商详情替换流程中的“场景理解规划器”。",
|
: "你是电商详情替换流程中的“场景理解规划器”。",
|
||||||
`当前要分析的参考图:${params.referenceLabel}`,
|
`当前要分析的参考图:${params.referenceLabel}`,
|
||||||
"当前视觉输入只包含这一张详情参考图;产品图已经单独做过跨视角结构分析,请根据下面的产品结构画像选择最匹配当前机位的产品视角,不要臆测产品结构。",
|
"你现在只分析这一张参考图本身,不分析产品图,不分析模特图。",
|
||||||
productIdentityProfile
|
|
||||||
? `【产品结构画像】\n${formatReplacementProductIdentityProfile(productIdentityProfile)}`
|
|
||||||
: "产品结构画像暂不可用:仍需按当前参考图机位选择产品图,但不得把旧产品的腿型、支架或构件拓扑当成新产品结构。",
|
|
||||||
referenceVisualProfile
|
referenceVisualProfile
|
||||||
? `像素初筛:nearWhite=${referenceVisualProfile.nearWhitePixelRatio.toFixed(2)}, lightLowSaturation=${referenceVisualProfile.lightLowSaturationPixelRatio.toFixed(2)}, topLight=${referenceVisualProfile.topLightLowSaturationPixelRatio.toFixed(2)}, borderLight=${referenceVisualProfile.borderLightLowSaturationPixelRatio.toFixed(2)}, dark=${referenceVisualProfile.darkPixelRatio.toFixed(2)}, likelyWhiteLayout=${referenceVisualProfile.likelyWhiteLayout ? "true" : "false"}, likelyWhiteScene=${referenceVisualProfile.likelyWhiteScene ? "true" : "false"}。${referenceVisualProfile.reason}`
|
? `像素初筛:nearWhite=${referenceVisualProfile.nearWhitePixelRatio.toFixed(2)}, lightLowSaturation=${referenceVisualProfile.lightLowSaturationPixelRatio.toFixed(2)}, topLight=${referenceVisualProfile.topLightLowSaturationPixelRatio.toFixed(2)}, borderLight=${referenceVisualProfile.borderLightLowSaturationPixelRatio.toFixed(2)}, dark=${referenceVisualProfile.darkPixelRatio.toFixed(2)}, likelyWhiteLayout=${referenceVisualProfile.likelyWhiteLayout ? "true" : "false"}, likelyWhiteScene=${referenceVisualProfile.likelyWhiteScene ? "true" : "false"}。${referenceVisualProfile.reason}`
|
||||||
: "",
|
: "",
|
||||||
@ -2581,7 +2231,7 @@ export async function analyzeReplacementExecutionPlan(params: {
|
|||||||
!referenceVisualProfile?.likelyWhiteLayout && referenceVisualProfile?.likelyWhiteScene
|
!referenceVisualProfile?.likelyWhiteLayout && referenceVisualProfile?.likelyWhiteScene
|
||||||
? "像素初筛已经命中白/浅色低饱和真实场景:这不是白底说明页。若画面存在真实空间、人物或产品照片,promptPackPolicy 应为 soft 或 allow,必须使用前置工作流词包重建图片框内产品、空间、软装和灯光,使差异清晰可见;但最终仍保持白/浅色低饱和基调,不要改造成深色厨房、黑墙、复杂吊灯或强软装。"
|
? "像素初筛已经命中白/浅色低饱和真实场景:这不是白底说明页。若画面存在真实空间、人物或产品照片,promptPackPolicy 应为 soft 或 allow,必须使用前置工作流词包重建图片框内产品、空间、软装和灯光,使差异清晰可见;但最终仍保持白/浅色低饱和基调,不要改造成深色厨房、黑墙、复杂吊灯或强软装。"
|
||||||
: "",
|
: "",
|
||||||
"产品图和模特图会在后续生图阶段单独提供给生图模型。你必须依据产品结构画像输出 selectedProductImageIndexes,选择最多两张最能匹配当前参考图机位并共同说明完整结构的产品图;不得固定选择前两张,也不要要求强行使用全部产品图。",
|
"产品图和模特图会在后续生图阶段单独提供给生图模型使用,你这里不要尝试理解它们的具体外观,也不要要求必须使用所有输入图。",
|
||||||
params.modelImageUrl && !isProductOnlyDetailReplacement
|
params.modelImageUrl && !isProductOnlyDetailReplacement
|
||||||
? "你只需要根据当前参考图判断:这一屏是否存在真实的人物展示需求;如果没有人物需求,useModelImage 必须为 false。"
|
? "你只需要根据当前参考图判断:这一屏是否存在真实的人物展示需求;如果没有人物需求,useModelImage 必须为 false。"
|
||||||
: "本轮没有模特图,useModelImage 必须为 false。",
|
: "本轮没有模特图,useModelImage 必须为 false。",
|
||||||
@ -2628,18 +2278,12 @@ export async function analyzeReplacementExecutionPlan(params: {
|
|||||||
isDetailReplacement
|
isDetailReplacement
|
||||||
? "5. 判断 textPolicy:画面中存在营销标题、卖点、编号、参数、标签且与图匹配时必须 lock 或 preserve,避免后续生图改字、混色或重排。"
|
? "5. 判断 textPolicy:画面中存在营销标题、卖点、编号、参数、标签且与图匹配时必须 lock 或 preserve,避免后续生图改字、混色或重排。"
|
||||||
: "",
|
: "",
|
||||||
isDetailReplacement
|
|
||||||
? "5a. 必须输出 subjectBoundingBox 和 textBoundingBoxes。所有坐标为 0到1000 归一化坐标 [x1,y1,x2,y2],不是 x/y/宽/高。subjectBoundingBox 紧密框住旧商品主体及自然阴影,不含文字,无商品时为 null;textBoundingBoxes 覆盖所有中英文文案、标题、卖点、参数、标签、数字、品牌字样、按钮文字及紧邻图标,无文字时为 []。"
|
|
||||||
: "",
|
|
||||||
isDetailReplacement
|
isDetailReplacement
|
||||||
? "6. 判断 peoplePolicy:有人物/多人/自拍/聚会/围坐/聚餐/模特互动/拿手机/用餐/试坐/承重动作时必须 preserve_relation;如果文字明确写了“多人围坐、聚餐、合照、用餐、单侧承重、横向受力测试、拿手机”等动作,generationPrompt 必须把这个动作写成硬约束;如果只提供 1 张模特图但参考图多人,仍要保留参考图人数和关系,额外人物按参考图生成相近人群。"
|
? "6. 判断 peoplePolicy:有人物/多人/自拍/聚会/围坐/聚餐/模特互动/拿手机/用餐/试坐/承重动作时必须 preserve_relation;如果文字明确写了“多人围坐、聚餐、合照、用餐、单侧承重、横向受力测试、拿手机”等动作,generationPrompt 必须把这个动作写成硬约束;如果只提供 1 张模特图但参考图多人,仍要保留参考图人数和关系,额外人物按参考图生成相近人群。"
|
||||||
: "",
|
: "",
|
||||||
isDetailReplacement
|
isDetailReplacement
|
||||||
? "7. 提炼 sceneSummary、cameraSummary、placementSummary、preserveSummary、styleSummary、avoidSummary;cameraSummary 必须写出产品主体的视角特征,例如正侧视/三分之四/俯视/平视、倾斜方向、可见面数量、主体朝向和透视消失方向。"
|
? "7. 提炼 sceneSummary、cameraSummary、placementSummary、preserveSummary、styleSummary、avoidSummary;cameraSummary 必须写出产品主体的视角特征,例如正侧视/三分之四/俯视/平视、倾斜方向、可见面数量、主体朝向和透视消失方向。"
|
||||||
: "",
|
: "",
|
||||||
isDetailReplacement
|
|
||||||
? "7a. 输出 selectedProductImageIndexes:根据当前详情图机位,从产品结构画像的图片编号中选择最多两张最相关的产品图;第一张应最接近当前机位,第二张补足腿、支架、横梁、侧框或其他容易变形的结构。输出 productSelectionReason 说明选择依据。"
|
|
||||||
: "",
|
|
||||||
isProductOnlyDetailReplacement
|
isProductOnlyDetailReplacement
|
||||||
? "4. 输出 generationPrompt:这是一段给后续生图模型的本屏生成指令,要求只替换产品主体,保留其余背景、文案、排版、装饰和光影完全不变。"
|
? "4. 输出 generationPrompt:这是一段给后续生图模型的本屏生成指令,要求只替换产品主体,保留其余背景、文案、排版、装饰和光影完全不变。"
|
||||||
: isDetailReplacement
|
: isDetailReplacement
|
||||||
@ -2665,7 +2309,7 @@ export async function analyzeReplacementExecutionPlan(params: {
|
|||||||
: "",
|
: "",
|
||||||
"只返回严格 JSON,不要任何额外解释。",
|
"只返回严格 JSON,不要任何额外解释。",
|
||||||
`JSON 格式:
|
`JSON 格式:
|
||||||
{"shouldReplace":true,"containsFurnitureProduct":true,"reason":"不超过30字","pageKind":"prompt_pack_scene|white_scene|white_layout|copy_locked|people_scene|action_poster|dimension_spec|material_detail|unknown","generationRoute":"${isDetailReplacement ? "prompt_pack_scene|layout_preserve_replace|copy_locked_regenerate|people_relation_locked" : "prompt_pack_scene|layout_preserve_replace|copy_locked_regenerate|people_relation_locked|direct_passthrough"}","promptPackPolicy":"allow|soft|forbid","textPolicy":"free|preserve|lock","peoplePolicy":"none|preserve_relation|require_user_model","whiteBackgroundLikely":false,"copyImageCoupled":false,"containsPeople":false,"containsMultiplePeople":false,"subjectBoundingBox":[0,0,1000,1000],"textBoundingBoxes":[[0,0,1000,1000]],"selectedProductImageIndexes":[1,2],"productSelectionReason":"不超过80字","routeReason":"不超过50字","useModelImage":false,"modelImageReason":"不超过40字","sceneSummary":"不超过80字","cameraSummary":"不超过60字","placementSummary":"不超过60字","preserveSummary":"不超过80字","styleSummary":"不超过80字","avoidSummary":"不超过80字","generationPrompt":"不超过300字"}`,
|
{"shouldReplace":true,"containsFurnitureProduct":true,"reason":"不超过30字","pageKind":"prompt_pack_scene|white_scene|white_layout|copy_locked|people_scene|action_poster|dimension_spec|material_detail|unknown","generationRoute":"${isDetailReplacement ? "prompt_pack_scene|layout_preserve_replace|copy_locked_regenerate|people_relation_locked" : "prompt_pack_scene|layout_preserve_replace|copy_locked_regenerate|people_relation_locked|direct_passthrough"}","promptPackPolicy":"allow|soft|forbid","textPolicy":"free|preserve|lock","peoplePolicy":"none|preserve_relation|require_user_model","whiteBackgroundLikely":false,"copyImageCoupled":false,"containsPeople":false,"containsMultiplePeople":false,"routeReason":"不超过50字","useModelImage":false,"modelImageReason":"不超过40字","sceneSummary":"不超过80字","cameraSummary":"不超过60字","placementSummary":"不超过60字","preserveSummary":"不超过80字","styleSummary":"不超过80字","avoidSummary":"不超过80字","generationPrompt":"不超过300字"}`,
|
||||||
]
|
]
|
||||||
.filter(Boolean)
|
.filter(Boolean)
|
||||||
.join("\n");
|
.join("\n");
|
||||||
@ -2690,7 +2334,7 @@ export async function analyzeReplacementExecutionPlan(params: {
|
|||||||
system:
|
system:
|
||||||
"你是高精度商品详情图场景规划器。你只分析当前参考图本身,负责理解这一屏的场景、机位、主体位置和人物需求,不要臆测或依赖其他输入图片。",
|
"你是高精度商品详情图场景规划器。你只分析当前参考图本身,负责理解这一屏的场景、机位、主体位置和人物需求,不要臆测或依赖其他输入图片。",
|
||||||
temperature: params.planningRuntime.kind === "local_moonshot" ? 1 : 0.1,
|
temperature: params.planningRuntime.kind === "local_moonshot" ? 1 : 0.1,
|
||||||
maxTokens: 1600,
|
maxTokens: 1200,
|
||||||
abortSignal: AbortSignal.timeout(replacementScenePlanningTimeoutMs),
|
abortSignal: AbortSignal.timeout(replacementScenePlanningTimeoutMs),
|
||||||
} as any);
|
} as any);
|
||||||
|
|
||||||
@ -2724,9 +2368,6 @@ export async function analyzeReplacementExecutionPlan(params: {
|
|||||||
: "configured_vision",
|
: "configured_vision",
|
||||||
scenePlan: {
|
scenePlan: {
|
||||||
...normalizedPlan.scenePlan,
|
...normalizedPlan.scenePlan,
|
||||||
productIdentitySummary: productIdentityProfile?.identitySummary || "",
|
|
||||||
productStructureSummary: productIdentityProfile?.structureSummary || "",
|
|
||||||
productStructuralRisks: productIdentityProfile?.structuralRisks || "",
|
|
||||||
analysisModelName: params.planningRuntime.modelName,
|
analysisModelName: params.planningRuntime.modelName,
|
||||||
analysisSource:
|
analysisSource:
|
||||||
params.planningRuntime.kind === "local_moonshot"
|
params.planningRuntime.kind === "local_moonshot"
|
||||||
@ -2772,12 +2413,7 @@ export async function analyzeReplacementExecutionPlan(params: {
|
|||||||
);
|
);
|
||||||
return {
|
return {
|
||||||
...normalizedFallbackPlan.decision,
|
...normalizedFallbackPlan.decision,
|
||||||
scenePlan: {
|
scenePlan: normalizedFallbackPlan.scenePlan,
|
||||||
...normalizedFallbackPlan.scenePlan,
|
|
||||||
productIdentitySummary: productIdentityProfile?.identitySummary || "",
|
|
||||||
productStructureSummary: productIdentityProfile?.structureSummary || "",
|
|
||||||
productStructuralRisks: productIdentityProfile?.structuralRisks || "",
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2901,17 +2537,11 @@ export function buildAgentReplacementPrompt(params: {
|
|||||||
]
|
]
|
||||||
: [];
|
: [];
|
||||||
const productAngleLockLines = [
|
const productAngleLockLines = [
|
||||||
"【产品角度锁定规则】最终商品/主体的拍摄角度、朝向、占比、重心和透视消失方向以最后一张参考图为准;商品身份、外轮廓、腿型、支架拓扑、构件数量和连接方式以前面的产品输入图为准。",
|
"【产品角度锁定规则】最终商品/主体的拍摄角度以最后一张参考图为准,而不是以前面的产品输入图为准;前面的产品图只提供款式、材质、颜色、结构和细节。",
|
||||||
"必须对齐最后一张参考图的正侧视/三分之四/俯视/平视关系、桌面或面板倾斜方向和可见侧面数量,但不得照搬旧商品的腿部、横梁、侧框、把手、转轴或内部构件设计。",
|
"必须逐项对齐最后一张参考图中的产品姿态:朝向、正侧视/三分之四/俯视/平视关系、桌面或面板倾斜方向、可见侧面数量、支架/腿部/把手/转轴位置、主体外轮廓、占比、重心和透视消失方向。",
|
||||||
"如果最后一张参考图的产品是侧视或低视角,不允许改成俯视三分之四;如果是俯视三分之四,不允许改成正侧视;不允许镜像、旋转、翻转或把产品重画成另一个角度。",
|
"如果最后一张参考图的产品是侧视或低视角,不允许改成俯视三分之四;如果是俯视三分之四,不允许改成正侧视;不允许镜像、旋转、翻转或把产品重画成另一个角度。",
|
||||||
"若参考图含功能标签、箭头、尺寸线、编号或引导线,它们指向的实际结构部位必须保持一致,不得因产品转角改变而让标签指向错误位置。",
|
"若参考图含功能标签、箭头、尺寸线、编号或引导线,它们指向的实际结构部位必须保持一致,不得因产品转角改变而让标签指向错误位置。",
|
||||||
];
|
];
|
||||||
const productStructureLockLines = buildReplacementProductStructureLockLines({
|
|
||||||
productImageCount: params.productImageCount,
|
|
||||||
identitySummary: params.scenePlan?.productIdentitySummary,
|
|
||||||
structureSummary: params.scenePlan?.productStructureSummary,
|
|
||||||
structuralRisks: params.scenePlan?.productStructuralRisks,
|
|
||||||
});
|
|
||||||
const normalizedBasePrompt = String(params.basePrompt || "").trim();
|
const normalizedBasePrompt = String(params.basePrompt || "").trim();
|
||||||
const promptPackInstructionLines =
|
const promptPackInstructionLines =
|
||||||
params.modeType === "detail_replacement" &&
|
params.modeType === "detail_replacement" &&
|
||||||
@ -3011,7 +2641,6 @@ export function buildAgentReplacementPrompt(params: {
|
|||||||
...promptPackInstructionLines,
|
...promptPackInstructionLines,
|
||||||
...scenePlanLines,
|
...scenePlanLines,
|
||||||
...marketplaceBrandRemovalLines,
|
...marketplaceBrandRemovalLines,
|
||||||
...productStructureLockLines,
|
|
||||||
...productAngleLockLines,
|
...productAngleLockLines,
|
||||||
"你现在做的是‘仅替换产品’,不是重建场景,也不是重新设计整张详情图。",
|
"你现在做的是‘仅替换产品’,不是重建场景,也不是重新设计整张详情图。",
|
||||||
"忽略用户补充提示词里的场景风格、氛围、空间改造要求;这一次只服务于产品主体替换。",
|
"忽略用户补充提示词里的场景风格、氛围、空间改造要求;这一次只服务于产品主体替换。",
|
||||||
@ -3041,7 +2670,6 @@ export function buildAgentReplacementPrompt(params: {
|
|||||||
...promptPackInstructionLines,
|
...promptPackInstructionLines,
|
||||||
...scenePlanLines,
|
...scenePlanLines,
|
||||||
...marketplaceBrandRemovalLines,
|
...marketplaceBrandRemovalLines,
|
||||||
...productStructureLockLines,
|
|
||||||
...productAngleLockLines,
|
...productAngleLockLines,
|
||||||
"你现在做的是“详情参考图背景/场景图替换”,不是重新设计整张详情图。",
|
"你现在做的是“详情参考图背景/场景图替换”,不是重新设计整张详情图。",
|
||||||
"【版式锁层规则】把最后一张参考图当成 PSD 模板:文字、标题、编号、卖点标签、按钮、图标、白底/浅色底、留白、分割线、原有圆角照片区域和原有裁切范围都属于锁定版式层,不允许移动、缩放、重排或重新设计;不要把这些版式边界画成新的白色线框、描边或卡片。",
|
"【版式锁层规则】把最后一张参考图当成 PSD 模板:文字、标题、编号、卖点标签、按钮、图标、白底/浅色底、留白、分割线、原有圆角照片区域和原有裁切范围都属于锁定版式层,不允许移动、缩放、重排或重新设计;不要把这些版式边界画成新的白色线框、描边或卡片。",
|
||||||
@ -3157,7 +2785,6 @@ export function buildAgentReplacementPrompt(params: {
|
|||||||
"【图片顺序规则】",
|
"【图片顺序规则】",
|
||||||
...imageOrderLines,
|
...imageOrderLines,
|
||||||
...scenePlanLines,
|
...scenePlanLines,
|
||||||
...productStructureLockLines,
|
|
||||||
...productAngleLockLines,
|
...productAngleLockLines,
|
||||||
params.pairedProductIndex !== undefined
|
params.pairedProductIndex !== undefined
|
||||||
? `请优先使用第 ${params.pairedProductIndex + 1} 张输入商品图作为本张参考图的一对一替换主体。`
|
? `请优先使用第 ${params.pairedProductIndex + 1} 张输入商品图作为本张参考图的一对一替换主体。`
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
import { createLogger } from "@/lib/observability/logger";
|
import { createLogger } from "@/lib/observability/logger";
|
||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
import { getRequestErrorMessage, isProviderBalanceError, isRateLimitError } from "@/lib/request-error";
|
import { getRequestErrorMessage } from "@/lib/request-error";
|
||||||
import { generateUUID } from "@/lib/utils";
|
import { generateUUID } from "@/lib/utils";
|
||||||
|
|
||||||
const chatApiLogger = createLogger("chat.errors");
|
const chatApiLogger = createLogger("chat.errors");
|
||||||
@ -20,14 +20,6 @@ export function getChatStreamErrorMessage(error: unknown): string {
|
|||||||
error instanceof Error ? String(error.message || "").trim() : String(error || "").trim();
|
error instanceof Error ? String(error.message || "").trim() : String(error || "").trim();
|
||||||
const normalized = rawMessage.toLowerCase();
|
const normalized = rawMessage.toLowerCase();
|
||||||
|
|
||||||
if (isProviderBalanceError(error)) {
|
|
||||||
return "当前模型服务商线路余额或额度不足,请切换模型或联系管理员;与您的积分余额无关。";
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isRateLimitError(error)) {
|
|
||||||
return "当前模型服务请求过于频繁,请稍后重试或切换模型。";
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
if (
|
||||||
normalized.includes("token quota is not enough") ||
|
normalized.includes("token quota is not enough") ||
|
||||||
normalized.includes("pre_consume_token_quota_failed") ||
|
normalized.includes("pre_consume_token_quota_failed") ||
|
||||||
@ -55,16 +47,6 @@ function resolveChatApiFailure(error: unknown) {
|
|||||||
const detailMessage = getRequestErrorMessage(error);
|
const detailMessage = getRequestErrorMessage(error);
|
||||||
const normalized = String(detailMessage || "").toLowerCase();
|
const normalized = String(detailMessage || "").toLowerCase();
|
||||||
|
|
||||||
if (isProviderBalanceError(error)) {
|
|
||||||
return {
|
|
||||||
code: "server_error:chat",
|
|
||||||
message: "模型服务商线路余额或额度不足,请切换模型或联系管理员。",
|
|
||||||
status: 503,
|
|
||||||
category: "provider_balance",
|
|
||||||
detailMessage,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
if (
|
||||||
normalized.includes("payload too large") ||
|
normalized.includes("payload too large") ||
|
||||||
normalized.includes("request entity too large") ||
|
normalized.includes("request entity too large") ||
|
||||||
|
|||||||
@ -4,8 +4,6 @@ import { getLanguageModelById } from "@/lib/ai/dynamic-provider";
|
|||||||
import { modelLacksReferenceImageInputSupport } from "@/lib/ai/reference-image-capability";
|
import { modelLacksReferenceImageInputSupport } from "@/lib/ai/reference-image-capability";
|
||||||
import type { ChatMessage } from "@/lib/types";
|
import type { ChatMessage } from "@/lib/types";
|
||||||
import { convertMessagesWithImages } from "./image-media";
|
import { convertMessagesWithImages } from "./image-media";
|
||||||
import { isProviderBalanceError } from "@/lib/request-error";
|
|
||||||
import { isImageFallbackModelBalanceCoolingDown } from "@/lib/ai/runtime/image-fallback-balance-cooldown";
|
|
||||||
|
|
||||||
const chatApiLogger = createLogger("chat.imageFailover");
|
const chatApiLogger = createLogger("chat.imageFailover");
|
||||||
|
|
||||||
@ -18,9 +16,6 @@ export const IMAGE_FAILOVER_PROVIDER_ALLOWLIST = new Set([
|
|||||||
"snapgen-image",
|
"snapgen-image",
|
||||||
"openai-image",
|
"openai-image",
|
||||||
"bananaimage",
|
"bananaimage",
|
||||||
"visionary",
|
|
||||||
"atlascloud",
|
|
||||||
"vip98k",
|
|
||||||
"stepfun-image",
|
"stepfun-image",
|
||||||
"runway",
|
"runway",
|
||||||
"krea",
|
"krea",
|
||||||
@ -59,21 +54,7 @@ export function resolveImageModelRuntimeId(
|
|||||||
export function normalizeFailoverErrorMessage(error: unknown): string {
|
export function normalizeFailoverErrorMessage(error: unknown): string {
|
||||||
if (!error) return "";
|
if (!error) return "";
|
||||||
if (typeof error === "string") return error;
|
if (typeof error === "string") return error;
|
||||||
if (error instanceof Error) {
|
if (error instanceof Error) return error.message || String(error);
|
||||||
const record = error as Error & {
|
|
||||||
status?: unknown;
|
|
||||||
statusCode?: unknown;
|
|
||||||
httpStatus?: unknown;
|
|
||||||
response?: { status?: unknown };
|
|
||||||
};
|
|
||||||
const status =
|
|
||||||
record.status ??
|
|
||||||
record.statusCode ??
|
|
||||||
record.httpStatus ??
|
|
||||||
record.response?.status;
|
|
||||||
const message = error.message || String(error);
|
|
||||||
return status ? `[HTTP ${String(status)}] ${message}` : message;
|
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
return JSON.stringify(error);
|
return JSON.stringify(error);
|
||||||
} catch {
|
} catch {
|
||||||
@ -84,7 +65,6 @@ export function normalizeFailoverErrorMessage(error: unknown): string {
|
|||||||
export function shouldCooldownImageFallbackModel(errorMessage: string): boolean {
|
export function shouldCooldownImageFallbackModel(errorMessage: string): boolean {
|
||||||
const msg = errorMessage.toLowerCase();
|
const msg = errorMessage.toLowerCase();
|
||||||
return (
|
return (
|
||||||
isProviderBalanceError(errorMessage) ||
|
|
||||||
msg.includes("model not found") ||
|
msg.includes("model not found") ||
|
||||||
msg.includes("model_not_found") ||
|
msg.includes("model_not_found") ||
|
||||||
msg.includes("no available channel for model") ||
|
msg.includes("no available channel for model") ||
|
||||||
@ -93,13 +73,6 @@ export function shouldCooldownImageFallbackModel(errorMessage: string): boolean
|
|||||||
msg.includes("api key is missing") ||
|
msg.includes("api key is missing") ||
|
||||||
msg.includes("invalid api key") ||
|
msg.includes("invalid api key") ||
|
||||||
msg.includes("unauthorized") ||
|
msg.includes("unauthorized") ||
|
||||||
/(?:^|\D)401(?:\D|$)/.test(msg) ||
|
|
||||||
msg === "forbidden" ||
|
|
||||||
msg.includes("access forbidden") ||
|
|
||||||
msg.includes("access denied") ||
|
|
||||||
msg.includes("permission denied") ||
|
|
||||||
/(?:^|\D)403(?:\D|$)/.test(msg) ||
|
|
||||||
msg.includes("vip98k task failed") ||
|
|
||||||
msg.includes("cannot convert argument to a bytestring") ||
|
msg.includes("cannot convert argument to a bytestring") ||
|
||||||
msg.includes("model maintenance") ||
|
msg.includes("model maintenance") ||
|
||||||
msg.includes("maintenance") ||
|
msg.includes("maintenance") ||
|
||||||
@ -272,9 +245,6 @@ export function getImageFailoverConvertProvider(providerType?: string | null) {
|
|||||||
case "snapgen-image":
|
case "snapgen-image":
|
||||||
case "openai-image":
|
case "openai-image":
|
||||||
case "bananaimage":
|
case "bananaimage":
|
||||||
case "visionary":
|
|
||||||
case "atlascloud":
|
|
||||||
case "vip98k":
|
|
||||||
case "stepfun-image":
|
case "stepfun-image":
|
||||||
case "midjourney":
|
case "midjourney":
|
||||||
case "runway":
|
case "runway":
|
||||||
@ -326,9 +296,9 @@ export function buildImageFallbackCandidates(
|
|||||||
if (!sameProvider && !IMAGE_FAILOVER_PROVIDER_ALLOWLIST.has(providerType)) {
|
if (!sameProvider && !IMAGE_FAILOVER_PROVIDER_ALLOWLIST.has(providerType)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (isImageFallbackModelCoolingDown(item.model.id) || isImageFallbackModelBalanceCoolingDown(item.model)) {
|
if (isImageFallbackModelCoolingDown(item.model.id)) {
|
||||||
const cooldown = imageFailoverModelCooldown.get(item.model.id);
|
const cooldown = imageFailoverModelCooldown.get(item.model.id);
|
||||||
chatApiLogger.info(`[Chat API] ⏭ Skip configured image fallback on cooldown: ${item.model.displayName} (${providerType}) - ${cooldown?.reason || "provider balance cooldown"}`);
|
chatApiLogger.info(`[Chat API] ⏭ Skip configured image fallback on cooldown: ${item.model.displayName} (${providerType}) - ${cooldown?.reason || "cooldown"}`);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@ -488,9 +458,9 @@ export function getImageCrossProviderCandidates(
|
|||||||
}
|
}
|
||||||
if (m.model.id === options.currentModelId) return false;
|
if (m.model.id === options.currentModelId) return false;
|
||||||
if (options.currentProviderId && m.provider.id === options.currentProviderId) return false;
|
if (options.currentProviderId && m.provider.id === options.currentProviderId) return false;
|
||||||
if (isImageFallbackModelCoolingDown(m.model.id) || isImageFallbackModelBalanceCoolingDown(m.model)) {
|
if (isImageFallbackModelCoolingDown(m.model.id)) {
|
||||||
const cooldown = imageFailoverModelCooldown.get(m.model.id);
|
const cooldown = imageFailoverModelCooldown.get(m.model.id);
|
||||||
chatApiLogger.info(`[Chat API] ⏭ Skip image failover model on cooldown: ${m.model.displayName} (${m.provider.type}) - ${cooldown?.reason || "provider balance cooldown"}`);
|
chatApiLogger.info(`[Chat API] ⏭ Skip image failover model on cooldown: ${m.model.displayName} (${m.provider.type}) - ${cooldown?.reason || "cooldown"}`);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@ -525,9 +525,6 @@ export async function convertMessagesWithImages(
|
|||||||
normalizedProviderType === "snapgen-image" ||
|
normalizedProviderType === "snapgen-image" ||
|
||||||
normalizedProviderType === "openai-image" ||
|
normalizedProviderType === "openai-image" ||
|
||||||
normalizedProviderType === "bananaimage" ||
|
normalizedProviderType === "bananaimage" ||
|
||||||
normalizedProviderType === "visionary" ||
|
|
||||||
normalizedProviderType === "atlascloud" ||
|
|
||||||
normalizedProviderType === "vip98k" ||
|
|
||||||
normalizedProviderType === "stepfun-image" ||
|
normalizedProviderType === "stepfun-image" ||
|
||||||
normalizedProviderType === "midjourney" ||
|
normalizedProviderType === "midjourney" ||
|
||||||
normalizedProviderType === "runway" ||
|
normalizedProviderType === "runway" ||
|
||||||
|
|||||||
@ -7,7 +7,6 @@ import type {
|
|||||||
ResolvedTargetAssets,
|
ResolvedTargetAssets,
|
||||||
} from "@/lib/ai/context-memory/types";
|
} from "@/lib/ai/context-memory/types";
|
||||||
import type { CanvasViewOperationPlan } from "@/types/canvas/agent-protocol";
|
import type { CanvasViewOperationPlan } from "@/types/canvas/agent-protocol";
|
||||||
import type { CanvasCapabilityClientCommand } from "@/lib/canvas/capabilities/client-command";
|
|
||||||
import { generateUUID } from "@/lib/utils";
|
import { generateUUID } from "@/lib/utils";
|
||||||
|
|
||||||
const chatApiLogger = createLogger("chat.stream");
|
const chatApiLogger = createLogger("chat.stream");
|
||||||
@ -189,9 +188,6 @@ function normalizeImageLoadingProviderType(providerType?: string | null): string
|
|||||||
normalized === "snapgen-image" ||
|
normalized === "snapgen-image" ||
|
||||||
normalized === "openai-image" ||
|
normalized === "openai-image" ||
|
||||||
normalized === "bananaimage" ||
|
normalized === "bananaimage" ||
|
||||||
normalized === "visionary" ||
|
|
||||||
normalized === "atlascloud" ||
|
|
||||||
normalized === "vip98k" ||
|
|
||||||
normalized === "stepfun-image" ||
|
normalized === "stepfun-image" ||
|
||||||
normalized === "midjourney" ||
|
normalized === "midjourney" ||
|
||||||
normalized === "runway" ||
|
normalized === "runway" ||
|
||||||
@ -426,19 +422,6 @@ export function writeCanvasViewOperationChunk(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function writeCanvasCapabilityCommandChunk(
|
|
||||||
writer: StreamWriter,
|
|
||||||
command: CanvasCapabilityClientCommand,
|
|
||||||
label: string
|
|
||||||
) {
|
|
||||||
writeStructuredDataChunk(
|
|
||||||
writer,
|
|
||||||
"data-canvasCapabilityCommand",
|
|
||||||
command,
|
|
||||||
label
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
type AgentTargetLockRefPreview = {
|
type AgentTargetLockRefPreview = {
|
||||||
url: string;
|
url: string;
|
||||||
title?: string;
|
title?: string;
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@ -48,7 +48,6 @@ const imageParamsSchema = z.object({
|
|||||||
aspectRatio: z.string().optional(),
|
aspectRatio: z.string().optional(),
|
||||||
imageSize: z.preprocess(normalizeImageSizeInput, z.enum(imageSizeValues).optional()),
|
imageSize: z.preprocess(normalizeImageSizeInput, z.enum(imageSizeValues).optional()),
|
||||||
imageCount: z.number().min(1).max(10).optional(),
|
imageCount: z.number().min(1).max(10).optional(),
|
||||||
quality: z.enum(["auto", "low", "medium", "high"]).optional(),
|
|
||||||
// 阿里生图专用参数
|
// 阿里生图专用参数
|
||||||
size: z.string().optional(), // 自定义分辨率,如 "1024*1024"
|
size: z.string().optional(), // 自定义分辨率,如 "1024*1024"
|
||||||
negativePrompt: z.string().optional(), // 反向提示词
|
negativePrompt: z.string().optional(), // 反向提示词
|
||||||
|
|||||||
@ -3,7 +3,6 @@ import {
|
|||||||
CompleteMultipartUploadCommand,
|
CompleteMultipartUploadCommand,
|
||||||
CreateMultipartUploadCommand,
|
CreateMultipartUploadCommand,
|
||||||
HeadObjectCommand,
|
HeadObjectCommand,
|
||||||
ListPartsCommand,
|
|
||||||
PutObjectCommand,
|
PutObjectCommand,
|
||||||
UploadPartCommand,
|
UploadPartCommand,
|
||||||
} from "@aws-sdk/client-s3";
|
} from "@aws-sdk/client-s3";
|
||||||
@ -13,10 +12,6 @@ import { z } from "zod";
|
|||||||
|
|
||||||
import { auth } from "@/app/(auth)/auth";
|
import { auth } from "@/app/(auth)/auth";
|
||||||
import { upsertMediaAsset } from "@/lib/db/queries";
|
import { upsertMediaAsset } from "@/lib/db/queries";
|
||||||
import {
|
|
||||||
IMAGE_UPLOAD_MAX_BYTES,
|
|
||||||
IMAGE_UPLOAD_TOO_LARGE_MESSAGE,
|
|
||||||
} from "@/lib/image-upload-limits";
|
|
||||||
import { createLogger } from "@/lib/observability/logger";
|
import { createLogger } from "@/lib/observability/logger";
|
||||||
import {
|
import {
|
||||||
buildS3PublicUrl,
|
buildS3PublicUrl,
|
||||||
@ -26,22 +21,12 @@ import {
|
|||||||
maybeEnqueueStorageMigrationTask,
|
maybeEnqueueStorageMigrationTask,
|
||||||
resolveS3UploadTarget,
|
resolveS3UploadTarget,
|
||||||
} from "@/lib/storage/service";
|
} from "@/lib/storage/service";
|
||||||
import {
|
|
||||||
getOpenClawChatFileContentType,
|
|
||||||
isOpenClawChatFolderFileAllowed,
|
|
||||||
OPENCLAW_CHAT_FILE_MAX_BYTES,
|
|
||||||
OPENCLAW_CHAT_FILE_UPLOAD_PURPOSE,
|
|
||||||
OPENCLAW_CHAT_FOLDER_UPLOAD_PURPOSE,
|
|
||||||
OPENCLAW_CHAT_VIDEO_MAX_BYTES,
|
|
||||||
OPENCLAW_CHAT_VIDEO_UPLOAD_PURPOSE,
|
|
||||||
} from "@/lib/openclaw-chat/media-attachments";
|
|
||||||
|
|
||||||
const logger = createLogger("direct-upload-api");
|
const logger = createLogger("direct-upload-api");
|
||||||
const DIRECT_UPLOAD_MAX_BYTES = OPENCLAW_CHAT_FILE_MAX_BYTES;
|
const DIRECT_UPLOAD_MAX_BYTES = 500 * 1024 * 1024;
|
||||||
const DIRECT_UPLOAD_MEDIA_MAX_BYTES = 500 * 1024 * 1024;
|
|
||||||
const MULTIPART_THRESHOLD_BYTES = 50 * 1024 * 1024;
|
const MULTIPART_THRESHOLD_BYTES = 50 * 1024 * 1024;
|
||||||
const DEFAULT_PART_SIZE_BYTES = 8 * 1024 * 1024;
|
const DEFAULT_PART_SIZE_BYTES = 8 * 1024 * 1024;
|
||||||
const PRESIGNED_URL_EXPIRES_SECONDS = 6 * 60 * 60;
|
const PRESIGNED_URL_EXPIRES_SECONDS = 15 * 60;
|
||||||
const DIRECT_UPLOAD_PREFIX = "uploads/direct";
|
const DIRECT_UPLOAD_PREFIX = "uploads/direct";
|
||||||
|
|
||||||
function elapsedMs(startedAt: number) {
|
function elapsedMs(startedAt: number) {
|
||||||
@ -54,13 +39,6 @@ const allowedDirectUploadTypes = new Set([
|
|||||||
"image/png",
|
"image/png",
|
||||||
"image/webp",
|
"image/webp",
|
||||||
]);
|
]);
|
||||||
const allowedOpenClawVideoUploadTypes = new Set([
|
|
||||||
"video/mp4",
|
|
||||||
"video/quicktime",
|
|
||||||
"video/webm",
|
|
||||||
"video/x-m4v",
|
|
||||||
"video/x-matroska",
|
|
||||||
]);
|
|
||||||
|
|
||||||
const InitiateSchema = z.object({
|
const InitiateSchema = z.object({
|
||||||
action: z.literal("initiate"),
|
action: z.literal("initiate"),
|
||||||
@ -90,18 +68,6 @@ const CompleteSchema = z.object({
|
|||||||
clientUploadId: z.string().max(128).optional(),
|
clientUploadId: z.string().max(128).optional(),
|
||||||
});
|
});
|
||||||
|
|
||||||
const ResumeSchema = z.object({
|
|
||||||
action: z.literal("resume"),
|
|
||||||
key: z.string().min(1).max(512),
|
|
||||||
uploadId: z.string().min(1).max(512),
|
|
||||||
filename: z.string().min(1).max(256),
|
|
||||||
contentType: z.string().min(1).max(128),
|
|
||||||
size: z.number().int().positive().max(DIRECT_UPLOAD_MAX_BYTES),
|
|
||||||
partSize: z.number().int().min(5 * 1024 * 1024).max(128 * 1024 * 1024),
|
|
||||||
purpose: z.string().max(64).optional(),
|
|
||||||
clientUploadId: z.string().max(128).optional(),
|
|
||||||
});
|
|
||||||
|
|
||||||
const AbortSchema = z.object({
|
const AbortSchema = z.object({
|
||||||
action: z.literal("abort"),
|
action: z.literal("abort"),
|
||||||
key: z.string().min(1).max(512),
|
key: z.string().min(1).max(512),
|
||||||
@ -111,7 +77,6 @@ const AbortSchema = z.object({
|
|||||||
|
|
||||||
const RequestSchema = z.discriminatedUnion("action", [
|
const RequestSchema = z.discriminatedUnion("action", [
|
||||||
InitiateSchema,
|
InitiateSchema,
|
||||||
ResumeSchema,
|
|
||||||
CompleteSchema,
|
CompleteSchema,
|
||||||
AbortSchema,
|
AbortSchema,
|
||||||
]);
|
]);
|
||||||
@ -139,52 +104,19 @@ function assertUserOwnedKey(key: string, userId: string) {
|
|||||||
const normalized = String(key || "").replace(/^\/+/, "");
|
const normalized = String(key || "").replace(/^\/+/, "");
|
||||||
const prefix = `${userUploadPrefix(userId)}/`;
|
const prefix = `${userUploadPrefix(userId)}/`;
|
||||||
if (!normalized.startsWith(prefix)) {
|
if (!normalized.startsWith(prefix)) {
|
||||||
const error = new Error("Invalid upload object key");
|
throw new Error("Invalid upload object key");
|
||||||
error.name = "InvalidUploadObjectKeyError";
|
|
||||||
throw error;
|
|
||||||
}
|
}
|
||||||
return normalized;
|
return normalized;
|
||||||
}
|
}
|
||||||
|
|
||||||
function isAllowedDirectUpload(
|
function isAllowedDirectUpload(contentType: string) {
|
||||||
contentType: string,
|
return allowedDirectUploadTypes.has(contentType);
|
||||||
purpose: string | undefined,
|
|
||||||
filename: string
|
|
||||||
) {
|
|
||||||
return (
|
|
||||||
allowedDirectUploadTypes.has(contentType) ||
|
|
||||||
(purpose === OPENCLAW_CHAT_VIDEO_UPLOAD_PURPOSE &&
|
|
||||||
allowedOpenClawVideoUploadTypes.has(contentType)) ||
|
|
||||||
(purpose === OPENCLAW_CHAT_FILE_UPLOAD_PURPOSE &&
|
|
||||||
Boolean(
|
|
||||||
getOpenClawChatFileContentType({ name: filename, type: contentType })
|
|
||||||
)) ||
|
|
||||||
(purpose === OPENCLAW_CHAT_FOLDER_UPLOAD_PURPOSE &&
|
|
||||||
isOpenClawChatFolderFileAllowed({ name: filename, type: contentType }))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
function getPurposeMaxBytes(purpose?: string, contentType?: string) {
|
|
||||||
if (String(contentType || "").toLowerCase().startsWith("image/")) {
|
|
||||||
return IMAGE_UPLOAD_MAX_BYTES;
|
|
||||||
}
|
|
||||||
if (purpose === OPENCLAW_CHAT_VIDEO_UPLOAD_PURPOSE) {
|
|
||||||
return OPENCLAW_CHAT_VIDEO_MAX_BYTES;
|
|
||||||
}
|
|
||||||
if (
|
|
||||||
purpose === OPENCLAW_CHAT_FILE_UPLOAD_PURPOSE ||
|
|
||||||
purpose === OPENCLAW_CHAT_FOLDER_UPLOAD_PURPOSE
|
|
||||||
) {
|
|
||||||
return DIRECT_UPLOAD_MAX_BYTES;
|
|
||||||
}
|
|
||||||
return DIRECT_UPLOAD_MEDIA_MAX_BYTES;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function computePartSize(size: number) {
|
function computePartSize(size: number) {
|
||||||
if (size <= 100 * 1024 * 1024) return DEFAULT_PART_SIZE_BYTES;
|
if (size <= 100 * 1024 * 1024) return DEFAULT_PART_SIZE_BYTES;
|
||||||
if (size <= 500 * 1024 * 1024) return 16 * 1024 * 1024;
|
if (size <= 300 * 1024 * 1024) return 12 * 1024 * 1024;
|
||||||
if (size <= 1024 * 1024 * 1024) return 32 * 1024 * 1024;
|
return 16 * 1024 * 1024;
|
||||||
return 64 * 1024 * 1024;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function createPartUrls(params: {
|
async function createPartUrls(params: {
|
||||||
@ -192,24 +124,24 @@ async function createPartUrls(params: {
|
|||||||
bucket: string;
|
bucket: string;
|
||||||
key: string;
|
key: string;
|
||||||
uploadId: string;
|
uploadId: string;
|
||||||
partNumbers: number[];
|
partCount: number;
|
||||||
}) {
|
}) {
|
||||||
return Promise.all(
|
const urls = [];
|
||||||
params.partNumbers.map(async (partNumber) => {
|
for (let partNumber = 1; partNumber <= params.partCount; partNumber += 1) {
|
||||||
const command = new UploadPartCommand({
|
const command = new UploadPartCommand({
|
||||||
Bucket: params.bucket,
|
Bucket: params.bucket,
|
||||||
Key: params.key,
|
Key: params.key,
|
||||||
UploadId: params.uploadId,
|
UploadId: params.uploadId,
|
||||||
PartNumber: partNumber,
|
PartNumber: partNumber,
|
||||||
});
|
});
|
||||||
return {
|
urls.push({
|
||||||
partNumber,
|
partNumber,
|
||||||
url: await getSignedUrl(params.client, command, {
|
url: await getSignedUrl(params.client, command, {
|
||||||
expiresIn: PRESIGNED_URL_EXPIRES_SECONDS,
|
expiresIn: PRESIGNED_URL_EXPIRES_SECONDS,
|
||||||
}),
|
}),
|
||||||
};
|
});
|
||||||
})
|
}
|
||||||
);
|
return urls;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function POST(request: Request) {
|
export async function POST(request: Request) {
|
||||||
@ -272,7 +204,7 @@ export async function POST(request: Request) {
|
|||||||
payload.filename
|
payload.filename
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!isAllowedDirectUpload(contentType, payload.purpose, payload.filename)) {
|
if (!isAllowedDirectUpload(contentType)) {
|
||||||
logger.warn("unsupported_direct_upload_type", {
|
logger.warn("unsupported_direct_upload_type", {
|
||||||
contentType,
|
contentType,
|
||||||
filenameExtension: payload.filename.toLowerCase().split(".").pop() || null,
|
filenameExtension: payload.filename.toLowerCase().split(".").pop() || null,
|
||||||
@ -281,24 +213,10 @@ export async function POST(request: Request) {
|
|||||||
clientUploadId: payload.clientUploadId || null,
|
clientUploadId: payload.clientUploadId || null,
|
||||||
});
|
});
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{
|
{ error: "Direct upload only supports JPG, PNG and WebP images" },
|
||||||
error:
|
|
||||||
"直传仅支持图片、Claw 视频、压缩包、表格和文件夹清单",
|
|
||||||
},
|
|
||||||
{ status: 400 }
|
{ status: 400 }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
const maxBytes = getPurposeMaxBytes(payload.purpose, contentType);
|
|
||||||
if (payload.size > maxBytes) {
|
|
||||||
return NextResponse.json(
|
|
||||||
{
|
|
||||||
error: contentType.startsWith("image/")
|
|
||||||
? IMAGE_UPLOAD_TOO_LARGE_MESSAGE
|
|
||||||
: `文件不能超过 ${maxBytes / 1024 / 1024}MB`,
|
|
||||||
},
|
|
||||||
{ status: 413 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const displayName = sanitizeDisplayName(payload.filename);
|
const displayName = sanitizeDisplayName(payload.filename);
|
||||||
const key = buildStorageObjectKey(
|
const key = buildStorageObjectKey(
|
||||||
@ -306,13 +224,7 @@ export async function POST(request: Request) {
|
|||||||
userUploadPrefix(session.user.id)
|
userUploadPrefix(session.user.id)
|
||||||
);
|
);
|
||||||
|
|
||||||
const multipartThreshold =
|
if (payload.size < MULTIPART_THRESHOLD_BYTES) {
|
||||||
payload.purpose === OPENCLAW_CHAT_FILE_UPLOAD_PURPOSE ||
|
|
||||||
payload.purpose === OPENCLAW_CHAT_VIDEO_UPLOAD_PURPOSE ||
|
|
||||||
payload.purpose === OPENCLAW_CHAT_FOLDER_UPLOAD_PURPOSE
|
|
||||||
? DEFAULT_PART_SIZE_BYTES
|
|
||||||
: MULTIPART_THRESHOLD_BYTES;
|
|
||||||
if (payload.size < multipartThreshold) {
|
|
||||||
const presignStartedAt = Date.now();
|
const presignStartedAt = Date.now();
|
||||||
const command = new PutObjectCommand({
|
const command = new PutObjectCommand({
|
||||||
Bucket: bucket,
|
Bucket: bucket,
|
||||||
@ -372,7 +284,7 @@ export async function POST(request: Request) {
|
|||||||
bucket,
|
bucket,
|
||||||
key,
|
key,
|
||||||
uploadId,
|
uploadId,
|
||||||
partNumbers: Array.from({ length: partCount }, (_, index) => index + 1),
|
partCount,
|
||||||
});
|
});
|
||||||
logger.info("direct_upload_multipart_prepared", {
|
logger.info("direct_upload_multipart_prepared", {
|
||||||
elapsedMs: elapsedMs(multipartStartedAt),
|
elapsedMs: elapsedMs(multipartStartedAt),
|
||||||
@ -396,105 +308,6 @@ export async function POST(request: Request) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (payload.action === "resume") {
|
|
||||||
const key = assertUserOwnedKey(payload.key, session.user.id);
|
|
||||||
const contentType = normalizeContentType(payload.contentType, payload.filename);
|
|
||||||
if (!isAllowedDirectUpload(contentType, payload.purpose, payload.filename)) {
|
|
||||||
return NextResponse.json({ error: "不支持续传该文件类型" }, { status: 400 });
|
|
||||||
}
|
|
||||||
if (payload.size > getPurposeMaxBytes(payload.purpose, contentType)) {
|
|
||||||
return NextResponse.json(
|
|
||||||
{
|
|
||||||
error: contentType.startsWith("image/")
|
|
||||||
? IMAGE_UPLOAD_TOO_LARGE_MESSAGE
|
|
||||||
: "文件超过允许的上传大小",
|
|
||||||
},
|
|
||||||
{ status: 413 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
const expectedPartSize = computePartSize(payload.size);
|
|
||||||
if (payload.partSize !== expectedPartSize) {
|
|
||||||
return NextResponse.json(
|
|
||||||
{ error: "续传分片参数已失效,请重新开始上传", code: "RESUME_SESSION_EXPIRED" },
|
|
||||||
{ status: 409 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
const listed = await client.send(
|
|
||||||
new ListPartsCommand({
|
|
||||||
Bucket: bucket,
|
|
||||||
Key: key,
|
|
||||||
UploadId: payload.uploadId,
|
|
||||||
MaxParts: 1000,
|
|
||||||
})
|
|
||||||
);
|
|
||||||
const uploadedParts = (listed.Parts || []).flatMap((part) => {
|
|
||||||
if (!part.ETag || typeof part.PartNumber !== "number") return [];
|
|
||||||
return [
|
|
||||||
{
|
|
||||||
ETag: part.ETag,
|
|
||||||
PartNumber: part.PartNumber,
|
|
||||||
size: Number(part.Size || 0),
|
|
||||||
},
|
|
||||||
];
|
|
||||||
});
|
|
||||||
const uploadedPartNumbers = new Set(
|
|
||||||
uploadedParts.map((part) => part.PartNumber)
|
|
||||||
);
|
|
||||||
const partCount = Math.ceil(payload.size / expectedPartSize);
|
|
||||||
const missingPartNumbers = Array.from(
|
|
||||||
{ length: partCount },
|
|
||||||
(_, index) => index + 1
|
|
||||||
).filter((partNumber) => !uploadedPartNumbers.has(partNumber));
|
|
||||||
const parts = await createPartUrls({
|
|
||||||
client,
|
|
||||||
bucket,
|
|
||||||
key,
|
|
||||||
uploadId: payload.uploadId,
|
|
||||||
partNumbers: missingPartNumbers,
|
|
||||||
});
|
|
||||||
|
|
||||||
logger.info("direct_upload_multipart_resumed", {
|
|
||||||
totalElapsedMs: elapsedMs(requestStartedAt),
|
|
||||||
size: payload.size,
|
|
||||||
uploadedPartCount: uploadedParts.length,
|
|
||||||
missingPartCount: missingPartNumbers.length,
|
|
||||||
clientUploadId: payload.clientUploadId || null,
|
|
||||||
});
|
|
||||||
return NextResponse.json({
|
|
||||||
mode: "multipart",
|
|
||||||
key,
|
|
||||||
uploadId: payload.uploadId,
|
|
||||||
partSize: expectedPartSize,
|
|
||||||
partCount,
|
|
||||||
parts,
|
|
||||||
uploadedParts,
|
|
||||||
resumed: true,
|
|
||||||
expiresIn: PRESIGNED_URL_EXPIRES_SECONDS,
|
|
||||||
clientUploadId: payload.clientUploadId || null,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
const head = await client
|
|
||||||
.send(new HeadObjectCommand({ Bucket: bucket, Key: key }))
|
|
||||||
.catch(() => null);
|
|
||||||
if (head?.ContentLength === payload.size) {
|
|
||||||
return NextResponse.json({
|
|
||||||
mode: "completed",
|
|
||||||
key,
|
|
||||||
clientUploadId: payload.clientUploadId || null,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
logger.warn("direct_upload_resume_session_missing", error, {
|
|
||||||
clientUploadId: payload.clientUploadId || null,
|
|
||||||
});
|
|
||||||
return NextResponse.json(
|
|
||||||
{ error: "续传会话已失效,请重新开始上传", code: "RESUME_SESSION_EXPIRED" },
|
|
||||||
{ status: 409 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (payload.action === "abort") {
|
if (payload.action === "abort") {
|
||||||
const key = assertUserOwnedKey(payload.key, session.user.id);
|
const key = assertUserOwnedKey(payload.key, session.user.id);
|
||||||
const abortStartedAt = Date.now();
|
const abortStartedAt = Date.now();
|
||||||
@ -515,25 +328,12 @@ export async function POST(request: Request) {
|
|||||||
|
|
||||||
const key = assertUserOwnedKey(payload.key, session.user.id);
|
const key = assertUserOwnedKey(payload.key, session.user.id);
|
||||||
const contentType = normalizeContentType(payload.contentType, payload.filename);
|
const contentType = normalizeContentType(payload.contentType, payload.filename);
|
||||||
if (!isAllowedDirectUpload(contentType, payload.purpose, payload.filename)) {
|
if (!isAllowedDirectUpload(contentType)) {
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{
|
{ error: "Direct upload only supports JPG, PNG and WebP images" },
|
||||||
error:
|
|
||||||
"直传仅支持图片、Claw 视频、压缩包、表格和文件夹清单",
|
|
||||||
},
|
|
||||||
{ status: 400 }
|
{ status: 400 }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if (payload.size > getPurposeMaxBytes(payload.purpose, contentType)) {
|
|
||||||
return NextResponse.json(
|
|
||||||
{
|
|
||||||
error: contentType.startsWith("image/")
|
|
||||||
? IMAGE_UPLOAD_TOO_LARGE_MESSAGE
|
|
||||||
: "文件超过允许的上传大小",
|
|
||||||
},
|
|
||||||
{ status: 413 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (payload.uploadId) {
|
if (payload.uploadId) {
|
||||||
const completeMultipartStartedAt = Date.now();
|
const completeMultipartStartedAt = Date.now();
|
||||||
@ -579,10 +379,8 @@ export async function POST(request: Request) {
|
|||||||
|
|
||||||
const actualSize =
|
const actualSize =
|
||||||
typeof head.ContentLength === "number" ? head.ContentLength : null;
|
typeof head.ContentLength === "number" ? head.ContentLength : null;
|
||||||
if (actualSize !== null && actualSize !== payload.size) {
|
if (actualSize !== null && actualSize <= 0) {
|
||||||
throw new Error(
|
throw new Error("Uploaded object is empty");
|
||||||
`Uploaded object size mismatch: expected ${payload.size}, received ${actualSize}`
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const url = buildS3PublicUrl(target.config, key);
|
const url = buildS3PublicUrl(target.config, key);
|
||||||
@ -591,43 +389,15 @@ export async function POST(request: Request) {
|
|||||||
const assetStartedAt = Date.now();
|
const assetStartedAt = Date.now();
|
||||||
const asset = await upsertMediaAsset({
|
const asset = await upsertMediaAsset({
|
||||||
userId: session.user.id,
|
userId: session.user.id,
|
||||||
mediaType:
|
mediaType: "image",
|
||||||
payload.purpose === OPENCLAW_CHAT_FILE_UPLOAD_PURPOSE ||
|
sourceType: "upload",
|
||||||
payload.purpose === OPENCLAW_CHAT_FOLDER_UPLOAD_PURPOSE
|
|
||||||
? "file"
|
|
||||||
: resolvedContentType.startsWith("video/")
|
|
||||||
? "video"
|
|
||||||
: "image",
|
|
||||||
sourceType:
|
|
||||||
payload.purpose === OPENCLAW_CHAT_VIDEO_UPLOAD_PURPOSE
|
|
||||||
? "openclaw_chat_video_upload"
|
|
||||||
: payload.purpose === OPENCLAW_CHAT_FILE_UPLOAD_PURPOSE
|
|
||||||
? "openclaw_chat_file_upload"
|
|
||||||
: payload.purpose === OPENCLAW_CHAT_FOLDER_UPLOAD_PURPOSE
|
|
||||||
? "openclaw_chat_folder_file_upload"
|
|
||||||
: "upload",
|
|
||||||
status: "ready",
|
status: "ready",
|
||||||
originalUrl: url,
|
originalUrl: url,
|
||||||
previewUrl:
|
previewUrl: url,
|
||||||
payload.purpose === OPENCLAW_CHAT_FILE_UPLOAD_PURPOSE ||
|
thumbnailUrl: url,
|
||||||
payload.purpose === OPENCLAW_CHAT_FOLDER_UPLOAD_PURPOSE
|
|
||||||
? null
|
|
||||||
: url,
|
|
||||||
thumbnailUrl:
|
|
||||||
payload.purpose === OPENCLAW_CHAT_FILE_UPLOAD_PURPOSE ||
|
|
||||||
payload.purpose === OPENCLAW_CHAT_FOLDER_UPLOAD_PURPOSE
|
|
||||||
? null
|
|
||||||
: url,
|
|
||||||
mimeType: resolvedContentType,
|
mimeType: resolvedContentType,
|
||||||
displayName: payload.filename,
|
displayName: payload.filename,
|
||||||
sourceTable:
|
sourceTable: "direct_upload",
|
||||||
payload.purpose === OPENCLAW_CHAT_VIDEO_UPLOAD_PURPOSE
|
|
||||||
? "openclaw_chat_video_upload"
|
|
||||||
: payload.purpose === OPENCLAW_CHAT_FILE_UPLOAD_PURPOSE
|
|
||||||
? "openclaw_chat_file_upload"
|
|
||||||
: payload.purpose === OPENCLAW_CHAT_FOLDER_UPLOAD_PURPOSE
|
|
||||||
? "openclaw_chat_folder_file_upload"
|
|
||||||
: "direct_upload",
|
|
||||||
metadata: {
|
metadata: {
|
||||||
pathname: key,
|
pathname: key,
|
||||||
size: actualSize ?? payload.size,
|
size: actualSize ?? payload.size,
|
||||||
@ -674,13 +444,7 @@ export async function POST(request: Request) {
|
|||||||
error: "Direct upload failed",
|
error: "Direct upload failed",
|
||||||
details: error instanceof Error ? error.message : String(error),
|
details: error instanceof Error ? error.message : String(error),
|
||||||
},
|
},
|
||||||
{
|
{ status: 500 }
|
||||||
status:
|
|
||||||
error instanceof Error &&
|
|
||||||
error.name === "InvalidUploadObjectKeyError"
|
|
||||||
? 403
|
|
||||||
: 500,
|
|
||||||
}
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,9 +1,5 @@
|
|||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import {
|
|
||||||
IMAGE_UPLOAD_TOO_LARGE_MESSAGE,
|
|
||||||
isImageUploadTooLarge,
|
|
||||||
} from "@/lib/image-upload-limits";
|
|
||||||
|
|
||||||
import { auth } from "@/app/(auth)/auth";
|
import { auth } from "@/app/(auth)/auth";
|
||||||
import { createAgentWorkflowPreviewImageBuffer } from "@/lib/agents/workflow-preview-images";
|
import { createAgentWorkflowPreviewImageBuffer } from "@/lib/agents/workflow-preview-images";
|
||||||
@ -11,7 +7,6 @@ import { upsertMediaAsset } from "@/lib/db/queries";
|
|||||||
import { isSiteAdminType } from "@/lib/middleware/admin";
|
import { isSiteAdminType } from "@/lib/middleware/admin";
|
||||||
import { getContentType, uploadFile } from "@/lib/storage/service";
|
import { getContentType, uploadFile } from "@/lib/storage/service";
|
||||||
import { createLogger } from "@/lib/observability/logger";
|
import { createLogger } from "@/lib/observability/logger";
|
||||||
import { OPENCLAW_CHAT_VIDEO_UPLOAD_PURPOSE } from "@/lib/openclaw-chat/media-attachments";
|
|
||||||
|
|
||||||
const logger = createLogger("upload-api");
|
const logger = createLogger("upload-api");
|
||||||
const ALLOWED_UPLOAD_IMAGE_TYPES = new Set([
|
const ALLOWED_UPLOAD_IMAGE_TYPES = new Set([
|
||||||
@ -19,32 +14,12 @@ const ALLOWED_UPLOAD_IMAGE_TYPES = new Set([
|
|||||||
"image/jpg",
|
"image/jpg",
|
||||||
"image/png",
|
"image/png",
|
||||||
"image/webp",
|
"image/webp",
|
||||||
"image/avif",
|
|
||||||
"image/tiff",
|
|
||||||
"image/x-tiff",
|
|
||||||
"image/svg+xml",
|
"image/svg+xml",
|
||||||
"image/heic",
|
"image/heic",
|
||||||
"image/heif",
|
"image/heif",
|
||||||
]);
|
]);
|
||||||
const ALLOWED_ATLASCLOUD_VIDEO_TYPES = new Set([
|
|
||||||
"video/mp4",
|
|
||||||
"video/quicktime",
|
|
||||||
"video/webm",
|
|
||||||
]);
|
|
||||||
const ALLOWED_OPENCLAW_VIDEO_TYPES = new Set([
|
|
||||||
"video/mp4",
|
|
||||||
"video/quicktime",
|
|
||||||
"video/webm",
|
|
||||||
"video/x-m4v",
|
|
||||||
"video/x-matroska",
|
|
||||||
]);
|
|
||||||
|
|
||||||
const HEIC_UPLOAD_TYPES = new Set(["image/heic", "image/heif"]);
|
const HEIC_UPLOAD_TYPES = new Set(["image/heic", "image/heif"]);
|
||||||
const SHARP_COMPATIBILITY_UPLOAD_TYPES = new Set([
|
|
||||||
"image/avif",
|
|
||||||
"image/tiff",
|
|
||||||
"image/x-tiff",
|
|
||||||
]);
|
|
||||||
|
|
||||||
function normalizeUploadContentType(contentType: string, filename: string) {
|
function normalizeUploadContentType(contentType: string, filename: string) {
|
||||||
const normalized = contentType.trim().toLowerCase();
|
const normalized = contentType.trim().toLowerCase();
|
||||||
@ -60,16 +35,8 @@ function createUnsupportedImageTypeMessage(contentType: string, filename: string
|
|||||||
if (contentType === "image/heic" || contentType === "image/heif" || extension === "heic" || extension === "heif") {
|
if (contentType === "image/heic" || contentType === "image/heif" || extension === "heic" || extension === "heif") {
|
||||||
return "HEIC/HEIF 图片转码失败,请在手机相册中导出为 JPG/PNG 后再上传。";
|
return "HEIC/HEIF 图片转码失败,请在手机相册中导出为 JPG/PNG 后再上传。";
|
||||||
}
|
}
|
||||||
if (
|
|
||||||
SHARP_COMPATIBILITY_UPLOAD_TYPES.has(contentType) ||
|
|
||||||
extension === "avif" ||
|
|
||||||
extension === "tif" ||
|
|
||||||
extension === "tiff"
|
|
||||||
) {
|
|
||||||
return "AVIF/TIFF 图片转码失败,请导出为 JPG/PNG 后再上传。";
|
|
||||||
}
|
|
||||||
|
|
||||||
return "图片格式不支持,请上传 JPG、PNG、WebP、HEIC、AVIF 或 TIFF 图片。";
|
return "File type should be JPEG, PNG, WebP or SVG";
|
||||||
}
|
}
|
||||||
|
|
||||||
function isHeicUpload(contentType: string, filename: string) {
|
function isHeicUpload(contentType: string, filename: string) {
|
||||||
@ -81,20 +48,10 @@ function isHeicUpload(contentType: string, filename: string) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function isSharpCompatibilityUpload(contentType: string, filename: string) {
|
|
||||||
const extension = filename.toLowerCase().split(".").pop() || "";
|
|
||||||
return (
|
|
||||||
SHARP_COMPATIBILITY_UPLOAD_TYPES.has(contentType) ||
|
|
||||||
extension === "avif" ||
|
|
||||||
extension === "tif" ||
|
|
||||||
extension === "tiff"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
function toJpegFilename(filename: string) {
|
function toJpegFilename(filename: string) {
|
||||||
const trimmed = filename.trim() || `upload-${Date.now()}`;
|
const trimmed = filename.trim() || `upload-${Date.now()}`;
|
||||||
if (/\.(avif|heic|heif|tif|tiff)$/i.test(trimmed)) {
|
if (/\.(heic|heif)$/i.test(trimmed)) {
|
||||||
return trimmed.replace(/\.(avif|heic|heif|tif|tiff)$/i, ".jpg");
|
return trimmed.replace(/\.(heic|heif)$/i, ".jpg");
|
||||||
}
|
}
|
||||||
if (/\.[a-z0-9]+$/i.test(trimmed)) {
|
if (/\.[a-z0-9]+$/i.test(trimmed)) {
|
||||||
return trimmed.replace(/\.[a-z0-9]+$/i, ".jpg");
|
return trimmed.replace(/\.[a-z0-9]+$/i, ".jpg");
|
||||||
@ -114,21 +71,6 @@ async function convertHeicToJpeg(buffer: Buffer): Promise<Buffer> {
|
|||||||
}
|
}
|
||||||
return Buffer.from(output);
|
return Buffer.from(output);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function convertSharpCompatibilityImageToJpeg(
|
|
||||||
buffer: Buffer
|
|
||||||
): Promise<Buffer> {
|
|
||||||
const sharp = (await import("sharp")).default;
|
|
||||||
return sharp(buffer, { animated: false, failOn: "none" })
|
|
||||||
.rotate()
|
|
||||||
.flatten({ background: "#ffffff" })
|
|
||||||
.jpeg({
|
|
||||||
quality: 92,
|
|
||||||
mozjpeg: true,
|
|
||||||
progressive: true,
|
|
||||||
})
|
|
||||||
.toBuffer();
|
|
||||||
}
|
|
||||||
/**
|
/**
|
||||||
* 使用Sharp智能优化图片(保持原格式)
|
* 使用Sharp智能优化图片(保持原格式)
|
||||||
* 策略:避免双重压缩,只在需要时优化
|
* 策略:避免双重压缩,只在需要时优化
|
||||||
@ -255,21 +197,9 @@ export async function POST(request: Request) {
|
|||||||
let filename = originalFilename;
|
let filename = originalFilename;
|
||||||
let contentType = originalContentType;
|
let contentType = originalContentType;
|
||||||
const uploadPurpose = String(formData.get("purpose") || "");
|
const uploadPurpose = String(formData.get("purpose") || "");
|
||||||
const isAtlasCloudReferenceVideo =
|
|
||||||
uploadPurpose === "canvas-video-process" &&
|
|
||||||
ALLOWED_ATLASCLOUD_VIDEO_TYPES.has(contentType);
|
|
||||||
const isOpenClawChatVideoUpload =
|
|
||||||
uploadPurpose === OPENCLAW_CHAT_VIDEO_UPLOAD_PURPOSE &&
|
|
||||||
ALLOWED_OPENCLAW_VIDEO_TYPES.has(contentType);
|
|
||||||
const clientUploadId =
|
const clientUploadId =
|
||||||
String(formData.get("clientUploadId") || "").trim() || null;
|
String(formData.get("clientUploadId") || "").trim() || null;
|
||||||
const isSvgUpload = contentType === "image/svg+xml";
|
const isSvgUpload = contentType === "image/svg+xml";
|
||||||
if (contentType.startsWith("image/") && isImageUploadTooLarge(file.size)) {
|
|
||||||
return NextResponse.json(
|
|
||||||
{ error: IMAGE_UPLOAD_TOO_LARGE_MESSAGE },
|
|
||||||
{ status: 413 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if (
|
if (
|
||||||
isSvgUpload &&
|
isSvgUpload &&
|
||||||
(uploadPurpose !== "admin-svg-icon" || !isSiteAdminType(session.user.type))
|
(uploadPurpose !== "admin-svg-icon" || !isSiteAdminType(session.user.type))
|
||||||
@ -297,11 +227,7 @@ export async function POST(request: Request) {
|
|||||||
return NextResponse.json({ error: errorMessage }, { status: 400 });
|
return NextResponse.json({ error: errorMessage }, { status: 400 });
|
||||||
}
|
}
|
||||||
|
|
||||||
if (
|
if (!ALLOWED_UPLOAD_IMAGE_TYPES.has(contentType)) {
|
||||||
!ALLOWED_UPLOAD_IMAGE_TYPES.has(contentType) &&
|
|
||||||
!isAtlasCloudReferenceVideo &&
|
|
||||||
!isOpenClawChatVideoUpload
|
|
||||||
) {
|
|
||||||
const errorMessage = createUnsupportedImageTypeMessage(contentType, filename);
|
const errorMessage = createUnsupportedImageTypeMessage(contentType, filename);
|
||||||
logger.warn("[Upload API] Unsupported upload content type", {
|
logger.warn("[Upload API] Unsupported upload content type", {
|
||||||
contentType,
|
contentType,
|
||||||
@ -351,38 +277,6 @@ export async function POST(request: Request) {
|
|||||||
{ status: 400 }
|
{ status: 400 }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
} else if (isSharpCompatibilityUpload(contentType, filename)) {
|
|
||||||
try {
|
|
||||||
const convertedBuffer =
|
|
||||||
await convertSharpCompatibilityImageToJpeg(uploadBuffer);
|
|
||||||
logger.info("[Upload API] Compatibility image converted to JPEG", {
|
|
||||||
clientUploadId,
|
|
||||||
originalContentType: contentType,
|
|
||||||
originalSize: uploadBuffer.length,
|
|
||||||
convertedSize: convertedBuffer.length,
|
|
||||||
});
|
|
||||||
uploadBuffer = convertedBuffer;
|
|
||||||
filename = toJpegFilename(filename);
|
|
||||||
convertedFrom = contentType;
|
|
||||||
contentType = "image/jpeg";
|
|
||||||
} catch (conversionError) {
|
|
||||||
logger.warn("[Upload API] Compatibility image conversion failed", {
|
|
||||||
clientUploadId,
|
|
||||||
contentType,
|
|
||||||
size: uploadBuffer.length,
|
|
||||||
error:
|
|
||||||
conversionError instanceof Error
|
|
||||||
? conversionError.message
|
|
||||||
: String(conversionError),
|
|
||||||
});
|
|
||||||
return NextResponse.json(
|
|
||||||
{
|
|
||||||
error: createUnsupportedImageTypeMessage(contentType, filename),
|
|
||||||
clientUploadId,
|
|
||||||
},
|
|
||||||
{ status: 400 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (uploadPurpose === "agent-workflow-preview" && contentType !== "image/svg+xml") {
|
if (uploadPurpose === "agent-workflow-preview" && contentType !== "image/svg+xml") {
|
||||||
@ -467,32 +361,18 @@ export async function POST(request: Request) {
|
|||||||
try {
|
try {
|
||||||
const asset = await upsertMediaAsset({
|
const asset = await upsertMediaAsset({
|
||||||
userId: session.user.id,
|
userId: session.user.id,
|
||||||
mediaType:
|
mediaType: "image",
|
||||||
isAtlasCloudReferenceVideo || isOpenClawChatVideoUpload
|
sourceType: "upload",
|
||||||
? "video"
|
|
||||||
: "image",
|
|
||||||
sourceType: isOpenClawChatVideoUpload
|
|
||||||
? "openclaw_chat_video_upload"
|
|
||||||
: "upload",
|
|
||||||
status: "ready",
|
status: "ready",
|
||||||
originalUrl: data.url,
|
originalUrl: data.url,
|
||||||
previewUrl:
|
previewUrl: data.url,
|
||||||
isAtlasCloudReferenceVideo || isOpenClawChatVideoUpload
|
thumbnailUrl: data.url,
|
||||||
? null
|
|
||||||
: data.url,
|
|
||||||
thumbnailUrl:
|
|
||||||
isAtlasCloudReferenceVideo || isOpenClawChatVideoUpload
|
|
||||||
? null
|
|
||||||
: data.url,
|
|
||||||
mimeType: data.contentType || contentType || null,
|
mimeType: data.contentType || contentType || null,
|
||||||
displayName: filename,
|
displayName: filename,
|
||||||
sourceTable: isOpenClawChatVideoUpload
|
sourceTable: "upload",
|
||||||
? "openclaw_chat_video_upload"
|
|
||||||
: "upload",
|
|
||||||
metadata: {
|
metadata: {
|
||||||
pathname: data.pathname,
|
pathname: data.pathname,
|
||||||
size: optimizedBuffer.length,
|
size: optimizedBuffer.length,
|
||||||
purpose: uploadPurpose || null,
|
|
||||||
clientUploadId,
|
clientUploadId,
|
||||||
convertedFrom,
|
convertedFrom,
|
||||||
},
|
},
|
||||||
|
|||||||
@ -102,8 +102,8 @@ export async function GET(request: Request) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: "local-e2e-video-model",
|
id: "local-e2e-video-model",
|
||||||
modelId: "veo-3.1-fast",
|
modelId: "kling-3-local",
|
||||||
displayName: "Veo 3.1 Fast(本地布局测试)",
|
displayName: "可灵 3.0",
|
||||||
avatarUrl: null,
|
avatarUrl: null,
|
||||||
description: "本地视频画布测试视频模型",
|
description: "本地视频画布测试视频模型",
|
||||||
supportsVision: true,
|
supportsVision: true,
|
||||||
@ -111,18 +111,18 @@ export async function GET(request: Request) {
|
|||||||
isMultimodal: true,
|
isMultimodal: true,
|
||||||
isDefault: false,
|
isDefault: false,
|
||||||
providerName: "Local E2E",
|
providerName: "Local E2E",
|
||||||
providerType: "vip98k",
|
providerType: "local",
|
||||||
isSubModel: false,
|
isSubModel: false,
|
||||||
parentModelId: null,
|
parentModelId: null,
|
||||||
capabilities: { kind: "video", supportedModes: ["text-to-video", "first-frame", "first-last-frame", "reference-images"], supportedRatios:["16:9"], supportedResolutions:["720p","1080p"], durationOptions:[8], maxReferenceImages:3 },
|
capabilities: { video: true },
|
||||||
videoToolCapability: {
|
videoToolCapability: {
|
||||||
supported: true,
|
supported: true,
|
||||||
durations: [8],
|
durations: [5, 10],
|
||||||
resolutions: ["720p", "1080p"],
|
resolutions: ["720p", "1080p"],
|
||||||
},
|
},
|
||||||
imageGenConfig: {
|
imageGenConfig: {
|
||||||
creditCostPerVideo: 20,
|
creditCostPerVideo: 20,
|
||||||
videoDurationOptions: [8],
|
videoDurationOptions: [5, 10],
|
||||||
},
|
},
|
||||||
sortOrder: 1,
|
sortOrder: 1,
|
||||||
selectable: true,
|
selectable: true,
|
||||||
|
|||||||
@ -3,5 +3,10 @@ export default function FlowBoardLayout({
|
|||||||
}: {
|
}: {
|
||||||
children: React.ReactNode;
|
children: React.ReactNode;
|
||||||
}) {
|
}) {
|
||||||
return <>{children}</>;
|
return (
|
||||||
|
<>
|
||||||
|
<link href="/vendor/drawnix.css" rel="stylesheet" />
|
||||||
|
{children}
|
||||||
|
</>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +1,5 @@
|
|||||||
import { Suspense } from "react";
|
import { Suspense } from "react";
|
||||||
import { ChatShell } from "@/components/chat-shell";
|
import { ChatShell } from "@/components/chat-shell";
|
||||||
import { CanvasLogoLoadingView } from "@/components/canvas/canvas-logo-loading";
|
|
||||||
import { DataStreamProvider } from "@/components/data-stream-provider";
|
import { DataStreamProvider } from "@/components/data-stream-provider";
|
||||||
import { auth } from "../(auth)/auth";
|
import { auth } from "../(auth)/auth";
|
||||||
import {
|
import {
|
||||||
@ -9,14 +8,10 @@ import {
|
|||||||
getTutorialVideos,
|
getTutorialVideos,
|
||||||
} from "@/lib/db/queries";
|
} from "@/lib/db/queries";
|
||||||
|
|
||||||
export const dynamic = "force-dynamic";
|
|
||||||
|
|
||||||
export default function Layout({ children }: { children: React.ReactNode }) {
|
export default function Layout({ children }: { children: React.ReactNode }) {
|
||||||
return (
|
return (
|
||||||
<DataStreamProvider>
|
<DataStreamProvider>
|
||||||
<Suspense
|
<Suspense fallback={<div className="flex h-dvh" />}>
|
||||||
fallback={<CanvasLogoLoadingView label="正在加载页面..." />}
|
|
||||||
>
|
|
||||||
<SidebarWrapper>{children}</SidebarWrapper>
|
<SidebarWrapper>{children}</SidebarWrapper>
|
||||||
</Suspense>
|
</Suspense>
|
||||||
</DataStreamProvider>
|
</DataStreamProvider>
|
||||||
|
|||||||
@ -1,75 +1,79 @@
|
|||||||
|
import { randomUUID } from "node:crypto";
|
||||||
import { redirect } from "next/navigation";
|
import { redirect } from "next/navigation";
|
||||||
import { and, desc, eq } from "drizzle-orm";
|
|
||||||
import { auth } from "@/app/(auth)/auth";
|
import { auth } from "@/app/(auth)/auth";
|
||||||
import { VideoCanvasHome, type VideoCanvasHomeProject } from "@/components/canvas/VideoCanvasHome";
|
|
||||||
import { db } from "@/lib/db";
|
import { db } from "@/lib/db";
|
||||||
import { getSiteSettings } from "@/lib/db/queries";
|
|
||||||
import { canvasProject } from "@/lib/db/schema";
|
import { canvasProject } from "@/lib/db/schema";
|
||||||
|
import { and, desc, eq } from "drizzle-orm";
|
||||||
|
|
||||||
type Props = { searchParams?: Promise<Record<string, string | string[] | undefined>> };
|
type VideoCanvasRedirectPageProps = {
|
||||||
|
searchParams?: Promise<Record<string, string | string[] | undefined>>;
|
||||||
|
};
|
||||||
|
|
||||||
function first(value: string | string[] | undefined) {
|
function getSearchParam(
|
||||||
|
params: Record<string, string | string[] | undefined>,
|
||||||
|
key: string
|
||||||
|
) {
|
||||||
|
const value = params[key];
|
||||||
return Array.isArray(value) ? value[0] : value;
|
return Array.isArray(value) ? value[0] : value;
|
||||||
}
|
}
|
||||||
|
|
||||||
function canvasHref(id: string, incoming: Record<string, string | string[] | undefined>) {
|
function buildVideoCanvasUrl(
|
||||||
const params = new URLSearchParams({ mode: "video", workspaceType: "video" });
|
canvasId: string,
|
||||||
for (const [key, value] of Object.entries(incoming)) {
|
params: Record<string, string | string[] | undefined>
|
||||||
if (["new", "mode", "workspaceType"].includes(key) || value === undefined) continue;
|
) {
|
||||||
for (const item of Array.isArray(value) ? value : [value]) params.append(key, item);
|
const query = new URLSearchParams();
|
||||||
|
query.set("mode", "video");
|
||||||
|
query.set("workspaceType", "video");
|
||||||
|
|
||||||
|
for (const [key, value] of Object.entries(params)) {
|
||||||
|
if (
|
||||||
|
key === "new" ||
|
||||||
|
key === "mode" ||
|
||||||
|
key === "workspaceType" ||
|
||||||
|
value === undefined
|
||||||
|
) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (Array.isArray(value)) {
|
||||||
|
value.forEach((item) => query.append(key, item));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
query.set(key, value);
|
||||||
}
|
}
|
||||||
return `/canvas/${id}?${params.toString()}`;
|
|
||||||
|
return `/canvas/${canvasId}?${query.toString()}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function projectPreview(thumbnail: string | null, canvasData: unknown) {
|
async function getLatestVideoCanvasId(userId: string) {
|
||||||
const nodes = canvasData && typeof canvasData === "object" && "nodes" in canvasData
|
const [latestProject] = await db
|
||||||
? (canvasData as { nodes?: unknown }).nodes : null;
|
.select({ id: canvasProject.id })
|
||||||
const candidates = [thumbnail];
|
.from(canvasProject)
|
||||||
if (Array.isArray(nodes)) for (const node of [...nodes].reverse()) {
|
.where(
|
||||||
if (!node || typeof node !== "object") continue;
|
and(
|
||||||
const item = node as { inputType?: string; draftValues?: Record<string, unknown> };
|
eq(canvasProject.userId, userId),
|
||||||
const draft = item.draftValues || {};
|
eq(canvasProject.workspaceType, "video")
|
||||||
candidates.push(
|
)
|
||||||
String(draft.poster || draft.previewImageUrl || draft.thumbnailUrl || draft.resultImage || (item.inputType === "image" ? draft.mediaUrl || (Array.isArray(draft.url) ? draft.url[0] : "") : "") || ""),
|
)
|
||||||
);
|
.orderBy(desc(canvasProject.updatedAt))
|
||||||
}
|
.limit(1);
|
||||||
return candidates.find(value => typeof value === "string" && (value.startsWith("https://") || value.startsWith("/"))) || null;
|
|
||||||
|
return latestProject?.id || null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default async function VideoCanvasHomePage({ searchParams }: Props) {
|
export default async function VideoCanvasRedirectPage({
|
||||||
const session = await auth();
|
searchParams,
|
||||||
if (!session?.user?.id) redirect("/login?callbackUrl=%2Fvideo-canvas");
|
}: VideoCanvasRedirectPageProps) {
|
||||||
|
|
||||||
const params = (await searchParams) || {};
|
const params = (await searchParams) || {};
|
||||||
if (first(params.new) === "1") {
|
const shouldCreateNew = getSearchParam(params, "new") === "1";
|
||||||
const [created] = await db.insert(canvasProject).values({
|
|
||||||
userId: session.user.id,
|
if (shouldCreateNew) {
|
||||||
title: "未命名视频画布",
|
redirect(buildVideoCanvasUrl(randomUUID(), params));
|
||||||
workspaceType: "video",
|
|
||||||
canvasData: { nodes: [], edges: [], viewport: { x: 0, y: 0, zoom: 1 } },
|
|
||||||
}).returning({ id: canvasProject.id });
|
|
||||||
redirect(canvasHref(created.id, params));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const [rows, siteSettings] = await Promise.all([db.select({
|
const session = await auth();
|
||||||
id: canvasProject.id,
|
const latestCanvasId = session?.user?.id
|
||||||
title: canvasProject.title,
|
? await getLatestVideoCanvasId(session.user.id).catch(() => null)
|
||||||
thumbnail: canvasProject.thumbnail,
|
: null;
|
||||||
canvasData: canvasProject.canvasData,
|
|
||||||
createdAt: canvasProject.createdAt,
|
|
||||||
updatedAt: canvasProject.updatedAt,
|
|
||||||
}).from(canvasProject).where(and(
|
|
||||||
eq(canvasProject.userId, session.user.id),
|
|
||||||
eq(canvasProject.workspaceType, "video"),
|
|
||||||
)).orderBy(desc(canvasProject.updatedAt)).limit(25), getSiteSettings()]);
|
|
||||||
|
|
||||||
const projects: VideoCanvasHomeProject[] = rows.slice(0, 24).map(row => ({
|
redirect(buildVideoCanvasUrl(latestCanvasId || randomUUID(), params));
|
||||||
id: row.id,
|
|
||||||
title: row.title,
|
|
||||||
thumbnail: projectPreview(row.thumbnail, row.canvasData),
|
|
||||||
createdAt: row.createdAt.toISOString(),
|
|
||||||
updatedAt: row.updatedAt.toISOString(),
|
|
||||||
}));
|
|
||||||
|
|
||||||
return <VideoCanvasHome initialHasMore={rows.length > 24} initialProjects={projects} siteLogo={siteSettings.siteLogo} siteLogoDark={siteSettings.siteLogoDark} siteName={siteSettings.siteName} userName={session.user.name || ""} />;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -33,11 +33,6 @@ const imageGenConfigSchema = z.object({
|
|||||||
supportsNegativePrompt: z.boolean().optional(),
|
supportsNegativePrompt: z.boolean().optional(),
|
||||||
supportsWatermark: z.boolean().optional(),
|
supportsWatermark: z.boolean().optional(),
|
||||||
supportsSeed: z.boolean().optional(),
|
supportsSeed: z.boolean().optional(),
|
||||||
supportsQuality: z.boolean().optional(),
|
|
||||||
supportedAspectRatios: z.array(z.string().min(1).max(32)).max(32).optional(),
|
|
||||||
supportedImageSizes: z.array(z.string().min(1).max(32)).max(32).optional(),
|
|
||||||
maxReferenceImages: z.number().min(0).max(64).optional(),
|
|
||||||
maxVideoReferences: z.number().min(0).max(16).optional(),
|
|
||||||
ruxaTaskInputMode: z
|
ruxaTaskInputMode: z
|
||||||
.enum(["size-image", "resolution-based"])
|
.enum(["size-image", "resolution-based"])
|
||||||
.optional(),
|
.optional(),
|
||||||
@ -50,7 +45,6 @@ const imageGenConfigSchema = z.object({
|
|||||||
creditCostPerVideoByResolution: z.record(z.string(), z.number().min(0).max(100000)).optional(),
|
creditCostPerVideoByResolution: z.record(z.string(), z.number().min(0).max(100000)).optional(),
|
||||||
enableLoadingPlaceholder: z.boolean().optional(),
|
enableLoadingPlaceholder: z.boolean().optional(),
|
||||||
loadingColor: z.string().optional(),
|
loadingColor: z.string().optional(),
|
||||||
vip98k: z.record(z.string(), z.unknown()).optional(),
|
|
||||||
}).optional();
|
}).optional();
|
||||||
|
|
||||||
const capabilitiesSchema = z.record(z.string(), z.unknown()).nullable().optional();
|
const capabilitiesSchema = z.record(z.string(), z.unknown()).nullable().optional();
|
||||||
|
|||||||
@ -9,7 +9,6 @@ import {
|
|||||||
} from "@/lib/db/queries";
|
} from "@/lib/db/queries";
|
||||||
import { ChatSDKError } from "@/lib/errors";
|
import { ChatSDKError } from "@/lib/errors";
|
||||||
import { BLTCY_VIDEO_BASE_URL, isBltcyVideoBaseUrl } from "@/lib/ai/bltcy-video-models";
|
import { BLTCY_VIDEO_BASE_URL, isBltcyVideoBaseUrl } from "@/lib/ai/bltcy-video-models";
|
||||||
import { VIP98K_BASE_URL } from "@/lib/ai/vip98k-models";
|
|
||||||
import { clearScopedSiteSettingsCache } from "@/lib/site-settings/public-cache";
|
import { clearScopedSiteSettingsCache } from "@/lib/site-settings/public-cache";
|
||||||
|
|
||||||
const providerTypes = [
|
const providerTypes = [
|
||||||
@ -34,9 +33,6 @@ const providerTypes = [
|
|||||||
"snapgen-image",
|
"snapgen-image",
|
||||||
"openai-image",
|
"openai-image",
|
||||||
"bananaimage",
|
"bananaimage",
|
||||||
"visionary",
|
|
||||||
"atlascloud",
|
|
||||||
"vip98k",
|
|
||||||
"stepfun-image",
|
"stepfun-image",
|
||||||
"midjourney",
|
"midjourney",
|
||||||
"fal-video",
|
"fal-video",
|
||||||
@ -79,14 +75,11 @@ const RUXA_IMAGE_BASE_URL = "https://api.ruxa.ai";
|
|||||||
const SNAPGEN_IMAGE_BASE_URL = "https://api.snapgen.ai";
|
const SNAPGEN_IMAGE_BASE_URL = "https://api.snapgen.ai";
|
||||||
const OPENAI_IMAGE_BASE_URL = "https://api.openai.com/v1";
|
const OPENAI_IMAGE_BASE_URL = "https://api.openai.com/v1";
|
||||||
const BANANAIMAGE_BASE_URL = "https://bananaimage.aigenmedia.art";
|
const BANANAIMAGE_BASE_URL = "https://bananaimage.aigenmedia.art";
|
||||||
const VISIONARY_BASE_URL = "https://api.visionary.beer";
|
|
||||||
const ATLASCLOUD_BASE_URL = "https://api.atlascloud.ai";
|
|
||||||
const VIP98K_PROVIDER_BASE_URL = VIP98K_BASE_URL;
|
|
||||||
const STEPFUN_IMAGE_BASE_URL = "https://api.stepfun.com";
|
const STEPFUN_IMAGE_BASE_URL = "https://api.stepfun.com";
|
||||||
const BFL_IMAGE_BASE_URL = "https://api.bfl.ai";
|
const BFL_IMAGE_BASE_URL = "https://api.bfl.ai";
|
||||||
const MIDJOURNEY_BASE_URL = "https://your-midjourney-api.example.com";
|
const MIDJOURNEY_BASE_URL = "https://your-midjourney-api.example.com";
|
||||||
const TRIPO_3D_BASE_URL = "https://api.tripo3d.ai/v2/openapi";
|
const TRIPO_3D_BASE_URL = "https://api.tripo3d.ai/v2/openapi";
|
||||||
const WORLDLABS_3D_BASE_URL = "https://api.vip98k.com";
|
const WORLDLABS_3D_BASE_URL = "https://api.worldlabs.ai";
|
||||||
const KITTYCAD_CAD_BASE_URL = "https://api.zoo.dev";
|
const KITTYCAD_CAD_BASE_URL = "https://api.zoo.dev";
|
||||||
const RUNWAY_BASE_URL = "https://api.dev.runwayml.com";
|
const RUNWAY_BASE_URL = "https://api.dev.runwayml.com";
|
||||||
const KREA_BASE_URL = "https://api.krea.ai";
|
const KREA_BASE_URL = "https://api.krea.ai";
|
||||||
@ -248,12 +241,6 @@ export async function POST(request: Request) {
|
|||||||
? data.baseUrl || OPENAI_IMAGE_BASE_URL
|
? data.baseUrl || OPENAI_IMAGE_BASE_URL
|
||||||
: data.type === "bananaimage"
|
: data.type === "bananaimage"
|
||||||
? data.baseUrl || BANANAIMAGE_BASE_URL
|
? data.baseUrl || BANANAIMAGE_BASE_URL
|
||||||
: data.type === "visionary"
|
|
||||||
? data.baseUrl || VISIONARY_BASE_URL
|
|
||||||
: data.type === "atlascloud"
|
|
||||||
? data.baseUrl || ATLASCLOUD_BASE_URL
|
|
||||||
: data.type === "vip98k"
|
|
||||||
? data.baseUrl || VIP98K_PROVIDER_BASE_URL
|
|
||||||
: data.type === "stepfun-image"
|
: data.type === "stepfun-image"
|
||||||
? data.baseUrl || STEPFUN_IMAGE_BASE_URL
|
? data.baseUrl || STEPFUN_IMAGE_BASE_URL
|
||||||
: data.type === "midjourney"
|
: data.type === "midjourney"
|
||||||
@ -316,12 +303,6 @@ export async function PUT(request: Request) {
|
|||||||
...data,
|
...data,
|
||||||
apiKey: normalizedApiKey,
|
apiKey: normalizedApiKey,
|
||||||
type: normalizeProviderTypeForStorage(data.type),
|
type: normalizeProviderTypeForStorage(data.type),
|
||||||
baseUrl:
|
|
||||||
data.baseUrl !== undefined
|
|
||||||
? data.baseUrl
|
|
||||||
: data.type === "vip98k"
|
|
||||||
? VIP98K_PROVIDER_BASE_URL
|
|
||||||
: undefined,
|
|
||||||
updatedBy: session.user.id,
|
updatedBy: session.user.id,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -1,182 +0,0 @@
|
|||||||
import { and, eq } from "drizzle-orm";
|
|
||||||
import { NextResponse } from "next/server";
|
|
||||||
import {
|
|
||||||
fetchAtlasCloudModelPresets,
|
|
||||||
resolveAtlasCloudDisplayName,
|
|
||||||
shouldInsertMissingAtlasCloudPreset,
|
|
||||||
} from "@/lib/ai/atlascloud-models";
|
|
||||||
import { db } from "@/lib/db";
|
|
||||||
import { getAiProviderById } from "@/lib/db/queries";
|
|
||||||
import { type AiModel, aiModel } from "@/lib/db/schema";
|
|
||||||
import { requireAdmin } from "@/lib/middleware/admin";
|
|
||||||
|
|
||||||
export const runtime = "nodejs";
|
|
||||||
export const maxDuration = 300;
|
|
||||||
|
|
||||||
export async function GET() {
|
|
||||||
try {
|
|
||||||
await requireAdmin();
|
|
||||||
const result = await fetchAtlasCloudModelPresets();
|
|
||||||
return NextResponse.json({
|
|
||||||
presets: result.presets,
|
|
||||||
errors: result.errors.length > 0 ? result.errors : undefined,
|
|
||||||
counts: {
|
|
||||||
total: result.catalogCount,
|
|
||||||
available: result.presets.length,
|
|
||||||
failed: result.errors.length,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
console.error("[Atlas Cloud Models API] GET error:", error);
|
|
||||||
return NextResponse.json(
|
|
||||||
{
|
|
||||||
error:
|
|
||||||
error instanceof Error
|
|
||||||
? error.message
|
|
||||||
: "Failed to fetch Atlas Cloud models",
|
|
||||||
},
|
|
||||||
{ status: 500 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function POST(request: Request) {
|
|
||||||
try {
|
|
||||||
await requireAdmin();
|
|
||||||
const body = await request.json();
|
|
||||||
const providerId = String(body?.providerId || "").trim();
|
|
||||||
const requestedModelIds = Array.isArray(body?.modelIds)
|
|
||||||
? new Set(
|
|
||||||
body.modelIds.map((item: unknown) => String(item || "").trim()).filter(Boolean)
|
|
||||||
)
|
|
||||||
: null;
|
|
||||||
if (!providerId) {
|
|
||||||
return NextResponse.json({ error: "Provider ID is required" }, { status: 400 });
|
|
||||||
}
|
|
||||||
|
|
||||||
const provider = await getAiProviderById({ id: providerId });
|
|
||||||
if (!provider || provider.type !== "atlascloud") {
|
|
||||||
return NextResponse.json(
|
|
||||||
{ error: "Provider must be an Atlas Cloud provider" },
|
|
||||||
{ status: 400 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const catalog = await fetchAtlasCloudModelPresets();
|
|
||||||
const presets = requestedModelIds
|
|
||||||
? catalog.presets.filter((preset) => requestedModelIds.has(preset.modelId))
|
|
||||||
: catalog.presets;
|
|
||||||
const addedModels: AiModel[] = [];
|
|
||||||
const updatedModels: AiModel[] = [];
|
|
||||||
const errors = [...catalog.errors];
|
|
||||||
const providerModels = await db.query.aiModel.findMany({
|
|
||||||
where: eq(aiModel.providerId, providerId),
|
|
||||||
});
|
|
||||||
const existingByModelId = new Map(
|
|
||||||
providerModels.map((model) => [model.modelId, model])
|
|
||||||
);
|
|
||||||
const existingMediaKinds = new Set(
|
|
||||||
providerModels.map((model) => {
|
|
||||||
const capabilities = model.capabilities as Record<string, unknown> | null;
|
|
||||||
return String(capabilities?.mediaType || "image");
|
|
||||||
})
|
|
||||||
);
|
|
||||||
let preservedDeletedCount = 0;
|
|
||||||
|
|
||||||
for (const preset of presets) {
|
|
||||||
try {
|
|
||||||
const existing = existingByModelId.get(preset.modelId);
|
|
||||||
const presetCapabilities = preset.capabilities as Record<string, unknown>;
|
|
||||||
const mediaKind = String(presetCapabilities.mediaType || "image");
|
|
||||||
if (
|
|
||||||
!existing &&
|
|
||||||
!shouldInsertMissingAtlasCloudPreset(existingMediaKinds, mediaKind)
|
|
||||||
) {
|
|
||||||
preservedDeletedCount += 1;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
const values = {
|
|
||||||
displayName: resolveAtlasCloudDisplayName(
|
|
||||||
existing?.displayName,
|
|
||||||
preset.displayName,
|
|
||||||
preset.modelId
|
|
||||||
),
|
|
||||||
avatarUrl: preset.avatarUrl || null,
|
|
||||||
description: preset.description,
|
|
||||||
supportsVision: preset.supportsVision,
|
|
||||||
supportsImageGen: preset.supportsImageGen,
|
|
||||||
sortOrder: existing?.sortOrder ?? preset.sortOrder,
|
|
||||||
capabilities: preset.capabilities,
|
|
||||||
defaultParams: preset.defaultParams,
|
|
||||||
imageGenConfig: preset.imageGenConfig,
|
|
||||||
updatedAt: new Date(),
|
|
||||||
};
|
|
||||||
|
|
||||||
if (existing) {
|
|
||||||
const [updated] = await db
|
|
||||||
.update(aiModel)
|
|
||||||
.set(values)
|
|
||||||
.where(
|
|
||||||
and(
|
|
||||||
eq(aiModel.providerId, providerId),
|
|
||||||
eq(aiModel.modelId, preset.modelId)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
.returning();
|
|
||||||
if (updated) updatedModels.push(updated);
|
|
||||||
} else {
|
|
||||||
const [created] = await db
|
|
||||||
.insert(aiModel)
|
|
||||||
.values({
|
|
||||||
providerId,
|
|
||||||
modelId: preset.modelId,
|
|
||||||
...values,
|
|
||||||
isEnabled: true,
|
|
||||||
isHidden: false,
|
|
||||||
isDefault: false,
|
|
||||||
isSubModel: false,
|
|
||||||
parentModelId: null,
|
|
||||||
})
|
|
||||||
.returning();
|
|
||||||
if (created) addedModels.push(created);
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error(
|
|
||||||
`[Atlas Cloud Models API] Failed to sync ${preset.modelId}:`,
|
|
||||||
error
|
|
||||||
);
|
|
||||||
errors.push({
|
|
||||||
modelId: preset.modelId,
|
|
||||||
error: error instanceof Error ? error.message : "Sync failed",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return NextResponse.json({
|
|
||||||
success: true,
|
|
||||||
addedCount: addedModels.length,
|
|
||||||
updatedCount: updatedModels.length,
|
|
||||||
addedModels,
|
|
||||||
updatedModels,
|
|
||||||
errors: errors.length > 0 ? errors : undefined,
|
|
||||||
counts: {
|
|
||||||
requested: presets.length,
|
|
||||||
total: catalog.catalogCount,
|
|
||||||
available: catalog.presets.length,
|
|
||||||
skippedUnsupported: catalog.skipped.length,
|
|
||||||
preservedDeleted: preservedDeletedCount,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
console.error("[Atlas Cloud Models API] POST error:", error);
|
|
||||||
return NextResponse.json(
|
|
||||||
{
|
|
||||||
error:
|
|
||||||
error instanceof Error
|
|
||||||
? error.message
|
|
||||||
: "Failed to sync Atlas Cloud models",
|
|
||||||
},
|
|
||||||
{ status: 500 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -94,8 +94,6 @@ export async function POST(request: Request) {
|
|||||||
providerId,
|
providerId,
|
||||||
modelId: preset.modelId,
|
modelId: preset.modelId,
|
||||||
...values,
|
...values,
|
||||||
isEnabled: true,
|
|
||||||
isHidden: false,
|
|
||||||
isDefault: false,
|
isDefault: false,
|
||||||
isSubModel: false,
|
isSubModel: false,
|
||||||
parentModelId: null,
|
parentModelId: null,
|
||||||
|
|||||||
@ -53,10 +53,6 @@ import {
|
|||||||
CANVAS_IMAGE_TASK_QUEUE_CONFIG_KEY,
|
CANVAS_IMAGE_TASK_QUEUE_CONFIG_KEY,
|
||||||
normalizeCanvasImageTaskQueueConfig,
|
normalizeCanvasImageTaskQueueConfig,
|
||||||
} from "@/lib/canvas/image-task-config";
|
} from "@/lib/canvas/image-task-config";
|
||||||
import {
|
|
||||||
CANVAS_LOCAL_EDIT_CONFIG_KEY,
|
|
||||||
normalizeCanvasLocalEditConfig,
|
|
||||||
} from "@/lib/canvas/local-edit-config";
|
|
||||||
import {
|
import {
|
||||||
DINGTALK_ROBOT_NOTIFICATION_CONFIG_KEY,
|
DINGTALK_ROBOT_NOTIFICATION_CONFIG_KEY,
|
||||||
normalizeDingTalkRobotNotificationConfig,
|
normalizeDingTalkRobotNotificationConfig,
|
||||||
@ -130,12 +126,6 @@ export async function GET(request: Request) {
|
|||||||
setting: setting ? { ...setting, value } : { key, value },
|
setting: setting ? { ...setting, value } : { key, value },
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
if (key === CANVAS_LOCAL_EDIT_CONFIG_KEY) {
|
|
||||||
const value = normalizeCanvasLocalEditConfig(setting?.value);
|
|
||||||
return Response.json({
|
|
||||||
setting: setting ? { ...setting, value } : { key, value },
|
|
||||||
});
|
|
||||||
}
|
|
||||||
if (key === DINGTALK_ROBOT_NOTIFICATION_CONFIG_KEY) {
|
if (key === DINGTALK_ROBOT_NOTIFICATION_CONFIG_KEY) {
|
||||||
const value = normalizeDingTalkRobotNotificationConfig(setting?.value);
|
const value = normalizeDingTalkRobotNotificationConfig(setting?.value);
|
||||||
return Response.json({
|
return Response.json({
|
||||||
@ -379,10 +369,6 @@ export async function POST(request: Request) {
|
|||||||
nextValue = normalizeCanvasImageTaskQueueConfig(value);
|
nextValue = normalizeCanvasImageTaskQueueConfig(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (key === CANVAS_LOCAL_EDIT_CONFIG_KEY) {
|
|
||||||
nextValue = normalizeCanvasLocalEditConfig(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (key === DINGTALK_ROBOT_NOTIFICATION_CONFIG_KEY) {
|
if (key === DINGTALK_ROBOT_NOTIFICATION_CONFIG_KEY) {
|
||||||
nextValue = normalizeDingTalkRobotNotificationConfig(value);
|
nextValue = normalizeDingTalkRobotNotificationConfig(value);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -76,6 +76,8 @@ export async function POST(request: Request) {
|
|||||||
description: preset.description,
|
description: preset.description,
|
||||||
supportsVision: preset.supportsVision,
|
supportsVision: preset.supportsVision,
|
||||||
supportsImageGen: preset.supportsImageGen,
|
supportsImageGen: preset.supportsImageGen,
|
||||||
|
isEnabled: true,
|
||||||
|
isHidden: false,
|
||||||
sortOrder: preset.sortOrder,
|
sortOrder: preset.sortOrder,
|
||||||
capabilities: preset.capabilities || null,
|
capabilities: preset.capabilities || null,
|
||||||
defaultParams: preset.defaultParams || null,
|
defaultParams: preset.defaultParams || null,
|
||||||
|
|||||||
@ -1,213 +0,0 @@
|
|||||||
import { and, eq } from "drizzle-orm";
|
|
||||||
import { NextResponse } from "next/server";
|
|
||||||
import {
|
|
||||||
fetchVip98kModelPresets,
|
|
||||||
type Vip98kModelPreset,
|
|
||||||
} from "@/lib/ai/vip98k-models";
|
|
||||||
import { db } from "@/lib/db";
|
|
||||||
import { getAiProviderById } from "@/lib/db/queries";
|
|
||||||
import { aiModel, type AiModel } from "@/lib/db/schema";
|
|
||||||
import { requireAdmin } from "@/lib/middleware/admin";
|
|
||||||
|
|
||||||
export const runtime = "nodejs";
|
|
||||||
export const maxDuration = 300;
|
|
||||||
|
|
||||||
function summarizeByType(presets: Vip98kModelPreset[]) {
|
|
||||||
return presets.reduce<Record<string, number>>((acc, preset) => {
|
|
||||||
const type = String(preset.type || "model");
|
|
||||||
acc[type] = (acc[type] || 0) + 1;
|
|
||||||
return acc;
|
|
||||||
}, {});
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function GET(request: Request) {
|
|
||||||
try {
|
|
||||||
await requireAdmin();
|
|
||||||
const { searchParams } = new URL(request.url);
|
|
||||||
const providerId = String(searchParams.get("providerId") || "").trim();
|
|
||||||
if (!providerId) {
|
|
||||||
return NextResponse.json(
|
|
||||||
{ error: "Provider ID is required to fetch VIP98K catalog" },
|
|
||||||
{ status: 400 },
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const provider = await getAiProviderById({ id: providerId });
|
|
||||||
if (!provider || provider.type !== "vip98k") {
|
|
||||||
return NextResponse.json(
|
|
||||||
{ error: "Provider must be a VIP98K provider" },
|
|
||||||
{ status: 400 },
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const presets = await fetchVip98kModelPresets({
|
|
||||||
baseURL: provider.baseUrl,
|
|
||||||
apiKey: provider.apiKey,
|
|
||||||
});
|
|
||||||
return NextResponse.json({
|
|
||||||
presets,
|
|
||||||
counts: {
|
|
||||||
total: presets.length,
|
|
||||||
byType: summarizeByType(presets),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
console.error("[VIP98K Models API] GET error:", error);
|
|
||||||
return NextResponse.json(
|
|
||||||
{
|
|
||||||
error:
|
|
||||||
error instanceof Error
|
|
||||||
? error.message
|
|
||||||
: "Failed to fetch VIP98K models",
|
|
||||||
},
|
|
||||||
{ status: 500 },
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function POST(request: Request) {
|
|
||||||
try {
|
|
||||||
await requireAdmin();
|
|
||||||
const body = await request.json();
|
|
||||||
const providerId = String(body?.providerId || "").trim();
|
|
||||||
const requestedModelIds = Array.isArray(body?.modelIds)
|
|
||||||
? new Set(
|
|
||||||
body.modelIds.map((item: unknown) => String(item || "").trim()).filter(Boolean),
|
|
||||||
)
|
|
||||||
: null;
|
|
||||||
const preserveDeletedModels = body?.preserveDeletedModels !== false;
|
|
||||||
if (!providerId) {
|
|
||||||
return NextResponse.json({ error: "Provider ID is required" }, { status: 400 });
|
|
||||||
}
|
|
||||||
|
|
||||||
const provider = await getAiProviderById({ id: providerId });
|
|
||||||
if (!provider || provider.type !== "vip98k") {
|
|
||||||
return NextResponse.json(
|
|
||||||
{ error: "Provider must be a VIP98K provider" },
|
|
||||||
{ status: 400 },
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const catalogPresets = await fetchVip98kModelPresets({
|
|
||||||
baseURL: provider.baseUrl,
|
|
||||||
apiKey: provider.apiKey,
|
|
||||||
});
|
|
||||||
const existingModels = await db.query.aiModel.findMany({
|
|
||||||
where: eq(aiModel.providerId, providerId),
|
|
||||||
});
|
|
||||||
const existingModelIds = new Set(existingModels.map((model) => model.modelId));
|
|
||||||
const preserveExistingCatalog =
|
|
||||||
requestedModelIds === null &&
|
|
||||||
preserveDeletedModels &&
|
|
||||||
existingModels.length > 0;
|
|
||||||
const presets = requestedModelIds
|
|
||||||
? catalogPresets.filter((preset) => requestedModelIds.has(preset.modelId))
|
|
||||||
: preserveExistingCatalog
|
|
||||||
? catalogPresets.filter((preset) => existingModelIds.has(preset.modelId))
|
|
||||||
: catalogPresets;
|
|
||||||
|
|
||||||
const addedModels: AiModel[] = [];
|
|
||||||
const updatedModels: AiModel[] = [];
|
|
||||||
const disabledModels: AiModel[] = [];
|
|
||||||
const errors: Array<{ modelId: string; error: string }> = [];
|
|
||||||
|
|
||||||
if (requestedModelIds === null) {
|
|
||||||
const catalogModelIds = new Set(catalogPresets.map((preset) => preset.modelId));
|
|
||||||
for (const existing of existingModels) {
|
|
||||||
if (catalogModelIds.has(existing.modelId)) continue;
|
|
||||||
const [disabled] = await db
|
|
||||||
.update(aiModel)
|
|
||||||
.set({ isEnabled: false, isHidden: true, updatedAt: new Date() })
|
|
||||||
.where(eq(aiModel.id, existing.id))
|
|
||||||
.returning();
|
|
||||||
if (disabled) disabledModels.push(disabled);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const preset of presets) {
|
|
||||||
try {
|
|
||||||
const existing = await db.query.aiModel.findFirst({
|
|
||||||
where: and(
|
|
||||||
eq(aiModel.providerId, providerId),
|
|
||||||
eq(aiModel.modelId, preset.modelId),
|
|
||||||
),
|
|
||||||
});
|
|
||||||
const values = {
|
|
||||||
displayName: preset.displayName,
|
|
||||||
avatarUrl: null,
|
|
||||||
description: preset.description,
|
|
||||||
supportsVision: preset.supportsVision,
|
|
||||||
supportsImageGen: preset.supportsImageGen,
|
|
||||||
isEnabled:
|
|
||||||
requestedModelIds !== null ? true : (existing?.isEnabled ?? true),
|
|
||||||
isHidden: preset.isHidden,
|
|
||||||
sortOrder: preset.sortOrder,
|
|
||||||
capabilities: preset.capabilities,
|
|
||||||
defaultParams: preset.defaultParams,
|
|
||||||
imageGenConfig: preset.imageGenConfig,
|
|
||||||
updatedAt: new Date(),
|
|
||||||
};
|
|
||||||
|
|
||||||
if (existing) {
|
|
||||||
const [updated] = await db
|
|
||||||
.update(aiModel)
|
|
||||||
.set(values)
|
|
||||||
.where(
|
|
||||||
and(
|
|
||||||
eq(aiModel.providerId, providerId),
|
|
||||||
eq(aiModel.modelId, preset.modelId),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
.returning();
|
|
||||||
if (updated) updatedModels.push(updated);
|
|
||||||
} else {
|
|
||||||
const [created] = await db
|
|
||||||
.insert(aiModel)
|
|
||||||
.values({
|
|
||||||
providerId,
|
|
||||||
modelId: preset.modelId,
|
|
||||||
...values,
|
|
||||||
isDefault: false,
|
|
||||||
isSubModel: false,
|
|
||||||
parentModelId: null,
|
|
||||||
})
|
|
||||||
.returning();
|
|
||||||
if (created) addedModels.push(created);
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error(`[VIP98K Models API] Failed to sync ${preset.modelId}:`, error);
|
|
||||||
errors.push({
|
|
||||||
modelId: preset.modelId,
|
|
||||||
error: error instanceof Error ? error.message : "Sync failed",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return NextResponse.json({
|
|
||||||
success: true,
|
|
||||||
addedCount: addedModels.length,
|
|
||||||
updatedCount: updatedModels.length,
|
|
||||||
disabledCount: disabledModels.length,
|
|
||||||
addedModels,
|
|
||||||
updatedModels,
|
|
||||||
disabledModelIds: disabledModels.map((model) => model.modelId),
|
|
||||||
errors: errors.length > 0 ? errors : undefined,
|
|
||||||
counts: {
|
|
||||||
requested: presets.length,
|
|
||||||
total: catalogPresets.length,
|
|
||||||
byType: summarizeByType(catalogPresets),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
console.error("[VIP98K Models API] POST error:", error);
|
|
||||||
return NextResponse.json(
|
|
||||||
{
|
|
||||||
error:
|
|
||||||
error instanceof Error
|
|
||||||
? error.message
|
|
||||||
: "Failed to sync VIP98K models",
|
|
||||||
},
|
|
||||||
{ status: 500 },
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,139 +0,0 @@
|
|||||||
import { and, eq } from "drizzle-orm";
|
|
||||||
import { NextResponse } from "next/server";
|
|
||||||
import { VISIONARY_MODEL_PRESETS } from "@/lib/ai/visionary-models";
|
|
||||||
import { db } from "@/lib/db";
|
|
||||||
import { getAiProviderById } from "@/lib/db/queries";
|
|
||||||
import { type AiModel, aiModel } from "@/lib/db/schema";
|
|
||||||
import { requireAdmin } from "@/lib/middleware/admin";
|
|
||||||
|
|
||||||
export async function GET() {
|
|
||||||
try {
|
|
||||||
await requireAdmin();
|
|
||||||
return NextResponse.json({
|
|
||||||
presets: VISIONARY_MODEL_PRESETS,
|
|
||||||
counts: { total: VISIONARY_MODEL_PRESETS.length },
|
|
||||||
});
|
|
||||||
} catch (error: any) {
|
|
||||||
console.error("[Visionary Models API] GET error:", error);
|
|
||||||
return NextResponse.json(
|
|
||||||
{ error: error.message || "Failed to fetch Visionary model presets" },
|
|
||||||
{ status: 500 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function POST(request: Request) {
|
|
||||||
try {
|
|
||||||
await requireAdmin();
|
|
||||||
const body = await request.json();
|
|
||||||
const providerId = String(body?.providerId || "").trim();
|
|
||||||
const modelIds = Array.isArray(body?.modelIds)
|
|
||||||
? body.modelIds
|
|
||||||
.map((item: unknown) => String(item || "").trim())
|
|
||||||
.filter(Boolean)
|
|
||||||
: null;
|
|
||||||
|
|
||||||
if (!providerId) {
|
|
||||||
return NextResponse.json(
|
|
||||||
{ error: "Provider ID is required" },
|
|
||||||
{ status: 400 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const provider = await getAiProviderById({ id: providerId });
|
|
||||||
if (!provider || provider.type !== "visionary") {
|
|
||||||
return NextResponse.json(
|
|
||||||
{ error: "Provider must be a Visionary provider" },
|
|
||||||
{ status: 400 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const presets = modelIds
|
|
||||||
? VISIONARY_MODEL_PRESETS.filter((preset) =>
|
|
||||||
modelIds.includes(preset.modelId)
|
|
||||||
)
|
|
||||||
: VISIONARY_MODEL_PRESETS;
|
|
||||||
const addedModels: AiModel[] = [];
|
|
||||||
const updatedModels: AiModel[] = [];
|
|
||||||
const errors: { modelId: string; error: string }[] = [];
|
|
||||||
|
|
||||||
for (const preset of presets) {
|
|
||||||
try {
|
|
||||||
const existing = await db.query.aiModel.findFirst({
|
|
||||||
where: and(
|
|
||||||
eq(aiModel.providerId, providerId),
|
|
||||||
eq(aiModel.modelId, preset.modelId)
|
|
||||||
),
|
|
||||||
});
|
|
||||||
const values = {
|
|
||||||
displayName: preset.displayName,
|
|
||||||
description: preset.description,
|
|
||||||
supportsVision: preset.supportsVision,
|
|
||||||
supportsImageGen: preset.supportsImageGen,
|
|
||||||
isEnabled: true,
|
|
||||||
isHidden: false,
|
|
||||||
sortOrder: preset.sortOrder,
|
|
||||||
capabilities: preset.capabilities || null,
|
|
||||||
defaultParams: preset.defaultParams || null,
|
|
||||||
imageGenConfig: preset.imageGenConfig || null,
|
|
||||||
};
|
|
||||||
|
|
||||||
if (existing) {
|
|
||||||
const [updated] = await db
|
|
||||||
.update(aiModel)
|
|
||||||
.set(values)
|
|
||||||
.where(
|
|
||||||
and(
|
|
||||||
eq(aiModel.providerId, providerId),
|
|
||||||
eq(aiModel.modelId, preset.modelId)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
.returning();
|
|
||||||
if (updated) updatedModels.push(updated);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const [created] = await db
|
|
||||||
.insert(aiModel)
|
|
||||||
.values({
|
|
||||||
providerId,
|
|
||||||
modelId: preset.modelId,
|
|
||||||
...values,
|
|
||||||
isDefault: false,
|
|
||||||
isSubModel: false,
|
|
||||||
parentModelId: null,
|
|
||||||
})
|
|
||||||
.returning();
|
|
||||||
if (created) addedModels.push(created);
|
|
||||||
} catch (error: any) {
|
|
||||||
console.error(
|
|
||||||
`[Visionary Models API] Failed to sync ${preset.modelId}:`,
|
|
||||||
error
|
|
||||||
);
|
|
||||||
errors.push({
|
|
||||||
modelId: preset.modelId,
|
|
||||||
error: error.message || "Sync failed",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return NextResponse.json({
|
|
||||||
success: true,
|
|
||||||
addedCount: addedModels.length,
|
|
||||||
updatedCount: updatedModels.length,
|
|
||||||
addedModels,
|
|
||||||
updatedModels,
|
|
||||||
errors: errors.length > 0 ? errors : undefined,
|
|
||||||
counts: {
|
|
||||||
requested: presets.length,
|
|
||||||
total: VISIONARY_MODEL_PRESETS.length,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
} catch (error: any) {
|
|
||||||
console.error("[Visionary Models API] POST error:", error);
|
|
||||||
return NextResponse.json(
|
|
||||||
{ error: error.message || "Failed to sync Visionary models" },
|
|
||||||
{ status: 500 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,295 +0,0 @@
|
|||||||
import { and, desc, eq, inArray, sql } from "drizzle-orm";
|
|
||||||
import { NextResponse } from "next/server";
|
|
||||||
import { auth } from "@/app/(auth)/auth";
|
|
||||||
import { db } from "@/lib/db";
|
|
||||||
import { canvasImageTask, canvasProject, message } from "@/lib/db/schema";
|
|
||||||
import { extractAgentProjectStateFromCanvasData, isAgentProjectRow } from "@/lib/agents/agent-project-compat";
|
|
||||||
import {
|
|
||||||
createCanvasImageTask,
|
|
||||||
enqueueCanvasImageTask,
|
|
||||||
ensureCanvasImageTaskActive,
|
|
||||||
getCanvasImageTaskForUser,
|
|
||||||
} from "@/lib/canvas/image-task-queue";
|
|
||||||
import {
|
|
||||||
checkUserImageQuota,
|
|
||||||
getChatById,
|
|
||||||
getEnabledAiModelsWithProvider,
|
|
||||||
getPointsSystemConfig,
|
|
||||||
getUserById,
|
|
||||||
saveChat,
|
|
||||||
} from "@/lib/db/queries";
|
|
||||||
import { getModelGenerationCreditCost } from "@/lib/ai/image-model-pricing";
|
|
||||||
import { resolveDirectReplacementModelRef } from "@/app/(chat)/api/chat/_lib/detail-replacement";
|
|
||||||
import {
|
|
||||||
buildAgentSlotPrompt,
|
|
||||||
normalizeAgentSupplementPrompt,
|
|
||||||
} from "@/app/(chat)/api/chat/_lib/agent-scene-workflow";
|
|
||||||
import {
|
|
||||||
detailReplacementSubmissionKey,
|
|
||||||
isDetailReplacementBatchRequest,
|
|
||||||
isDetailReplacementUuid,
|
|
||||||
type DetailReplacementBatchReference,
|
|
||||||
type DetailReplacementBatchRequest,
|
|
||||||
type DetailReplacementBatchResult,
|
|
||||||
} from "@/lib/agents/detail-replacement-batch";
|
|
||||||
|
|
||||||
export const maxDuration = 1200;
|
|
||||||
|
|
||||||
function scopedChatId(projectId: string) {
|
|
||||||
const normalized = projectId.toLowerCase().replace(/-/g, "");
|
|
||||||
const scoped = `${normalized.slice(0, 20)}a93e17a60001`;
|
|
||||||
return [scoped.slice(0, 8), scoped.slice(8, 12), scoped.slice(12, 16), scoped.slice(16, 20), scoped.slice(20, 32)].join("-");
|
|
||||||
}
|
|
||||||
|
|
||||||
function taskResponse(task: Awaited<ReturnType<typeof getCanvasImageTaskForUser>>) {
|
|
||||||
const result = task?.result as DetailReplacementBatchResult | null;
|
|
||||||
const request = task?.request as DetailReplacementBatchRequest | null;
|
|
||||||
return {
|
|
||||||
id: task?.id,
|
|
||||||
status: task?.status,
|
|
||||||
attempts: task?.attempts,
|
|
||||||
error: task?.error || null,
|
|
||||||
cards: Array.isArray(result?.cards) ? result.cards : [],
|
|
||||||
currentCardId: result?.currentCardId || null,
|
|
||||||
requestedCardIds: request?.references?.map((item) => item.id) || [],
|
|
||||||
createdAt: task?.createdAt || null,
|
|
||||||
queueStatus: task?.queueStatus || null,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
async function findActiveBatchId(params: {
|
|
||||||
userId: string;
|
|
||||||
projectId: string;
|
|
||||||
submissionKey: string;
|
|
||||||
}) {
|
|
||||||
const [row] = await db.select({ id: canvasImageTask.id })
|
|
||||||
.from(canvasImageTask)
|
|
||||||
.where(and(
|
|
||||||
eq(canvasImageTask.userId, params.userId),
|
|
||||||
inArray(canvasImageTask.status, ["pending", "running"]),
|
|
||||||
sql`${canvasImageTask.request}->>'taskKind' = 'agent-detail-replacement-batch'`,
|
|
||||||
sql`${canvasImageTask.request}->>'projectId' = ${params.projectId}`,
|
|
||||||
sql`${canvasImageTask.request}->>'submissionKey' = ${params.submissionKey}`,
|
|
||||||
))
|
|
||||||
.orderBy(desc(canvasImageTask.createdAt)).limit(1);
|
|
||||||
return row?.id || null;
|
|
||||||
}
|
|
||||||
|
|
||||||
function isUniqueViolation(error: unknown) {
|
|
||||||
for (let cursor: unknown = error; cursor && typeof cursor === "object"; ) {
|
|
||||||
if ((cursor as { code?: unknown }).code === "23505") return true;
|
|
||||||
cursor = (cursor as { cause?: unknown }).cause;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function GET(request: Request) {
|
|
||||||
const session = await auth();
|
|
||||||
if (!session?.user?.id) return NextResponse.json({ error: "未登录" }, { status: 401 });
|
|
||||||
const params = new URL(request.url).searchParams;
|
|
||||||
const projectId = String(params.get("projectId") || "").trim();
|
|
||||||
const taskId = String(params.get("taskId") || "").trim();
|
|
||||||
if (!isDetailReplacementUuid(projectId)) {
|
|
||||||
return NextResponse.json({ error: "项目编号无效" }, { status: 400 });
|
|
||||||
}
|
|
||||||
const [project] = await db.select({ id: canvasProject.id }).from(canvasProject).where(and(
|
|
||||||
eq(canvasProject.id, projectId), eq(canvasProject.userId, session.user.id),
|
|
||||||
)).limit(1);
|
|
||||||
if (!project) return NextResponse.json({ error: "项目不存在" }, { status: 404 });
|
|
||||||
|
|
||||||
const [candidate] = await db.select({ id: canvasImageTask.id, request: canvasImageTask.request })
|
|
||||||
.from(canvasImageTask)
|
|
||||||
.where(and(
|
|
||||||
eq(canvasImageTask.userId, session.user.id),
|
|
||||||
...(isDetailReplacementUuid(taskId) ? [eq(canvasImageTask.id, taskId)] : []),
|
|
||||||
sql`${canvasImageTask.request}->>'taskKind' = 'agent-detail-replacement-batch'`,
|
|
||||||
sql`${canvasImageTask.request}->>'projectId' = ${projectId}`,
|
|
||||||
))
|
|
||||||
// A batch that is still working matters more than a newer finished one, so the
|
|
||||||
// workspace keeps showing live progress after a rerun of a single card.
|
|
||||||
.orderBy(
|
|
||||||
sql`case when ${canvasImageTask.status} in ('pending', 'running') then 0 else 1 end`,
|
|
||||||
desc(canvasImageTask.createdAt),
|
|
||||||
).limit(1);
|
|
||||||
if (!candidate || !isDetailReplacementBatchRequest(candidate.request)) {
|
|
||||||
return NextResponse.json({ task: null });
|
|
||||||
}
|
|
||||||
let task = await getCanvasImageTaskForUser({ taskId: candidate.id, userId: session.user.id });
|
|
||||||
if (task && (task.status === "failed" ||
|
|
||||||
(task.status === "running" && Date.now() - task.updatedAt.getTime() > 90_000))) {
|
|
||||||
await ensureCanvasImageTaskActive({ taskId: task.id, userId: session.user.id });
|
|
||||||
task = await getCanvasImageTaskForUser({ taskId: candidate.id, userId: session.user.id });
|
|
||||||
}
|
|
||||||
return NextResponse.json({ task: taskResponse(task) });
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function POST(request: Request) {
|
|
||||||
try {
|
|
||||||
const session = await auth();
|
|
||||||
if (!session?.user?.id) return NextResponse.json({ error: "未登录" }, { status: 401 });
|
|
||||||
const body = await request.json();
|
|
||||||
const projectId = String(body.projectId || "").trim();
|
|
||||||
const taskId = String(body.requestId || "").trim();
|
|
||||||
if (!isDetailReplacementUuid(projectId) || !isDetailReplacementUuid(taskId)) {
|
|
||||||
return NextResponse.json({ error: "任务编号或项目编号无效" }, { status: 400 });
|
|
||||||
}
|
|
||||||
const [project] = await db.select().from(canvasProject).where(and(
|
|
||||||
eq(canvasProject.id, projectId), eq(canvasProject.userId, session.user.id),
|
|
||||||
)).limit(1);
|
|
||||||
if (!project || !isAgentProjectRow(project)) {
|
|
||||||
return NextResponse.json({ error: "风格化项目不存在" }, { status: 404 });
|
|
||||||
}
|
|
||||||
|
|
||||||
const state = extractAgentProjectStateFromCanvasData(project.canvasData);
|
|
||||||
const knownReferences = new Map(
|
|
||||||
(state.workflowState.agentModeState?.detailReplacement?.references || [])
|
|
||||||
.map((item) => [String(item.id), String(item.imageUrl)]),
|
|
||||||
);
|
|
||||||
const requested = Array.isArray(body.references) ? body.references : [];
|
|
||||||
const references: DetailReplacementBatchReference[] = requested.map((raw: unknown) => {
|
|
||||||
const item = raw && typeof raw === "object" ? raw as Record<string, unknown> : {};
|
|
||||||
return {
|
|
||||||
id: String(item.id || "").slice(0, 128),
|
|
||||||
label: String(item.label || "详情参考图").slice(0, 255),
|
|
||||||
imageUrl: String(item.imageUrl || "").trim(),
|
|
||||||
candidateId: String(item.candidateId || "").slice(0, 128) || null,
|
|
||||||
passthrough: item.passthrough === true,
|
|
||||||
rerunReferenceImages: Array.isArray(item.rerunReferenceImages)
|
|
||||||
? item.rerunReferenceImages.slice(0, 6).map((entry: { url?: string }) => ({ url: String(entry?.url || "") }))
|
|
||||||
: [],
|
|
||||||
};
|
|
||||||
});
|
|
||||||
if (
|
|
||||||
references.length === 0 || references.length > 40 ||
|
|
||||||
new Set(references.map((item) => item.id)).size !== references.length ||
|
|
||||||
references.some((item) => !item.id || knownReferences.get(item.id) !== item.imageUrl)
|
|
||||||
) {
|
|
||||||
return NextResponse.json({ error: "详情参考图尚未保存或已发生变化,请刷新后重试" }, { status: 400 });
|
|
||||||
}
|
|
||||||
const productImageUrls = Array.isArray(body.productImageUrls)
|
|
||||||
? Array.from(new Set(body.productImageUrls.map((url: unknown) => String(url || "").trim())
|
|
||||||
.filter((url: string) => /^https?:\/\//i.test(url)))).slice(0, 16) as string[]
|
|
||||||
: [];
|
|
||||||
if (productImageUrls.length === 0 && !references.every((item) => item.passthrough)) {
|
|
||||||
return NextResponse.json({ error: "请先在输入图区选择至少一张产品图" }, { status: 400 });
|
|
||||||
}
|
|
||||||
const strategy = body.replacementStrategy === "product_only" ? "product_only" : "scene_replace";
|
|
||||||
const prompt = String(body.prompt || "").trim().slice(0, 24_000);
|
|
||||||
if (strategy !== "product_only" && !prompt) {
|
|
||||||
return NextResponse.json({ error: "请先配置风格化提示词" }, { status: 400 });
|
|
||||||
}
|
|
||||||
const models = await getEnabledAiModelsWithProvider();
|
|
||||||
const model = await resolveDirectReplacementModelRef({
|
|
||||||
selectedChatModel: String(body.model || "auto"),
|
|
||||||
finalModelId: String(body.model || "auto"),
|
|
||||||
models,
|
|
||||||
});
|
|
||||||
if (!model || model === "auto" || !models.some((entry) => entry.model.id === model)) {
|
|
||||||
return NextResponse.json({ error: "详情替换模型不可用,请检查模型配置" }, { status: 400 });
|
|
||||||
}
|
|
||||||
const quota = await checkUserImageQuota(session.user.id);
|
|
||||||
if (!quota.allowed) {
|
|
||||||
return NextResponse.json({ error: "本月图片生成配额已用完" }, { status: 429 });
|
|
||||||
}
|
|
||||||
const requestedImageCount = references.filter((item) => !item.passthrough).length;
|
|
||||||
if (quota.limit != null && quota.used + requestedImageCount > quota.limit) {
|
|
||||||
return NextResponse.json({ error: `本月剩余生图配额不足 ${requestedImageCount} 张` }, { status: 429 });
|
|
||||||
}
|
|
||||||
const pointsConfig = await getPointsSystemConfig();
|
|
||||||
if (pointsConfig.enabled && pointsConfig.blockOnInsufficient !== false) {
|
|
||||||
const selectedModel = models.find((entry) => entry.model.id === model)?.model;
|
|
||||||
const unitCost = getModelGenerationCreditCost(
|
|
||||||
selectedModel as Parameters<typeof getModelGenerationCreditCost>[0], "image", {
|
|
||||||
imageSize: ["1K", "2K", "4K"].includes(body.imageSize) ? body.imageSize : "2K",
|
|
||||||
});
|
|
||||||
const user = await getUserById({ id: session.user.id });
|
|
||||||
const expectedCost = unitCost * requestedImageCount;
|
|
||||||
if (!user?.isHighVip && expectedCost > Number(user?.pointsBalance || 0)) {
|
|
||||||
return NextResponse.json({ error: `积分不足,本批次预计消耗 ${expectedCost} 积分` }, { status: 402 });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
const chatId = scopedChatId(projectId);
|
|
||||||
const existingChat = await getChatById({ id: chatId });
|
|
||||||
if (existingChat && existingChat.userId !== session.user.id) {
|
|
||||||
return NextResponse.json({ error: "无权访问项目对话" }, { status: 403 });
|
|
||||||
}
|
|
||||||
if (!existingChat) await saveChat({
|
|
||||||
id: chatId, userId: session.user.id, title: project.title || "详情替换", visibility: "private",
|
|
||||||
});
|
|
||||||
const taskRequest: DetailReplacementBatchRequest = {
|
|
||||||
taskKind: "agent-detail-replacement-batch",
|
|
||||||
projectId,
|
|
||||||
chatId,
|
|
||||||
model,
|
|
||||||
prompt: strategy === "product_only"
|
|
||||||
? "仅替换参考详情图中的产品主体"
|
|
||||||
: buildAgentSlotPrompt({
|
|
||||||
workflowPrompt: prompt,
|
|
||||||
slotPrompt: "",
|
|
||||||
userSupplementPrompt: normalizeAgentSupplementPrompt(
|
|
||||||
String(body.userPrompt || ""), "详情参考图替换",
|
|
||||||
),
|
|
||||||
includeSlotTitle: false,
|
|
||||||
modeType: "detail_replacement",
|
|
||||||
}),
|
|
||||||
userPrompt: String(body.userPrompt || "").slice(0, 12_000),
|
|
||||||
imageSize: ["1K", "2K", "4K"].includes(body.imageSize) ? body.imageSize : "2K",
|
|
||||||
replacementStrategy: strategy,
|
|
||||||
productImageUrls,
|
|
||||||
modelImageUrl: /^https?:\/\//i.test(String(body.modelImageUrl || ""))
|
|
||||||
? String(body.modelImageUrl) : null,
|
|
||||||
references,
|
|
||||||
};
|
|
||||||
const submissionKey = detailReplacementSubmissionKey(taskRequest);
|
|
||||||
taskRequest.submissionKey = submissionKey;
|
|
||||||
|
|
||||||
const duplicateId = await findActiveBatchId({
|
|
||||||
userId: session.user.id, projectId, submissionKey,
|
|
||||||
});
|
|
||||||
if (duplicateId) {
|
|
||||||
await ensureCanvasImageTaskActive({ taskId: duplicateId, userId: session.user.id });
|
|
||||||
const duplicate = await getCanvasImageTaskForUser({ taskId: duplicateId, userId: session.user.id });
|
|
||||||
return NextResponse.json({ task: taskResponse(duplicate) });
|
|
||||||
}
|
|
||||||
|
|
||||||
let task: Awaited<ReturnType<typeof createCanvasImageTask>>;
|
|
||||||
try {
|
|
||||||
task = await createCanvasImageTask({
|
|
||||||
taskId, userId: session.user.id, request: taskRequest as unknown as Record<string, unknown>,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
if (!isUniqueViolation(error)) throw error;
|
|
||||||
// Two identical submissions raced past the lookup above; the database
|
|
||||||
// unique index kept one of them, so answer with that batch.
|
|
||||||
const racedId = await findActiveBatchId({
|
|
||||||
userId: session.user.id, projectId, submissionKey,
|
|
||||||
});
|
|
||||||
if (!racedId) throw error;
|
|
||||||
const raced = await getCanvasImageTaskForUser({ taskId: racedId, userId: session.user.id });
|
|
||||||
return NextResponse.json({ task: taskResponse(raced) });
|
|
||||||
}
|
|
||||||
const existingTask = await getCanvasImageTaskForUser({ taskId: task.id, userId: session.user.id });
|
|
||||||
const storedRequest = existingTask?.request as DetailReplacementBatchRequest | null;
|
|
||||||
if (
|
|
||||||
!isDetailReplacementBatchRequest(storedRequest) ||
|
|
||||||
storedRequest.projectId !== projectId ||
|
|
||||||
(storedRequest.submissionKey && storedRequest.submissionKey !== submissionKey)
|
|
||||||
) {
|
|
||||||
return NextResponse.json({ error: "任务编号已被其他请求使用" }, { status: 409 });
|
|
||||||
}
|
|
||||||
await db.insert(message).values({
|
|
||||||
id: taskId,
|
|
||||||
chatId,
|
|
||||||
role: "user",
|
|
||||||
parts: [{ type: "text", text: taskRequest.userPrompt || `批量替换 ${references.length} 张详情图` }],
|
|
||||||
attachments: [],
|
|
||||||
createdAt: new Date(),
|
|
||||||
}).onConflictDoNothing({ target: message.id });
|
|
||||||
await enqueueCanvasImageTask(task.id);
|
|
||||||
const current = await getCanvasImageTaskForUser({ taskId, userId: session.user.id });
|
|
||||||
return NextResponse.json({ task: taskResponse(current) });
|
|
||||||
} catch (error) {
|
|
||||||
console.error("[Detail Replacement Batch] Failed to start", error);
|
|
||||||
return NextResponse.json({ error: error instanceof Error ? error.message : "提交详情替换任务失败" }, { status: 500 });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -13,7 +13,6 @@ import {
|
|||||||
findStyleProImageModelByRef,
|
findStyleProImageModelByRef,
|
||||||
getStyleProImageModelEntries,
|
getStyleProImageModelEntries,
|
||||||
} from "@/lib/style-pro/model-options";
|
} from "@/lib/style-pro/model-options";
|
||||||
import { getAiModelFrontendAvailability } from "@/lib/ai/model-frontend-availability";
|
|
||||||
|
|
||||||
const requestSchema = z.object({
|
const requestSchema = z.object({
|
||||||
prompt: z.string().min(1).max(5000),
|
prompt: z.string().min(1).max(5000),
|
||||||
@ -53,16 +52,6 @@ export async function POST(request: Request) {
|
|||||||
{ status: 400 }
|
{ status: 400 }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
const availability = getAiModelFrontendAvailability(selectedModel.model);
|
|
||||||
if (availability.selectable === false) {
|
|
||||||
return NextResponse.json(
|
|
||||||
{
|
|
||||||
success: false,
|
|
||||||
error: availability.disabledReason || "所选 AI洗图 生图模型当前不可用",
|
|
||||||
},
|
|
||||||
{ status: 400 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const imageSize = body.imageSize?.trim() || "2K";
|
const imageSize = body.imageSize?.trim() || "2K";
|
||||||
const unitCost = getModelGenerationCreditCost(selectedModel.model as any, "image", {
|
const unitCost = getModelGenerationCreditCost(selectedModel.model as any, "image", {
|
||||||
|
|||||||
@ -1,10 +1,385 @@
|
|||||||
import { NextResponse } from "next/server";
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
|
import {
|
||||||
|
createMediaAssetRelation,
|
||||||
|
getMediaAssetsByUserAndUrls,
|
||||||
|
getPointsSystemConfig,
|
||||||
|
getSystemSetting,
|
||||||
|
upsertMediaAsset,
|
||||||
|
} from "@/lib/db/queries";
|
||||||
import { auth } from "@/app/(auth)/auth";
|
import { auth } from "@/app/(auth)/auth";
|
||||||
import { executeAngleEditPOST } from "@/lib/canvas/operations/angle-edit";
|
import { uploadFile } from "@/lib/storage/service";
|
||||||
export const runtime = "nodejs";
|
import { db } from "@/lib/db";
|
||||||
|
import { generatedImage } from "@/lib/db/schema";
|
||||||
|
import { createLogger } from "@/lib/observability/logger";
|
||||||
|
import { getImageToolCreditCost } from "@/lib/points/config";
|
||||||
|
import { prepareFeaturePointsCharge } from "@/lib/points/feature-charge";
|
||||||
|
|
||||||
|
const logger = createLogger("angle-edit-api");
|
||||||
|
|
||||||
|
// 角度编辑API配置类型
|
||||||
|
type AngleEditConfig = {
|
||||||
|
enabled?: boolean;
|
||||||
|
falApiKey?: string;
|
||||||
|
loraScale?: number;
|
||||||
|
inferenceSteps?: number;
|
||||||
|
guidanceScale?: number;
|
||||||
|
maxImageSize?: number;
|
||||||
|
timeout?: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
// 设置API路由最大执行时间为3分钟(180秒)
|
||||||
export const maxDuration = 180;
|
export const maxDuration = 180;
|
||||||
export async function POST(request: Request) {
|
|
||||||
const session = await auth();
|
/**
|
||||||
if (!session?.user?.id) return NextResponse.json({ error: "未登录" }, { status: 401 });
|
* 压缩图片生成缩略图
|
||||||
return executeAngleEditPOST(request, session.user.id);
|
*/
|
||||||
|
async function createThumbnail(buffer: Buffer): Promise<Buffer> {
|
||||||
|
try {
|
||||||
|
const sharp = await import("sharp");
|
||||||
|
|
||||||
|
const thumbnailBuffer = await sharp.default(buffer)
|
||||||
|
.resize(500, null, {
|
||||||
|
fit: "inside",
|
||||||
|
withoutEnlargement: true,
|
||||||
|
})
|
||||||
|
.jpeg({ quality: 80 })
|
||||||
|
.toBuffer();
|
||||||
|
|
||||||
|
logger.debug("thumbnail_created", {
|
||||||
|
originalBytes: buffer.length,
|
||||||
|
thumbnailBytes: thumbnailBuffer.length,
|
||||||
|
});
|
||||||
|
return thumbnailBuffer;
|
||||||
|
} catch (error) {
|
||||||
|
logger.warn("thumbnail_creation_failed", error);
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将 base64 图片上传到 S3
|
||||||
|
*/
|
||||||
|
async function uploadBase64ToS3(base64Data: string, prefix: string = "angle-edit"): Promise<{ thumbnail: string; original: string }> {
|
||||||
|
try {
|
||||||
|
const buffer = Buffer.from(base64Data, "base64");
|
||||||
|
const timestamp = Date.now();
|
||||||
|
const randomStr = Math.random().toString(36).slice(2, 8);
|
||||||
|
const thumbnailFilename = `${prefix}-thumb-${timestamp}-${randomStr}.png`;
|
||||||
|
const originalFilename = `${prefix}-orig-${timestamp}-${randomStr}.png`;
|
||||||
|
|
||||||
|
logger.debug("source_image_prepared", { originalBytes: buffer.length });
|
||||||
|
|
||||||
|
const thumbnailBuffer = await createThumbnail(buffer);
|
||||||
|
|
||||||
|
const thumbnailArrayBuffer = thumbnailBuffer.buffer.slice(
|
||||||
|
thumbnailBuffer.byteOffset,
|
||||||
|
thumbnailBuffer.byteOffset + thumbnailBuffer.byteLength
|
||||||
|
) as ArrayBuffer;
|
||||||
|
const originalArrayBuffer = buffer.buffer.slice(
|
||||||
|
buffer.byteOffset,
|
||||||
|
buffer.byteOffset + buffer.byteLength
|
||||||
|
) as ArrayBuffer;
|
||||||
|
|
||||||
|
logger.debug("storage_upload_started", {
|
||||||
|
hasOriginal: originalArrayBuffer.byteLength > 0,
|
||||||
|
hasThumbnail: thumbnailArrayBuffer.byteLength > 0,
|
||||||
|
prefix,
|
||||||
|
});
|
||||||
|
const [thumbnailResult, originalResult] = await Promise.all([
|
||||||
|
uploadFile(thumbnailFilename, thumbnailArrayBuffer),
|
||||||
|
uploadFile(originalFilename, originalArrayBuffer),
|
||||||
|
]);
|
||||||
|
|
||||||
|
logger.info("storage_upload_completed", {
|
||||||
|
originalUrl: originalResult.url,
|
||||||
|
thumbnailUrl: thumbnailResult.url,
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
thumbnail: thumbnailResult.url,
|
||||||
|
original: originalResult.url,
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
logger.error("storage_upload_failed", error);
|
||||||
|
throw new Error(`图片上传失败: ${error instanceof Error ? error.message : "未知错误"}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function POST(request: NextRequest) {
|
||||||
|
const startTime = Date.now();
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 1. 验证用户登录
|
||||||
|
const session = await auth();
|
||||||
|
if (!session?.user?.id) {
|
||||||
|
return NextResponse.json({ error: "未登录" }, { status: 401 });
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info("request_received", { authenticated: true });
|
||||||
|
|
||||||
|
// 2. 从数据库获取配置
|
||||||
|
const angleEditConfig = await getSystemSetting({ key: "angle_edit_config" });
|
||||||
|
const config = (angleEditConfig?.value as AngleEditConfig) || {};
|
||||||
|
|
||||||
|
// 验证功能是否启用
|
||||||
|
if (!config.enabled) {
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: "角度编辑功能未启用,请联系管理员在系统设置中开启" },
|
||||||
|
{ status: 400 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 验证 API Key
|
||||||
|
if (!config.falApiKey) {
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: "fal.ai API Key 未配置,请联系管理员配置" },
|
||||||
|
{ status: 500 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.debug("config_loaded", {
|
||||||
|
enabled: Boolean(config.enabled),
|
||||||
|
hasApiKey: Boolean(config.falApiKey),
|
||||||
|
loraScale: config.loraScale || 1,
|
||||||
|
});
|
||||||
|
|
||||||
|
// 3. 解析请求
|
||||||
|
const body = await request.json();
|
||||||
|
const {
|
||||||
|
image_url,
|
||||||
|
image_base64,
|
||||||
|
// 数值参数 (官方 API 格式)
|
||||||
|
horizontal_angle, // 0-360 度
|
||||||
|
vertical_angle, // -30 到 90 度
|
||||||
|
zoom, // 0-10
|
||||||
|
lora_scale,
|
||||||
|
} = body;
|
||||||
|
|
||||||
|
if (!image_url && !image_base64) {
|
||||||
|
return NextResponse.json({ error: "缺少图片数据" }, { status: 400 });
|
||||||
|
}
|
||||||
|
|
||||||
|
// 使用请求中的参数或配置中的默认值
|
||||||
|
const finalLoraScale = lora_scale ?? config.loraScale ?? 1;
|
||||||
|
const finalHorizontalAngle = horizontal_angle ?? 0;
|
||||||
|
const finalVerticalAngle = vertical_angle ?? 0;
|
||||||
|
const finalZoom = zoom ?? 5;
|
||||||
|
|
||||||
|
logger.info("request_parameters_normalized", {
|
||||||
|
hasBase64Input: Boolean(image_base64),
|
||||||
|
hasUrlInput: Boolean(image_url),
|
||||||
|
horizontalAngle: finalHorizontalAngle,
|
||||||
|
loraScale: finalLoraScale,
|
||||||
|
verticalAngle: finalVerticalAngle,
|
||||||
|
zoom: finalZoom,
|
||||||
|
});
|
||||||
|
|
||||||
|
const pointsConfig = await getPointsSystemConfig();
|
||||||
|
const pointsCharge = await prepareFeaturePointsCharge({
|
||||||
|
userId: session.user.id,
|
||||||
|
unitCost: getImageToolCreditCost(pointsConfig, "angle-edit"),
|
||||||
|
reason: "image_angle_edit",
|
||||||
|
modelId: "qwen-image-edit-2511-multiple-angles",
|
||||||
|
note: "多角度编辑消耗积分",
|
||||||
|
pointsConfig,
|
||||||
|
});
|
||||||
|
if (!pointsCharge.ok) {
|
||||||
|
return NextResponse.json(pointsCharge.payload, { status: pointsCharge.status });
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. 准备图片输入
|
||||||
|
let imageInput: string;
|
||||||
|
if (image_url) {
|
||||||
|
imageInput = image_url;
|
||||||
|
} else {
|
||||||
|
const mimeType = image_base64.startsWith("/9j/") ? "image/jpeg" : "image/png";
|
||||||
|
imageInput = `data:${mimeType};base64,${image_base64}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 5. 动态导入 fal.ai SDK 并调用官方多角度 API
|
||||||
|
logger.info("provider_call_started", {
|
||||||
|
modelId: "fal-ai/qwen-image-edit-2511-multiple-angles",
|
||||||
|
});
|
||||||
|
|
||||||
|
const fal = await import("@fal-ai/serverless-client");
|
||||||
|
|
||||||
|
fal.config({
|
||||||
|
credentials: config.falApiKey,
|
||||||
|
});
|
||||||
|
|
||||||
|
// 使用官方多角度 API 端点
|
||||||
|
// 文档: https://fal.ai/models/fal-ai/qwen-image-edit-2511-multiple-angles
|
||||||
|
const result = await fal.subscribe("fal-ai/qwen-image-edit-2511-multiple-angles", {
|
||||||
|
input: {
|
||||||
|
image_urls: [imageInput], // 官方使用 image_urls (数组)
|
||||||
|
horizontal_angle: finalHorizontalAngle, // 水平旋转角度 0-360
|
||||||
|
vertical_angle: finalVerticalAngle, // 垂直角度 -30 到 90
|
||||||
|
zoom: finalZoom, // 缩放 0-10
|
||||||
|
lora_scale: finalLoraScale, // LoRA 强度
|
||||||
|
num_inference_steps: config.inferenceSteps || 28,
|
||||||
|
guidance_scale: config.guidanceScale || 4.5, // 官方默认 4.5
|
||||||
|
enable_safety_checker: true,
|
||||||
|
output_format: "png",
|
||||||
|
},
|
||||||
|
logs: true,
|
||||||
|
onQueueUpdate: (update) => {
|
||||||
|
if (update.status === "IN_PROGRESS") {
|
||||||
|
logger.debug("provider_progress", { status: update.status });
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// 6. 处理结果
|
||||||
|
const apiResult = result as { images?: Array<{ url?: string }>; prompt?: string };
|
||||||
|
const outputImage = apiResult.images?.[0];
|
||||||
|
if (!outputImage?.url) {
|
||||||
|
throw new Error("API 未返回有效图片");
|
||||||
|
}
|
||||||
|
|
||||||
|
const outputImageUrl = outputImage.url;
|
||||||
|
logger.info("provider_call_completed", {
|
||||||
|
hasGeneratedPrompt: Boolean(apiResult.prompt),
|
||||||
|
imageCount: apiResult.images?.length ?? 0,
|
||||||
|
outputImageUrl,
|
||||||
|
});
|
||||||
|
|
||||||
|
// 7. 下载图片并上传到我们的 S3
|
||||||
|
const imageResponse = await fetch(outputImageUrl);
|
||||||
|
if (!imageResponse.ok) {
|
||||||
|
throw new Error(`下载输出图片失败: ${imageResponse.status}`);
|
||||||
|
}
|
||||||
|
const imageBuffer = Buffer.from(await imageResponse.arrayBuffer());
|
||||||
|
const imageBase64Result = imageBuffer.toString("base64");
|
||||||
|
|
||||||
|
const s3Result = await uploadBase64ToS3(imageBase64Result, "angle-edit");
|
||||||
|
logger.info("result_image_persisted", {
|
||||||
|
originalUrl: s3Result.original,
|
||||||
|
thumbnailUrl: s3Result.thumbnail,
|
||||||
|
});
|
||||||
|
|
||||||
|
// 8. 保存到数据库
|
||||||
|
const processingTime = (Date.now() - startTime) / 1000;
|
||||||
|
|
||||||
|
const [savedImage] = await db
|
||||||
|
.insert(generatedImage)
|
||||||
|
.values({
|
||||||
|
userId: session.user.id,
|
||||||
|
thumbnailUrl: s3Result.thumbnail,
|
||||||
|
originalUrl: s3Result.original,
|
||||||
|
prompt: `角度编辑: H${finalHorizontalAngle}° V${finalVerticalAngle}° Z${finalZoom}`,
|
||||||
|
modelId: "qwen-image-edit-2511-multiple-angles",
|
||||||
|
createdAt: new Date(),
|
||||||
|
})
|
||||||
|
.returning();
|
||||||
|
|
||||||
|
if (pointsCharge.chargeable) {
|
||||||
|
try {
|
||||||
|
await pointsCharge.consume({
|
||||||
|
note: `多角度编辑消耗积分(${savedImage.id})`,
|
||||||
|
});
|
||||||
|
} catch (pointsError) {
|
||||||
|
logger.error("points_consume_failed", pointsError, {
|
||||||
|
imageId: savedImage.id,
|
||||||
|
userId: session.user.id,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const sourceAssets =
|
||||||
|
typeof image_url === "string" && image_url.trim()
|
||||||
|
? await getMediaAssetsByUserAndUrls({
|
||||||
|
userId: session.user.id,
|
||||||
|
urls: [image_url],
|
||||||
|
})
|
||||||
|
: [];
|
||||||
|
const media = await upsertMediaAsset({
|
||||||
|
userId: session.user.id,
|
||||||
|
mediaType: "image",
|
||||||
|
sourceType: "edited",
|
||||||
|
status: "ready",
|
||||||
|
originalUrl: s3Result.original,
|
||||||
|
previewUrl: s3Result.original,
|
||||||
|
thumbnailUrl: s3Result.thumbnail,
|
||||||
|
displayName: `角度编辑 ${finalHorizontalAngle}/${finalVerticalAngle}`.slice(0, 80),
|
||||||
|
prompt: `角度编辑: H${finalHorizontalAngle}° V${finalVerticalAngle}° Z${finalZoom}`,
|
||||||
|
modelId: "qwen-image-edit-2511-multiple-angles",
|
||||||
|
sourceTable: "GeneratedImage",
|
||||||
|
sourceRowId: savedImage.id,
|
||||||
|
primaryParentAssetId: sourceAssets[0]?.id ?? null,
|
||||||
|
metadata: {
|
||||||
|
horizontalAngle: finalHorizontalAngle,
|
||||||
|
verticalAngle: finalVerticalAngle,
|
||||||
|
zoom: finalZoom,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
if (media) {
|
||||||
|
for (const sourceAsset of sourceAssets) {
|
||||||
|
await createMediaAssetRelation({
|
||||||
|
userId: session.user.id,
|
||||||
|
fromAssetId: sourceAsset.id,
|
||||||
|
toAssetId: media.id,
|
||||||
|
relationType: "edited_from",
|
||||||
|
metadata: {
|
||||||
|
horizontalAngle: finalHorizontalAngle,
|
||||||
|
verticalAngle: finalVerticalAngle,
|
||||||
|
zoom: finalZoom,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (assetError) {
|
||||||
|
logger.warn("media_asset_registration_failed", assetError);
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info("request_completed", {
|
||||||
|
processingSeconds: Number(processingTime.toFixed(2)),
|
||||||
|
});
|
||||||
|
|
||||||
|
// 9. 返回结果
|
||||||
|
return NextResponse.json({
|
||||||
|
success: true,
|
||||||
|
image_url: s3Result.original,
|
||||||
|
thumbnail_url: s3Result.thumbnail,
|
||||||
|
image_base64: imageBase64Result,
|
||||||
|
processing_time: processingTime,
|
||||||
|
angles: {
|
||||||
|
horizontal_angle: finalHorizontalAngle,
|
||||||
|
vertical_angle: finalVerticalAngle,
|
||||||
|
zoom: finalZoom,
|
||||||
|
},
|
||||||
|
prompt: apiResult.prompt,
|
||||||
|
});
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
logger.error("request_failed", error);
|
||||||
|
|
||||||
|
// 输出详细的错误信息
|
||||||
|
if (error && typeof error === "object" && "status" in error) {
|
||||||
|
const apiError = error as { status?: number; body?: unknown; message?: string };
|
||||||
|
logger.warn("provider_error_summary", {
|
||||||
|
bodyType: typeof apiError.body,
|
||||||
|
hasBody: apiError.body != null,
|
||||||
|
message: apiError.message,
|
||||||
|
status: apiError.status,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (apiError.status === 403) {
|
||||||
|
return NextResponse.json(
|
||||||
|
{
|
||||||
|
error: "API 访问被拒绝 (403 Forbidden)。请检查 API Key 是否正确,以及是否有权限访问该模型。",
|
||||||
|
details: apiError.body,
|
||||||
|
},
|
||||||
|
{ status: 403 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return NextResponse.json(
|
||||||
|
{
|
||||||
|
error: error instanceof Error ? error.message : "角度编辑处理失败",
|
||||||
|
},
|
||||||
|
{ status: 500 }
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,9 +7,9 @@ import {
|
|||||||
CANVAS_SKILL_TAG_ORDER_SETTING_KEY_PREFIX,
|
CANVAS_SKILL_TAG_ORDER_SETTING_KEY_PREFIX,
|
||||||
CANVAS_SKILL_TAGS_CONFIG_VERSION,
|
CANVAS_SKILL_TAGS_CONFIG_VERSION,
|
||||||
CANVAS_SKILL_TAGS_SETTING_KEY,
|
CANVAS_SKILL_TAGS_SETTING_KEY,
|
||||||
|
type CanvasSkillTag,
|
||||||
normalizeCanvasSkillTagSettings,
|
normalizeCanvasSkillTagSettings,
|
||||||
} from "@/lib/canvas-skill-tags";
|
} from "@/lib/canvas-skill-tags";
|
||||||
import { applyCanvasSkillTagOrder } from "@/lib/canvas-skill-tag-order";
|
|
||||||
|
|
||||||
function getUserSkillTagOrderKey(userId: string) {
|
function getUserSkillTagOrderKey(userId: string) {
|
||||||
return `${CANVAS_SKILL_TAG_ORDER_SETTING_KEY_PREFIX}${userId}`;
|
return `${CANVAS_SKILL_TAG_ORDER_SETTING_KEY_PREFIX}${userId}`;
|
||||||
@ -32,6 +32,18 @@ function normalizeSkillTagOrder(value: unknown) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function applySkillTagOrder(tags: CanvasSkillTag[], order: string[]) {
|
||||||
|
if (tags.length < 2 || order.length === 0) return tags;
|
||||||
|
|
||||||
|
const tagById = new Map(tags.map((tag) => [tag.id, tag]));
|
||||||
|
const ordered = order
|
||||||
|
.map((id) => tagById.get(id))
|
||||||
|
.filter((tag): tag is CanvasSkillTag => Boolean(tag));
|
||||||
|
const orderedIds = new Set(ordered.map((tag) => tag.id));
|
||||||
|
const remaining = tags.filter((tag) => !orderedIds.has(tag.id));
|
||||||
|
return [...ordered, ...remaining];
|
||||||
|
}
|
||||||
|
|
||||||
export async function GET() {
|
export async function GET() {
|
||||||
try {
|
try {
|
||||||
const session = await auth().catch(() => null);
|
const session = await auth().catch(() => null);
|
||||||
@ -59,7 +71,7 @@ export async function GET() {
|
|||||||
const userOrder = normalizeSkillTagOrder(userOrderSetting?.value);
|
const userOrder = normalizeSkillTagOrder(userOrderSetting?.value);
|
||||||
return NextResponse.json({
|
return NextResponse.json({
|
||||||
...settings,
|
...settings,
|
||||||
tags: applyCanvasSkillTagOrder(settings.tags, userOrder),
|
tags: applySkillTagOrder(settings.tags, userOrder),
|
||||||
userOrder,
|
userOrder,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@ -121,7 +133,7 @@ export async function PATCH(request: NextRequest) {
|
|||||||
return NextResponse.json({
|
return NextResponse.json({
|
||||||
success: true,
|
success: true,
|
||||||
userOrder: tagIds,
|
userOrder: tagIds,
|
||||||
tags: applyCanvasSkillTagOrder(settings.tags, tagIds),
|
tags: applySkillTagOrder(settings.tags, tagIds),
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("[CanvasSkillTags API] Save order error:", error);
|
console.error("[CanvasSkillTags API] Save order error:", error);
|
||||||
@ -140,12 +152,10 @@ export async function PUT(request: NextRequest) {
|
|||||||
const normalized = normalizeCanvasSkillTagSettings(body);
|
const normalized = normalizeCanvasSkillTagSettings(body);
|
||||||
const enabled = normalized.enabled;
|
const enabled = normalized.enabled;
|
||||||
const title = normalized.title;
|
const title = normalized.title;
|
||||||
const categories = normalized.categories;
|
|
||||||
const tags = normalized.tags;
|
const tags = normalized.tags;
|
||||||
const nextValue = {
|
const nextValue = {
|
||||||
enabled,
|
enabled,
|
||||||
title,
|
title,
|
||||||
categories,
|
|
||||||
tags,
|
tags,
|
||||||
version: CANVAS_SKILL_TAGS_CONFIG_VERSION,
|
version: CANVAS_SKILL_TAGS_CONFIG_VERSION,
|
||||||
};
|
};
|
||||||
@ -173,7 +183,7 @@ export async function PUT(request: NextRequest) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return NextResponse.json({ success: true, enabled, title, categories, tags });
|
return NextResponse.json({ success: true, enabled, title, tags });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("[CanvasSkillTags API] Update error:", error);
|
console.error("[CanvasSkillTags API] Update error:", error);
|
||||||
return NextResponse.json({ error: "Failed to update" }, { status: 500 });
|
return NextResponse.json({ error: "Failed to update" }, { status: 500 });
|
||||||
|
|||||||
@ -14,7 +14,6 @@ const ALLOWED_MODEL_HOST_DOMAINS = [
|
|||||||
"worldlabs.ai",
|
"worldlabs.ai",
|
||||||
"s3.1080p.hk",
|
"s3.1080p.hk",
|
||||||
"img.yydd.ai",
|
"img.yydd.ai",
|
||||||
"scqny.jiansun.vip",
|
|
||||||
"blob.vercel-storage.com",
|
"blob.vercel-storage.com",
|
||||||
"aliyuncs.com",
|
"aliyuncs.com",
|
||||||
"amazonaws.com",
|
"amazonaws.com",
|
||||||
|
|||||||
@ -1,91 +0,0 @@
|
|||||||
import { NextResponse } from "next/server";
|
|
||||||
import { ZodError } from "zod";
|
|
||||||
|
|
||||||
import { auth } from "@/app/(auth)/auth";
|
|
||||||
import { canvasCapabilityClientCommandSchema } from "@/lib/canvas/capabilities/client-command";
|
|
||||||
import { CanvasCapabilityRuntimeError } from "@/lib/canvas/capabilities/context";
|
|
||||||
import { getCanvasCapabilityManifest } from "@/lib/canvas/capabilities/registry";
|
|
||||||
import { executeCanvasCapabilityRuntime } from "@/lib/canvas/capabilities/runtime";
|
|
||||||
import { getCanvasCapabilityTask } from "@/lib/canvas/capabilities/task-store";
|
|
||||||
import { CanvasAgentMutationError } from "@/lib/canvas/agent-mutations";
|
|
||||||
|
|
||||||
export const dynamic = "force-dynamic";
|
|
||||||
export const maxDuration = 600;
|
|
||||||
|
|
||||||
export async function GET(
|
|
||||||
request: Request,
|
|
||||||
{ params }: { params: Promise<{ id: string }> }
|
|
||||||
) {
|
|
||||||
const session = await auth();
|
|
||||||
if (!session?.user?.id) {
|
|
||||||
return NextResponse.json({ success: false, error: "未登录" }, { status: 401 });
|
|
||||||
}
|
|
||||||
const { id } = await params;
|
|
||||||
const requestId = new URL(request.url).searchParams.get("requestId");
|
|
||||||
if (!requestId) {
|
|
||||||
return NextResponse.json({
|
|
||||||
success: true,
|
|
||||||
capabilities: getCanvasCapabilityManifest(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
const task = await getCanvasCapabilityTask({
|
|
||||||
requestId,
|
|
||||||
userId: session.user.id,
|
|
||||||
projectId: id,
|
|
||||||
});
|
|
||||||
if (!task) {
|
|
||||||
return NextResponse.json(
|
|
||||||
{ success: false, error: "能力任务不存在" },
|
|
||||||
{ status: 404 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return NextResponse.json({ success: true, task });
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function POST(
|
|
||||||
request: Request,
|
|
||||||
{ params }: { params: Promise<{ id: string }> }
|
|
||||||
) {
|
|
||||||
const session = await auth();
|
|
||||||
if (!session?.user?.id) {
|
|
||||||
return NextResponse.json({ success: false, error: "未登录" }, { status: 401 });
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
const { id } = await params;
|
|
||||||
const command = canvasCapabilityClientCommandSchema.parse(await request.json());
|
|
||||||
const result = await executeCanvasCapabilityRuntime({
|
|
||||||
command,
|
|
||||||
projectId: id,
|
|
||||||
requestHeaders: request.headers,
|
|
||||||
requestUrl: request.url,
|
|
||||||
userId: session.user.id,
|
|
||||||
});
|
|
||||||
return NextResponse.json({ success: true, ...result });
|
|
||||||
} catch (error) {
|
|
||||||
if (
|
|
||||||
error instanceof CanvasCapabilityRuntimeError ||
|
|
||||||
error instanceof CanvasAgentMutationError
|
|
||||||
) {
|
|
||||||
return NextResponse.json(
|
|
||||||
{ success: false, error: error.message, code: error.code },
|
|
||||||
{ status: error.status }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if (error instanceof ZodError) {
|
|
||||||
return NextResponse.json(
|
|
||||||
{
|
|
||||||
success: false,
|
|
||||||
error: "画布能力参数无效",
|
|
||||||
issues: error.issues,
|
|
||||||
},
|
|
||||||
{ status: 400 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
const message = error instanceof Error ? error.message : "执行画布能力失败";
|
|
||||||
return NextResponse.json(
|
|
||||||
{ success: false, error: message },
|
|
||||||
{ status: 500 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,65 +0,0 @@
|
|||||||
import { NextRequest, NextResponse } from "next/server";
|
|
||||||
import { and, eq, inArray, sql } from "drizzle-orm";
|
|
||||||
import { auth } from "@/app/(auth)/auth";
|
|
||||||
import { db } from "@/lib/db";
|
|
||||||
import { canvasProject, imageArchiveTask, generatedImage, canvasImageTask } from "@/lib/db/schema";
|
|
||||||
import { imageArchiveSourceKey } from "@/lib/storage/image-archive-db";
|
|
||||||
import { isGeneratedImageSource } from "@/lib/storage/image-archive-urls";
|
|
||||||
import { scheduleImageArchiveWorker } from "@/lib/storage/image-archive-worker";
|
|
||||||
|
|
||||||
export async function POST(request: NextRequest, { params }: { params: Promise<{ id: string }> }) {
|
|
||||||
const session = await auth();
|
|
||||||
if (!session?.user?.id) return NextResponse.json({ error: "未登录" }, { status: 401 });
|
|
||||||
const { id } = await params;
|
|
||||||
const [project] = await db
|
|
||||||
.select({ id: canvasProject.id })
|
|
||||||
.from(canvasProject)
|
|
||||||
.where(and(eq(canvasProject.id, id), eq(canvasProject.userId, session.user.id)))
|
|
||||||
.limit(1);
|
|
||||||
if (!project) return NextResponse.json({ error: "画布不存在" }, { status: 404 });
|
|
||||||
const body = await request.json();
|
|
||||||
if (
|
|
||||||
!Array.isArray(body.urls) ||
|
|
||||||
body.urls.length > 100 ||
|
|
||||||
body.urls.some((url: unknown) => typeof url !== "string" || !isGeneratedImageSource(url))
|
|
||||||
) {
|
|
||||||
return NextResponse.json({ error: "图片地址格式错误" }, { status: 400 });
|
|
||||||
}
|
|
||||||
const urls: string[] = [...new Set<string>(body.urls)];
|
|
||||||
if (!urls.length) return NextResponse.json({ replacements: {}, pending: [] });
|
|
||||||
const source = imageArchiveTask.sourceUrl;
|
|
||||||
const rows = await db
|
|
||||||
.select({
|
|
||||||
sourceUrl: imageArchiveTask.sourceUrl,
|
|
||||||
targetUrl: imageArchiveTask.targetUrl,
|
|
||||||
status: imageArchiveTask.status,
|
|
||||||
})
|
|
||||||
.from(imageArchiveTask)
|
|
||||||
.where(
|
|
||||||
and(
|
|
||||||
inArray(imageArchiveTask.sourceUrl, urls.map(imageArchiveSourceKey)),
|
|
||||||
sql`(
|
|
||||||
exists (select 1 from ${generatedImage} where ${generatedImage.userId} = ${session.user.id}
|
|
||||||
and (${generatedImage.originalUrl} = ${source} or ${generatedImage.thumbnailUrl} = ${source}
|
|
||||||
or ${generatedImage.originalUrl} = ${imageArchiveTask.targetUrl} or ${generatedImage.thumbnailUrl} = ${imageArchiveTask.targetUrl}))
|
|
||||||
or exists (select 1 from ${canvasImageTask} where ${canvasImageTask.userId} = ${session.user.id}
|
|
||||||
and (position(${source} in ${canvasImageTask.result}::text) > 0 or position(${imageArchiveTask.targetUrl} in ${canvasImageTask.result}::text) > 0))
|
|
||||||
or exists (select 1 from ${canvasProject} where ${canvasProject.id} = ${id} and ${canvasProject.userId} = ${session.user.id}
|
|
||||||
and (position(${source} in ${canvasProject.canvasData}::text) > 0 or position(${imageArchiveTask.targetUrl} in ${canvasProject.canvasData}::text) > 0))
|
|
||||||
)`,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
const byKey = new Map(rows.map((row) => [row.sourceUrl, row]));
|
|
||||||
const replacements: Record<string, string> = {};
|
|
||||||
const pending: string[] = [];
|
|
||||||
for (const url of urls) {
|
|
||||||
const row = byKey.get(imageArchiveSourceKey(url));
|
|
||||||
if (row?.targetUrl) replacements[url] = row.targetUrl;
|
|
||||||
else if (row && row.status !== "abandoned") pending.push(url);
|
|
||||||
}
|
|
||||||
if (rows.some((row) => row.status === "pending")) scheduleImageArchiveWorker(true);
|
|
||||||
return NextResponse.json(
|
|
||||||
{ replacements, pending },
|
|
||||||
{ headers: { "Cache-Control": "private, no-store" } },
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@ -1,53 +0,0 @@
|
|||||||
import { and, eq } from "drizzle-orm";
|
|
||||||
import { z } from "zod";
|
|
||||||
import { auth } from "@/app/(auth)/auth";
|
|
||||||
import { db } from "@/lib/db";
|
|
||||||
import { canvasProject } from "@/lib/db/schema";
|
|
||||||
|
|
||||||
const canvasIdSchema = z.string().uuid();
|
|
||||||
|
|
||||||
export async function GET(
|
|
||||||
_request: Request,
|
|
||||||
{ params }: { params: Promise<{ id: string }> }
|
|
||||||
) {
|
|
||||||
const session = await auth();
|
|
||||||
if (!session?.user?.id) {
|
|
||||||
return Response.json(
|
|
||||||
{ success: false, error: "未登录" },
|
|
||||||
{ status: 401 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const { id: rawId } = await params;
|
|
||||||
const parsedId = canvasIdSchema.safeParse(rawId);
|
|
||||||
if (!parsedId.success) {
|
|
||||||
return Response.json(
|
|
||||||
{ success: false, error: "画布 ID 无效" },
|
|
||||||
{ status: 400 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const id = parsedId.data;
|
|
||||||
const [project] = await db
|
|
||||||
.select({ updatedAt: canvasProject.updatedAt })
|
|
||||||
.from(canvasProject)
|
|
||||||
.where(
|
|
||||||
and(
|
|
||||||
eq(canvasProject.id, id),
|
|
||||||
eq(canvasProject.userId, session.user.id)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
.limit(1);
|
|
||||||
|
|
||||||
if (!project) {
|
|
||||||
return Response.json(
|
|
||||||
{ success: false, error: "画布不存在" },
|
|
||||||
{ status: 404 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return Response.json(
|
|
||||||
{ success: true, data: { updatedAt: project.updatedAt.toISOString() } },
|
|
||||||
{ headers: { "Cache-Control": "no-store" } }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@ -3,7 +3,6 @@ import { auth } from '@/app/(auth)/auth';
|
|||||||
import { db } from '@/lib/db';
|
import { db } from '@/lib/db';
|
||||||
import { canvasProject } from '@/lib/db/schema';
|
import { canvasProject } from '@/lib/db/schema';
|
||||||
import { syncCanvasNodesForProject } from '@/lib/db/queries';
|
import { syncCanvasNodesForProject } from '@/lib/db/queries';
|
||||||
import { resolveArchivedImageUrls } from '@/lib/storage/image-archive-db';
|
|
||||||
import { eq, and } from 'drizzle-orm';
|
import { eq, and } from 'drizzle-orm';
|
||||||
import {
|
import {
|
||||||
buildStaleCanvasSaveConflictPayload,
|
buildStaleCanvasSaveConflictPayload,
|
||||||
@ -103,7 +102,7 @@ export async function GET(
|
|||||||
// 更新画布(支持 upsert:如果不存在则创建)
|
// 更新画布(支持 upsert:如果不存在则创建)
|
||||||
export async function PUT(
|
export async function PUT(
|
||||||
request: NextRequest,
|
request: NextRequest,
|
||||||
{ params }: { params: Promise<{ id: string }> },
|
{ params }: { params: Promise<{ id: string }> }
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
const { id } = await params;
|
const { id } = await params;
|
||||||
@ -117,34 +116,29 @@ export async function PUT(
|
|||||||
|
|
||||||
const session = await auth();
|
const session = await auth();
|
||||||
if (!session?.user?.id) {
|
if (!session?.user?.id) {
|
||||||
return NextResponse.json(
|
return NextResponse.json({ success: false, error: '未登录' }, { status: 401 });
|
||||||
{ success: false, error: "未登录" },
|
|
||||||
{ status: 401 },
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const { title, description, canvasData } = body;
|
const { title, description, thumbnail, canvasData } = body;
|
||||||
const thumbnail = await resolveArchivedImageUrls(body.thumbnail);
|
const baseCanvasRevision = body?.baseCanvasUpdatedAt || body?.baseCanvasRevision;
|
||||||
const baseCanvasRevision =
|
|
||||||
body?.baseCanvasUpdatedAt || body?.baseCanvasRevision;
|
|
||||||
const workspaceType =
|
const workspaceType =
|
||||||
body && Object.prototype.hasOwnProperty.call(body, "workspaceType")
|
body && Object.prototype.hasOwnProperty.call(body, "workspaceType")
|
||||||
? resolveWorkspaceType(body.workspaceType)
|
? resolveWorkspaceType(body.workspaceType)
|
||||||
: undefined;
|
: undefined;
|
||||||
|
|
||||||
return await db.transaction(async (tx) => {
|
// 先检查画布是否存在
|
||||||
const [existingProject] = await tx
|
const [existingProject] = await db
|
||||||
.select()
|
.select()
|
||||||
.from(canvasProject)
|
.from(canvasProject)
|
||||||
.where(
|
.where(
|
||||||
and(
|
and(
|
||||||
eq(canvasProject.id, id),
|
eq(canvasProject.id, id),
|
||||||
eq(canvasProject.userId, session.user.id),
|
eq(canvasProject.userId, session.user.id)
|
||||||
),
|
|
||||||
)
|
)
|
||||||
.for("update");
|
);
|
||||||
|
|
||||||
|
if (existingProject) {
|
||||||
if (
|
if (
|
||||||
existingProject &&
|
|
||||||
canvasData !== undefined &&
|
canvasData !== undefined &&
|
||||||
baseCanvasRevision &&
|
baseCanvasRevision &&
|
||||||
isStaleCanvasSaveBaseRevision({
|
isStaleCanvasSaveBaseRevision({
|
||||||
@ -155,66 +149,71 @@ export async function PUT(
|
|||||||
) {
|
) {
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
buildStaleCanvasSaveConflictPayload(existingProject.updatedAt),
|
buildStaleCanvasSaveConflictPayload(existingProject.updatedAt),
|
||||||
{ status: 409 },
|
{ status: 409 }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if (!existingProject) {
|
|
||||||
// The parent must exist before node/asset foreign keys are synchronized.
|
|
||||||
const [created] = await tx
|
|
||||||
.insert(canvasProject)
|
|
||||||
.values({
|
|
||||||
id,
|
|
||||||
userId: session.user.id,
|
|
||||||
createdAt: new Date(),
|
|
||||||
updatedAt: new Date(),
|
|
||||||
title: title || getDefaultWorkspaceTitle(workspaceType || "canvas"),
|
|
||||||
workspaceType: workspaceType || "canvas",
|
|
||||||
canvasData: {
|
|
||||||
nodes: [],
|
|
||||||
edges: [],
|
|
||||||
viewport: { x: 0, y: 0, zoom: 1 },
|
|
||||||
},
|
|
||||||
})
|
|
||||||
.onConflictDoNothing({ target: canvasProject.id })
|
|
||||||
.returning();
|
|
||||||
if (!created)
|
|
||||||
return NextResponse.json(
|
|
||||||
{ success: false, error: "画布已被创建,请重新加载后保存" },
|
|
||||||
{ status: 409 },
|
|
||||||
);
|
|
||||||
}
|
|
||||||
const syncedCanvasData =
|
const syncedCanvasData =
|
||||||
canvasData !== undefined
|
canvasData !== undefined
|
||||||
? await syncCanvasNodesForProject(
|
? await syncCanvasNodesForProject({
|
||||||
{ projectId: id, userId: session.user.id, canvasData },
|
projectId: id,
|
||||||
tx,
|
userId: session.user.id,
|
||||||
)
|
canvasData,
|
||||||
|
})
|
||||||
: undefined;
|
: undefined;
|
||||||
const updateData: any = { updatedAt: new Date(Math.max(Date.now(), (existingProject?.updatedAt.getTime() || 0) + 1)) };
|
|
||||||
|
// 更新现有画布
|
||||||
|
const updateData: any = { updatedAt: new Date() };
|
||||||
if (title !== undefined) updateData.title = title;
|
if (title !== undefined) updateData.title = title;
|
||||||
if (description !== undefined) updateData.description = description;
|
if (description !== undefined) updateData.description = description;
|
||||||
if (thumbnail !== undefined) updateData.thumbnail = thumbnail;
|
if (thumbnail !== undefined) updateData.thumbnail = thumbnail;
|
||||||
if (workspaceType !== undefined) updateData.workspaceType = workspaceType;
|
if (workspaceType !== undefined) updateData.workspaceType = workspaceType;
|
||||||
if (syncedCanvasData !== undefined)
|
if (syncedCanvasData !== undefined) updateData.canvasData = syncedCanvasData;
|
||||||
updateData.canvasData = syncedCanvasData;
|
|
||||||
const [saved] = await tx
|
const [updatedProject] = await db
|
||||||
.update(canvasProject)
|
.update(canvasProject)
|
||||||
.set(updateData)
|
.set(updateData)
|
||||||
.where(
|
.where(
|
||||||
and(
|
and(
|
||||||
eq(canvasProject.id, id),
|
eq(canvasProject.id, id),
|
||||||
eq(canvasProject.userId, session.user.id),
|
eq(canvasProject.userId, session.user.id)
|
||||||
),
|
)
|
||||||
)
|
)
|
||||||
.returning();
|
.returning();
|
||||||
return NextResponse.json({ success: true, data: saved });
|
|
||||||
});
|
return NextResponse.json({ success: true, data: updatedProject });
|
||||||
|
} else {
|
||||||
|
const syncedCanvasData =
|
||||||
|
canvasData !== undefined
|
||||||
|
? await syncCanvasNodesForProject({
|
||||||
|
projectId: id,
|
||||||
|
userId: session.user.id,
|
||||||
|
canvasData,
|
||||||
|
})
|
||||||
|
: undefined;
|
||||||
|
|
||||||
|
// 创建新画布(使用指定 ID)
|
||||||
|
const [newProject] = await db
|
||||||
|
.insert(canvasProject)
|
||||||
|
.values({
|
||||||
|
id, // 使用指定的 ID
|
||||||
|
userId: session.user.id,
|
||||||
|
title: title || getDefaultWorkspaceTitle(workspaceType || "canvas"),
|
||||||
|
description,
|
||||||
|
workspaceType: workspaceType || "canvas",
|
||||||
|
canvasData: syncedCanvasData || {
|
||||||
|
nodes: [],
|
||||||
|
edges: [],
|
||||||
|
viewport: { x: 0, y: 0, zoom: 1 },
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.returning();
|
||||||
|
|
||||||
|
return NextResponse.json({ success: true, data: newProject });
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("更新/创建画布失败:", error);
|
console.error('更新/创建画布失败:', error);
|
||||||
return NextResponse.json(
|
return NextResponse.json({ success: false, error: '更新画布失败' }, { status: 500 });
|
||||||
{ success: false, error: "更新画布失败" },
|
|
||||||
{ status: 500 },
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,57 +0,0 @@
|
|||||||
import { and, desc, eq, lte } from "drizzle-orm";
|
|
||||||
import { NextResponse } from "next/server";
|
|
||||||
import { z } from "zod";
|
|
||||||
import { auth } from "@/app/(auth)/auth";
|
|
||||||
import { db } from "@/lib/db";
|
|
||||||
import { tvstarSession } from "@/lib/db/schema";
|
|
||||||
import { ownsTaskProject } from "@/lib/video-canvas/task-store";
|
|
||||||
import { tvstarSessionSchema } from "@/lib/video-canvas/tvstar-session";
|
|
||||||
|
|
||||||
export const dynamic = "force-dynamic";
|
|
||||||
|
|
||||||
async function authorize(projectId: string) {
|
|
||||||
const session = await auth();
|
|
||||||
if (!session?.user?.id) return { error: NextResponse.json({ error: "未登录" }, { status: 401 }) };
|
|
||||||
if (!z.string().uuid().safeParse(projectId).success || !await ownsTaskProject(session.user.id, projectId)) {
|
|
||||||
return { error: NextResponse.json({ error: "画布不存在" }, { status: 404 }) };
|
|
||||||
}
|
|
||||||
return { userId: session.user.id };
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function GET(_request: Request, { params }: { params: Promise<{ id: string }> }) {
|
|
||||||
const { id } = await params;
|
|
||||||
const access = await authorize(id);
|
|
||||||
if (access.error) return access.error;
|
|
||||||
const rows = await db.select({ snapshot: tvstarSession.snapshot, shareToken: tvstarSession.shareToken })
|
|
||||||
.from(tvstarSession)
|
|
||||||
.where(and(eq(tvstarSession.userId, access.userId!), eq(tvstarSession.projectId, id)))
|
|
||||||
.orderBy(desc(tvstarSession.clientUpdatedAt)).limit(20);
|
|
||||||
return NextResponse.json({ sessions: rows.map(({ snapshot, shareToken }) => ({
|
|
||||||
...(snapshot as Record<string, unknown>), shareToken: shareToken || undefined,
|
|
||||||
})) }, { headers: { "Cache-Control": "no-store" } });
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function PUT(request: Request, { params }: { params: Promise<{ id: string }> }) {
|
|
||||||
const { id } = await params;
|
|
||||||
const access = await authorize(id);
|
|
||||||
if (access.error) return access.error;
|
|
||||||
const raw = await request.text();
|
|
||||||
if (raw.length > 900_000) return NextResponse.json({ error: "对话记录过大" }, { status: 413 });
|
|
||||||
let body: unknown;
|
|
||||||
try { body = JSON.parse(raw); } catch { return NextResponse.json({ error: "请求格式无效" }, { status: 400 }); }
|
|
||||||
const parsed = z.object({ sessions: z.array(tvstarSessionSchema).max(20) }).safeParse(body);
|
|
||||||
if (!parsed.success) return NextResponse.json({ error: "对话记录格式无效" }, { status: 400 });
|
|
||||||
for (const session of parsed.data.sessions) {
|
|
||||||
const clientUpdatedAt = new Date(Math.min(session.updatedAt, Date.now() + 60_000));
|
|
||||||
await db.insert(tvstarSession).values({
|
|
||||||
id: session.id, projectId: id, userId: access.userId!, title: session.title,
|
|
||||||
snapshot: session, clientUpdatedAt,
|
|
||||||
}).onConflictDoNothing({ target: tvstarSession.id });
|
|
||||||
await db.update(tvstarSession).set({ title: session.title, snapshot: session, clientUpdatedAt, updatedAt: new Date() })
|
|
||||||
.where(and(
|
|
||||||
eq(tvstarSession.id, session.id), eq(tvstarSession.projectId, id), eq(tvstarSession.userId, access.userId!),
|
|
||||||
lte(tvstarSession.clientUpdatedAt, clientUpdatedAt),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
return NextResponse.json({ ok: true });
|
|
||||||
}
|
|
||||||
@ -1,58 +0,0 @@
|
|||||||
import { randomBytes } from "node:crypto";
|
|
||||||
import { and, eq } from "drizzle-orm";
|
|
||||||
import { NextResponse } from "next/server";
|
|
||||||
import { z } from "zod";
|
|
||||||
import { auth } from "@/app/(auth)/auth";
|
|
||||||
import { db } from "@/lib/db";
|
|
||||||
import { tvstarSession } from "@/lib/db/schema";
|
|
||||||
import { ownsTaskProject } from "@/lib/video-canvas/task-store";
|
|
||||||
import { publicTVstarSnapshot, tvstarSessionSchema } from "@/lib/video-canvas/tvstar-session";
|
|
||||||
|
|
||||||
export const dynamic = "force-dynamic";
|
|
||||||
|
|
||||||
async function owner(projectId: string) {
|
|
||||||
const session = await auth();
|
|
||||||
if (!session?.user?.id) return { error: NextResponse.json({ error: "未登录" }, { status: 401 }) };
|
|
||||||
if (!z.string().uuid().safeParse(projectId).success || !await ownsTaskProject(session.user.id, projectId)) {
|
|
||||||
return { error: NextResponse.json({ error: "画布不存在" }, { status: 404 }) };
|
|
||||||
}
|
|
||||||
return { userId: session.user.id };
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function POST(request: Request, { params }: { params: Promise<{ id: string }> }) {
|
|
||||||
const { id } = await params;
|
|
||||||
const access = await owner(id);
|
|
||||||
if (access.error) return access.error;
|
|
||||||
const raw = await request.text();
|
|
||||||
if (raw.length > 300_000) return NextResponse.json({ error: "对话记录过大" }, { status: 413 });
|
|
||||||
let body: unknown;
|
|
||||||
try { body = JSON.parse(raw); } catch { return NextResponse.json({ error: "请求格式无效" }, { status: 400 }); }
|
|
||||||
const parsed = z.object({ session: tvstarSessionSchema }).safeParse(body);
|
|
||||||
if (!parsed.success) return NextResponse.json({ error: "对话记录格式无效" }, { status: 400 });
|
|
||||||
const current = parsed.data.session;
|
|
||||||
const condition = and(eq(tvstarSession.id, current.id), eq(tvstarSession.projectId, id), eq(tvstarSession.userId, access.userId!));
|
|
||||||
await db.insert(tvstarSession).values({
|
|
||||||
id: current.id, projectId: id, userId: access.userId!, title: current.title,
|
|
||||||
snapshot: current, clientUpdatedAt: new Date(Math.min(current.updatedAt, Date.now() + 60_000)),
|
|
||||||
}).onConflictDoNothing({ target: tvstarSession.id });
|
|
||||||
const existing = (await db.select({ shareToken: tvstarSession.shareToken }).from(tvstarSession).where(condition).limit(1))[0];
|
|
||||||
if (!existing) return NextResponse.json({ error: "对话不存在" }, { status: 404 });
|
|
||||||
const token = existing.shareToken || randomBytes(24).toString("base64url");
|
|
||||||
if (!existing.shareToken) {
|
|
||||||
await db.update(tvstarSession).set({
|
|
||||||
shareToken: token, sharedSnapshot: publicTVstarSnapshot(current), sharedAt: new Date(), updatedAt: new Date(),
|
|
||||||
}).where(condition);
|
|
||||||
}
|
|
||||||
return NextResponse.json({ url: `/tvstar/share/${token}`, token });
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function DELETE(request: Request, { params }: { params: Promise<{ id: string }> }) {
|
|
||||||
const { id } = await params;
|
|
||||||
const access = await owner(id);
|
|
||||||
if (access.error) return access.error;
|
|
||||||
const sessionId = new URL(request.url).searchParams.get("sessionId") || "";
|
|
||||||
if (!z.string().uuid().safeParse(sessionId).success) return NextResponse.json({ error: "对话 ID 无效" }, { status: 400 });
|
|
||||||
await db.update(tvstarSession).set({ shareToken: null, sharedSnapshot: null, sharedAt: null, updatedAt: new Date() })
|
|
||||||
.where(and(eq(tvstarSession.id, sessionId), eq(tvstarSession.projectId, id), eq(tvstarSession.userId, access.userId!)));
|
|
||||||
return NextResponse.json({ ok: true });
|
|
||||||
}
|
|
||||||
@ -1,40 +0,0 @@
|
|||||||
import { PROJECT_AGENT_OPERATIONS, projectOperationAuthorized } from "@/lib/ai/canvas-agent-runtime/project-operation-catalog";
|
|
||||||
import { NextResponse } from "next/server";
|
|
||||||
import { z } from "zod";
|
|
||||||
import { and, eq } from "drizzle-orm";
|
|
||||||
import { auth } from "@/app/(auth)/auth";
|
|
||||||
import { db } from "@/lib/db";
|
|
||||||
import { canvasProject } from "@/lib/db/schema";
|
|
||||||
import { getSystemSetting } from "@/lib/db/queries";
|
|
||||||
import {
|
|
||||||
normalizeVideoCanvasFunctionConfig,
|
|
||||||
VIDEO_CANVAS_FUNCTION_CONFIG_KEY,
|
|
||||||
} from "@/lib/video-canvas/config";
|
|
||||||
import { getCanvasAgentAccessManifest } from "@/lib/ai/canvas-agent-runtime/capability-access";
|
|
||||||
export const dynamic = "force-dynamic";
|
|
||||||
export async function GET(request: Request) {
|
|
||||||
const session = await auth();
|
|
||||||
if (!session?.user?.id) return NextResponse.json({ error: "未登录" }, { status: 401 });
|
|
||||||
const parsed = z.string().uuid().safeParse(new URL(request.url).searchParams.get("projectId"));
|
|
||||||
if (!parsed.success) return NextResponse.json({ error: "画布编号无效" }, { status: 400 });
|
|
||||||
const [project] = await db
|
|
||||||
.select({ id: canvasProject.id, workspace: canvasProject.workspaceType })
|
|
||||||
.from(canvasProject)
|
|
||||||
.where(and(eq(canvasProject.id, parsed.data), eq(canvasProject.userId, session.user.id)))
|
|
||||||
.limit(1);
|
|
||||||
if (!project) return NextResponse.json({ error: "画布不存在" }, { status: 404 });
|
|
||||||
const config = normalizeVideoCanvasFunctionConfig(
|
|
||||||
(await getSystemSetting({ key: VIDEO_CANVAS_FUNCTION_CONFIG_KEY }))?.value,
|
|
||||||
);
|
|
||||||
return NextResponse.json(
|
|
||||||
{
|
|
||||||
workspace: project.workspace,
|
|
||||||
projectServices: PROJECT_AGENT_OPERATIONS.map(item => ({ ...item, authorized: projectOperationAuthorized(item) })),
|
|
||||||
utilityTools: ["getWeather", "createDocument", "updateDocument", "requestSuggestions", "loadProjectSkill"],
|
|
||||||
capabilities: getCanvasAgentAccessManifest(config).filter(
|
|
||||||
(item) => item.workspace === (project.workspace === "video" ? "video" : "canvas"),
|
|
||||||
),
|
|
||||||
},
|
|
||||||
{ headers: { "Cache-Control": "private, no-store" } },
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@ -1,491 +0,0 @@
|
|||||||
import { agentModelCandidates } from "@/lib/ai/runtime/agent-model-failover";
|
|
||||||
import { agentModelCircuitStore } from "@/lib/ai/runtime/agent-model-circuit-store";
|
|
||||||
import { createAgentVisionExecutor } from "@/lib/ai/runtime/agent-vision-failover";
|
|
||||||
import { canvasAgentProgress } from "@/lib/ai/canvas-agent-runtime/progress";
|
|
||||||
import { getStyleProImageModelEntries } from "@/lib/style-pro/model-options";
|
|
||||||
import { registerProjectAgentTools } from "@/lib/ai/canvas-agent-runtime/project-tools";
|
|
||||||
import { isZcodeWorkspaceAuthorized } from "@/lib/ai/canvas-agent-runtime/authorization";
|
|
||||||
import {
|
|
||||||
createUIMessageStream,
|
|
||||||
createUIMessageStreamResponse,
|
|
||||||
generateText,
|
|
||||||
} from "ai";
|
|
||||||
import { after, NextResponse } from "next/server";
|
|
||||||
import { z } from "zod";
|
|
||||||
import { db } from "@/lib/db";
|
|
||||||
import { siteSettings, canvasProject } from "@/lib/db/schema";
|
|
||||||
import { eq } from "drizzle-orm";
|
|
||||||
import { auth } from "@/app/(auth)/auth";
|
|
||||||
import { postRequestBodySchema } from "@/app/(chat)/api/chat/schema";
|
|
||||||
import { entitlementsByUserType } from "@/lib/ai/entitlements";
|
|
||||||
import { getLanguageModelById } from "@/lib/ai/dynamic-provider";
|
|
||||||
import {
|
|
||||||
getAgentAiModelWithProvider,
|
|
||||||
getAccessibleAgentPresetById,
|
|
||||||
getEnabledAiModelsWithProvider,
|
|
||||||
getChatById,
|
|
||||||
getMessagesByChatId,
|
|
||||||
getMessageCountByUserId,
|
|
||||||
getSiteSettings,
|
|
||||||
saveChat,
|
|
||||||
saveMessages,
|
|
||||||
} from "@/lib/db/queries";
|
|
||||||
import { runZcodeCanvasTask } from "@/lib/ai/zcode-agent/task";
|
|
||||||
import { canvasAgentReplyText } from "@/lib/ai/zcode-agent/assistant-output";
|
|
||||||
import { createZcodeModelGateway } from "@/lib/ai/zcode-agent/model-gateway";
|
|
||||||
import { newAgentRunSnapshot, publicAgentError } from "@/lib/ai/canvas-agent-runtime/protocol";
|
|
||||||
import { shouldUseCanvasAgentRuntime } from "@/lib/ai/canvas-agent-runtime/routing";
|
|
||||||
import {
|
|
||||||
acquireAgentRun,
|
|
||||||
agentRunView,
|
|
||||||
AgentRunError,
|
|
||||||
checkpointAgentRun,
|
|
||||||
consumeAgentRunInputs,
|
|
||||||
heartbeatAgentRun,
|
|
||||||
} from "@/lib/ai/canvas-agent-runtime/store";
|
|
||||||
import { readOwnedCanvas } from "@/lib/ai/canvas-agent-runtime/canvas";
|
|
||||||
import { buildCanvasRuntimeTools } from "@/lib/ai/canvas-agent-runtime/tools";
|
|
||||||
import { canvasRuntimeSystemPrompt } from "@/lib/ai/canvas-agent-runtime/prompt";
|
|
||||||
import { verifyCanvasAgentResults } from "@/lib/ai/canvas-agent-runtime/review";
|
|
||||||
import { inferCanvasTaskRequirements } from "@/lib/ai/canvas-agent-runtime/requirements";
|
|
||||||
import {
|
|
||||||
resolveRuntimeConversationSources,
|
|
||||||
persistRuntimeConversationResults,
|
|
||||||
} from "@/lib/ai/canvas-agent-runtime/conversation";
|
|
||||||
import { getCanvasAgentFeatureFlagsFromSystemSettings } from "@/lib/canvas/agent-feature-settings";
|
|
||||||
import { canApplyCanvasAgentMutations } from "@/lib/canvas/agent-feature-flags";
|
|
||||||
|
|
||||||
import {
|
|
||||||
videoAgentSettingsSchema,
|
|
||||||
inferVideoTaskRequirements,
|
|
||||||
videoNodeUrl,
|
|
||||||
videoRuntimeSystemPrompt,
|
|
||||||
} from "@/lib/ai/canvas-agent-runtime/video-protocol";
|
|
||||||
import { readOwnedVideoCanvas } from "@/lib/ai/canvas-agent-runtime/video-store";
|
|
||||||
import {
|
|
||||||
buildVideoRuntimeTools,
|
|
||||||
createVideoRuntimePorts,
|
|
||||||
} from "@/lib/ai/canvas-agent-runtime/video-tools";
|
|
||||||
import { probeVideoMetadata } from "@/lib/ai/canvas-agent-runtime/video-probe";
|
|
||||||
import { verifyVideoAgentResults } from "@/lib/ai/canvas-agent-runtime/video-review";
|
|
||||||
|
|
||||||
export const runtime = "nodejs";
|
|
||||||
export const maxDuration = 1200;
|
|
||||||
const requestSchema = postRequestBodySchema.extend({
|
|
||||||
resumeRunId: z.string().uuid().optional(),
|
|
||||||
selectedImageModel: z.string().min(1).max(180).optional(),
|
|
||||||
workspaceVariant: z.enum(["canvas", "video"]).default("canvas"),
|
|
||||||
videoAgentSettings: videoAgentSettingsSchema.optional(),
|
|
||||||
});
|
|
||||||
export async function POST(request: Request) {
|
|
||||||
const session = await auth();
|
|
||||||
if (!session?.user?.id) return NextResponse.json({ error: "未登录" }, { status: 401 });
|
|
||||||
try {
|
|
||||||
const body = requestSchema.parse(await request.json());
|
|
||||||
if (!shouldUseCanvasAgentRuntime(body))
|
|
||||||
throw new AgentRunError("此请求不属于画布 Agent 任务", 400);
|
|
||||||
const projectId = body.contextPayload!.projectId!;
|
|
||||||
const [ownedProject] = await db.select({ userId: canvasProject.userId, workspaceType: canvasProject.workspaceType })
|
|
||||||
.from(canvasProject).where(eq(canvasProject.id, projectId)).limit(1);
|
|
||||||
if (!ownedProject || ownedProject.userId !== session.user.id) throw new AgentRunError("画布不存在", 404);
|
|
||||||
if (!isZcodeWorkspaceAuthorized(ownedProject.workspaceType)) throw new AgentRunError("视频画布暂未授权给 ZCode Agent,请使用手工功能。", 403);
|
|
||||||
const isVideo = ownedProject.workspaceType === "video";
|
|
||||||
const preset = body.agentPresetId ? await getAccessibleAgentPresetById({ id: body.agentPresetId, userId: session.user.id }) : null;
|
|
||||||
if (preset && String(preset.type) === "video") throw new AgentRunError("视频技能暂未授权给 ZCode Agent", 403);
|
|
||||||
if (body.agentPresetId && (!preset || preset.status !== "active")) throw new AgentRunError("此 Agent 预设不可用", 403);
|
|
||||||
const objective = body.message.parts
|
|
||||||
.filter((part) => part.type === "text")
|
|
||||||
.map((part) => part.text)
|
|
||||||
.join("\n")
|
|
||||||
.trim();
|
|
||||||
if (!objective) throw new AgentRunError("请描述需要完成的创作任务", 400);
|
|
||||||
const settings = await getSiteSettings();
|
|
||||||
if (
|
|
||||||
!settings.agentEnabled ||
|
|
||||||
!canApplyCanvasAgentMutations(await getCanvasAgentFeatureFlagsFromSystemSettings())
|
|
||||||
) {
|
|
||||||
throw new AgentRunError("管理员尚未开启画布 Agent 操作", 403);
|
|
||||||
}
|
|
||||||
const [usageSettings] = await db.select().from(siteSettings).limit(1);
|
|
||||||
if (usageSettings?.usageTimeEnabled) {
|
|
||||||
const time = new Intl.DateTimeFormat("en-GB", {
|
|
||||||
timeZone: "Asia/Shanghai",
|
|
||||||
hour: "2-digit",
|
|
||||||
minute: "2-digit",
|
|
||||||
hourCycle: "h23",
|
|
||||||
}).format(new Date());
|
|
||||||
if (
|
|
||||||
time < (usageSettings.usageTimeStart || "08:00") ||
|
|
||||||
time > (usageSettings.usageTimeEnd || "20:00")
|
|
||||||
)
|
|
||||||
throw new AgentRunError(usageSettings.usageTimeMessage || "当前不在系统使用时间段内", 403);
|
|
||||||
}
|
|
||||||
const count = await getMessageCountByUserId({ id: session.user.id, differenceInHours: 24 });
|
|
||||||
if (count > entitlementsByUserType[session.user.type].maxMessagesPerDay)
|
|
||||||
throw new AgentRunError("今日对话次数已达到限额", 429);
|
|
||||||
const imageCanvas = isVideo ? null : await readOwnedCanvas(session.user.id, projectId);
|
|
||||||
if (imageCanvas?.project.workspaceType === "video")
|
|
||||||
throw new AgentRunError("请使用视频画布 Agent 入口", 400);
|
|
||||||
const videoCanvas = isVideo ? await readOwnedVideoCanvas(session.user.id, projectId) : null;
|
|
||||||
const nodes = imageCanvas?.nodes || [];
|
|
||||||
const availableIds = isVideo
|
|
||||||
? videoCanvas!.nodes.map((node) => node.id)
|
|
||||||
: nodes.map((node) => node.id);
|
|
||||||
const selectedNodeIds = body.contextPayload?.selectedNodeIds || [];
|
|
||||||
if (selectedNodeIds.some((id) => !availableIds.includes(id))) {
|
|
||||||
throw new AgentRunError("选中图片尚未保存到画布,请等待保存完成后重试。");
|
|
||||||
}
|
|
||||||
const existingChat = await getChatById({ id: body.id });
|
|
||||||
if (existingChat && existingChat.userId !== session.user.id)
|
|
||||||
throw new AgentRunError("对话不存在", 404);
|
|
||||||
if (!existingChat)
|
|
||||||
await saveChat({
|
|
||||||
id: body.id,
|
|
||||||
userId: session.user.id,
|
|
||||||
title: objective.slice(0, 80),
|
|
||||||
visibility: "private",
|
|
||||||
});
|
|
||||||
const conversation = isVideo
|
|
||||||
? {
|
|
||||||
sourceNodeIds: selectedNodeIds,
|
|
||||||
targetReason: "video-selected-nodes",
|
|
||||||
previousContext: null,
|
|
||||||
}
|
|
||||||
: await resolveRuntimeConversationSources({
|
|
||||||
userId: session.user.id,
|
|
||||||
chatId: body.id,
|
|
||||||
projectId,
|
|
||||||
text: objective,
|
|
||||||
parts: body.message.parts,
|
|
||||||
nodes,
|
|
||||||
selectedNodeIds,
|
|
||||||
});
|
|
||||||
const candidates = agentModelCandidates(await getAgentAiModelWithProvider(), await getEnabledAiModelsWithProvider());
|
|
||||||
const configured = candidates[0];
|
|
||||||
if (!configured) throw new AgentRunError("请先在后台配置可用的多模态 Agent 对话模型", 503);
|
|
||||||
let model = await getLanguageModelById(configured.model.id);
|
|
||||||
for (const candidate of candidates.slice(1)) {
|
|
||||||
if (model) break;
|
|
||||||
model = await getLanguageModelById(candidate.model.id);
|
|
||||||
}
|
|
||||||
if (!model) throw new AgentRunError("Agent 模型暂不可用", 503);
|
|
||||||
const visionModel = model;
|
|
||||||
const executeVisionModel = createAgentVisionExecutor(candidates, agentModelCircuitStore, getLanguageModelById);
|
|
||||||
// Validate the user's image choice independently of the Agent planning model.
|
|
||||||
if (body.selectedImageModel) {
|
|
||||||
const images = await getStyleProImageModelEntries();
|
|
||||||
if (!images.some(({ model }) => model.id === body.selectedImageModel && model.healthStatus !== "auto_disabled"))
|
|
||||||
throw new AgentRunError("你选择的生图模型当前不可用,请重新选择;不会自动换模型生成。", 400);
|
|
||||||
}
|
|
||||||
const assistantMessageId = crypto.randomUUID();
|
|
||||||
const previousVideoMessages =
|
|
||||||
existingChat
|
|
||||||
? (await getMessagesByChatId({ id: body.id })).slice(-6).map((message) => ({
|
|
||||||
role: message.role as "user" | "assistant",
|
|
||||||
content: (message.parts as Array<{ type: string; text?: string }>)
|
|
||||||
.filter((part) => part.type === "text")
|
|
||||||
.map((part) => part.text || "")
|
|
||||||
.join("\n")
|
|
||||||
.slice(0, 4000),
|
|
||||||
}))
|
|
||||||
: [];
|
|
||||||
const initialSnapshot = newAgentRunSnapshot(objective, [
|
|
||||||
...previousVideoMessages,
|
|
||||||
{ role: "user", content: objective },
|
|
||||||
]);
|
|
||||||
initialSnapshot.workspace = isVideo ? "video" : "canvas";
|
|
||||||
if (isVideo) {
|
|
||||||
initialSnapshot.videoRequirements = inferVideoTaskRequirements(objective);
|
|
||||||
initialSnapshot.videoSettings = videoAgentSettingsSchema.parse({
|
|
||||||
...body.videoAgentSettings,
|
|
||||||
...(initialSnapshot.videoRequirements.duration
|
|
||||||
? { duration: initialSnapshot.videoRequirements.duration }
|
|
||||||
: {}),
|
|
||||||
...(initialSnapshot.videoRequirements.ratio
|
|
||||||
? { ratio: initialSnapshot.videoRequirements.ratio }
|
|
||||||
: {}),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
initialSnapshot.sourceNodeIds = conversation.sourceNodeIds;
|
|
||||||
initialSnapshot.requirements = {
|
|
||||||
...inferCanvasTaskRequirements(objective, initialSnapshot.sourceNodeIds.length),
|
|
||||||
sourceCount: initialSnapshot.sourceNodeIds.length,
|
|
||||||
};
|
|
||||||
const run = await acquireAgentRun({
|
|
||||||
id: body.message.id,
|
|
||||||
userId: session.user.id,
|
|
||||||
projectId,
|
|
||||||
chatId: body.id,
|
|
||||||
assistantMessageId,
|
|
||||||
resumeRunId: body.resumeRunId,
|
|
||||||
snapshot: initialSnapshot,
|
|
||||||
});
|
|
||||||
const snapshot = run.snapshot;
|
|
||||||
snapshot.imagePreferences = {
|
|
||||||
...snapshot.imagePreferences,
|
|
||||||
// Each submitted turn captures the current controls, including switching back to Auto.
|
|
||||||
modelId: body.selectedImageModel,
|
|
||||||
parameters: body.imageParams || snapshot.imagePreferences?.parameters,
|
|
||||||
};
|
|
||||||
const taskSourceNodeIds = snapshot.sourceNodeIds ?? selectedNodeIds;
|
|
||||||
const context = {
|
|
||||||
recentConversation: previousVideoMessages,
|
|
||||||
preset: preset ? { name: preset.name, type: preset.type, promptTemplate: preset.promptTemplate, defaultModelId: preset.defaultModelId, defaultParams: preset.defaultParams, slots: preset.slots.map(slot => ({ title: slot.title, promptTemplate: slot.promptTemplate, defaultParams: slot.defaultParams })) } : null,
|
|
||||||
videoSettings: snapshot.videoSettings,
|
|
||||||
selectedNodeIds: taskSourceNodeIds,
|
|
||||||
nodes: nodes
|
|
||||||
.filter((node) => taskSourceNodeIds.includes(node.id))
|
|
||||||
.map((node) => ({
|
|
||||||
id: node.id,
|
|
||||||
type: node.type,
|
|
||||||
position: node.position,
|
|
||||||
size: node.size,
|
|
||||||
title: node.data.title,
|
|
||||||
})),
|
|
||||||
skillInstructions: body.hiddenPrompt || undefined,
|
|
||||||
imagePreferences: snapshot.imagePreferences,
|
|
||||||
targetReason: conversation.targetReason,
|
|
||||||
previousConversationContext: conversation.previousContext,
|
|
||||||
};
|
|
||||||
await saveMessages({
|
|
||||||
messages: [
|
|
||||||
{
|
|
||||||
id: body.message.id,
|
|
||||||
chatId: body.id,
|
|
||||||
role: "user",
|
|
||||||
parts: body.message.parts,
|
|
||||||
attachments: [],
|
|
||||||
createdAt: new Date(),
|
|
||||||
videoStorageStatus: null,
|
|
||||||
temporaryVideoUrl: null,
|
|
||||||
permanentVideoUrl: null,
|
|
||||||
archivedAt: null,
|
|
||||||
videoMetadata: null,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
});
|
|
||||||
let archiveVideoResults: (() => Promise<void>) | undefined;
|
|
||||||
const stream = createUIMessageStream({
|
|
||||||
execute: async ({ writer }) => {
|
|
||||||
const emit = (type: string, data: unknown) => writer.write({ type, data, ...(type === "data-canvasAgentRun" ? { transient: true } : {}) } as never);
|
|
||||||
let cancelled = false;
|
|
||||||
let leaseLost = false;
|
|
||||||
const activeModelController = new AbortController();
|
|
||||||
const withModel: typeof executeVisionModel = (execute, signal) => executeVisionModel(execute,
|
|
||||||
signal ? AbortSignal.any([signal, activeModelController.signal]) : activeModelController.signal);
|
|
||||||
const heartbeat = setInterval(() => {
|
|
||||||
void heartbeatAgentRun(run)
|
|
||||||
.then((current) => {
|
|
||||||
if (!current) leaseLost = true;
|
|
||||||
if (current?.cancelRequested) cancelled = true;
|
|
||||||
if (cancelled || leaseLost) activeModelController?.abort();
|
|
||||||
})
|
|
||||||
.catch(() => {
|
|
||||||
leaseLost = true;
|
|
||||||
activeModelController?.abort();
|
|
||||||
});
|
|
||||||
emit("data-keepalive", { label: "canvas-agent-runtime", ts: Date.now() });
|
|
||||||
}, 10_000);
|
|
||||||
const textId = crypto.randomUUID();
|
|
||||||
writer.write({ type: "start", messageId: assistantMessageId });
|
|
||||||
writer.write({ type: "text-start", id: textId });
|
|
||||||
let streamedText = "";
|
|
||||||
const emitAssistantText = (delta: string) => {
|
|
||||||
streamedText += delta;
|
|
||||||
writer.write({ type: "text-delta", id: textId, delta });
|
|
||||||
};
|
|
||||||
try {
|
|
||||||
const videoPorts = isVideo
|
|
||||||
? await createVideoRuntimePorts({
|
|
||||||
userId: session.user.id,
|
|
||||||
chatId: body.id,
|
|
||||||
projectId,
|
|
||||||
runId: run.id,
|
|
||||||
assistantMessageId,
|
|
||||||
request,
|
|
||||||
snapshot,
|
|
||||||
emit,
|
|
||||||
})
|
|
||||||
: null;
|
|
||||||
archiveVideoResults = videoPorts?.archiveResults;
|
|
||||||
const tools = isVideo
|
|
||||||
? buildVideoRuntimeTools({
|
|
||||||
runId: run.id,
|
|
||||||
projectId,
|
|
||||||
selectedNodeIds,
|
|
||||||
snapshot,
|
|
||||||
ports: videoPorts!,
|
|
||||||
})
|
|
||||||
: await buildCanvasRuntimeTools({
|
|
||||||
userId: session.user.id,
|
|
||||||
chatId: body.id,
|
|
||||||
projectId,
|
|
||||||
runId: run.id,
|
|
||||||
assistantMessageId,
|
|
||||||
selectedNodeIds: taskSourceNodeIds,
|
|
||||||
request,
|
|
||||||
model: visionModel || model,
|
|
||||||
supportsVision: Boolean(visionModel),
|
|
||||||
withModel,
|
|
||||||
imageSize: snapshot.imagePreferences?.parameters?.imageSize,
|
|
||||||
imagePreferences: snapshot.imagePreferences,
|
|
||||||
preferredModelId: snapshot.imagePreferences?.modelId || (preset?.type === "image" ? preset.defaultModelId || undefined : undefined),
|
|
||||||
operationSelectedNodeIds: selectedNodeIds,
|
|
||||||
objective: [snapshot.objective, ...snapshot.inputs.map((input) => input.text)].join(
|
|
||||||
"\n",
|
|
||||||
),
|
|
||||||
emit,
|
|
||||||
});
|
|
||||||
await registerProjectAgentTools({ session, projectId, request, objective: snapshot.objective,
|
|
||||||
definitions: tools.definitions, executable: tools.executable, emit,
|
|
||||||
imagePreferences: snapshot.imagePreferences });
|
|
||||||
const gateway = await createZcodeModelGateway(configured, undefined, candidates);
|
|
||||||
const status = await runZcodeCanvasTask({
|
|
||||||
runId: run.id, snapshot, definitions: tools.definitions,
|
|
||||||
instructions: (isVideo ? videoRuntimeSystemPrompt : canvasRuntimeSystemPrompt)(snapshot.objective, snapshot.summary, snapshot.inputs.map(input => input.text), context),
|
|
||||||
context, gateway, signal: activeModelController.signal,
|
|
||||||
onAssistantText: emitAssistantText,
|
|
||||||
tools: tools.executable,
|
|
||||||
checkpoint: async (state, stateStatus) => {
|
|
||||||
if (leaseLost) throw new AgentRunError("任务执行已中断");
|
|
||||||
const saved = await checkpointAgentRun(run, state, stateStatus);
|
|
||||||
cancelled ||= saved.cancelRequested;
|
|
||||||
emit("data-canvasAgentRun", agentRunView(saved));
|
|
||||||
},
|
|
||||||
consumeInputs: async (state) => {
|
|
||||||
if (leaseLost) throw new AgentRunError("任务执行已中断");
|
|
||||||
const control = await consumeAgentRunInputs(run, state);
|
|
||||||
cancelled ||= control.cancelled;
|
|
||||||
return { cancelled };
|
|
||||||
},
|
|
||||||
isCancelled: async () => {
|
|
||||||
const current = await heartbeatAgentRun(run);
|
|
||||||
if (!current) throw new AgentRunError("任务执行已中断");
|
|
||||||
cancelled ||= current.cancelRequested;
|
|
||||||
return cancelled;
|
|
||||||
},
|
|
||||||
verify: async (state) =>
|
|
||||||
isVideo
|
|
||||||
? verifyVideoAgentResults({
|
|
||||||
snapshot: state,
|
|
||||||
nodes: (await readOwnedVideoCanvas(session.user.id, projectId)).nodes,
|
|
||||||
inspect: async (node, requirements) => {
|
|
||||||
const metadata = await probeVideoMetadata(videoNodeUrl(node));
|
|
||||||
const report = await videoPorts!.analyze({
|
|
||||||
mode: "video-analysis",
|
|
||||||
prompt: `请基于真实抽帧画面检查主体、场景、镜头连贯性,说明不确定内容。用户要求:${requirements}`,
|
|
||||||
imageUrls: [],
|
|
||||||
videoUrls: [videoNodeUrl(node)],
|
|
||||||
});
|
|
||||||
const verdict = await generateText({
|
|
||||||
model,
|
|
||||||
system:
|
|
||||||
'你是视频验收员。依据实际抽帧分析判断是否满足用户要求;不能从文件名或提示词判断。若报告无法读取真实帧,必须不通过。抽帧只能检查画面,不能证明音频、精确时长或完整运动,涉及这些要求无法确认时说明需要人工检查。仅返回 JSON {"passed":boolean,"issues":string[]}。',
|
|
||||||
prompt: JSON.stringify({ requirements, report }),
|
|
||||||
maxOutputTokens: 1200,
|
|
||||||
maxRetries: 0,
|
|
||||||
abortSignal: AbortSignal.timeout(60_000),
|
|
||||||
});
|
|
||||||
state.inputTokens += verdict.usage.inputTokens || 0;
|
|
||||||
state.outputTokens += verdict.usage.outputTokens || 0;
|
|
||||||
const match = verdict.text.match(/\{[\s\S]*\}/);
|
|
||||||
if (!match) throw new Error("视频检查未返回有效结果");
|
|
||||||
return {
|
|
||||||
...z
|
|
||||||
.object({ passed: z.boolean(), issues: z.array(z.string()).max(12) })
|
|
||||||
.parse(JSON.parse(match[0])),
|
|
||||||
...metadata,
|
|
||||||
};
|
|
||||||
},
|
|
||||||
})
|
|
||||||
: verifyCanvasAgentResults({
|
|
||||||
snapshot: state,
|
|
||||||
userId: session.user.id,
|
|
||||||
projectId,
|
|
||||||
model: visionModel || model,
|
|
||||||
supportsVision: Boolean(visionModel),
|
|
||||||
withModel,
|
|
||||||
signal: activeModelController.signal,
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
const finalText = canvasAgentReplyText(snapshot);
|
|
||||||
const progress = canvasAgentProgress(agentRunView({ ...run, snapshot, status }));
|
|
||||||
writer.write({ type: "data-canvasAgentProgress", id: run.id, data: progress });
|
|
||||||
if (!isVideo)
|
|
||||||
await persistRuntimeConversationResults({
|
|
||||||
userId: session.user.id,
|
|
||||||
chatId: body.id,
|
|
||||||
projectId,
|
|
||||||
assistantMessageId,
|
|
||||||
snapshot,
|
|
||||||
}).catch((error) => {
|
|
||||||
console.warn("[Canvas Agent] Conversation memory update failed", {
|
|
||||||
runId: run.id,
|
|
||||||
error: publicAgentError(error),
|
|
||||||
});
|
|
||||||
});
|
|
||||||
await saveMessages({
|
|
||||||
messages: [
|
|
||||||
{
|
|
||||||
id: assistantMessageId,
|
|
||||||
chatId: body.id,
|
|
||||||
role: "assistant",
|
|
||||||
parts: [{ type: "text", text: finalText }, { type: "data-canvasAgentProgress", data: progress }],
|
|
||||||
attachments: [],
|
|
||||||
createdAt: new Date(),
|
|
||||||
videoStorageStatus: null,
|
|
||||||
temporaryVideoUrl: null,
|
|
||||||
permanentVideoUrl: null,
|
|
||||||
archivedAt: null,
|
|
||||||
videoMetadata: null,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
});
|
|
||||||
emitAssistantText(finalText.startsWith(streamedText) ? finalText.slice(streamedText.length) : `\n\n${snapshot.message}`);
|
|
||||||
} catch (error) {
|
|
||||||
const statusText = publicAgentError(error);
|
|
||||||
const text = canvasAgentReplyText({ ...snapshot, message: statusText });
|
|
||||||
await checkpointAgentRun(run, { ...snapshot, message: statusText }, "needs_review").catch(
|
|
||||||
() => undefined,
|
|
||||||
);
|
|
||||||
const progress = canvasAgentProgress(agentRunView({ ...run, snapshot: { ...snapshot, message: statusText }, status: "needs_review" }));
|
|
||||||
writer.write({ type: "data-canvasAgentProgress", id: run.id, data: progress });
|
|
||||||
await saveMessages({ messages: [{ id: assistantMessageId, chatId: body.id, role: "assistant",
|
|
||||||
parts: [{ type: "text", text }, { type: "data-canvasAgentProgress", data: progress }],
|
|
||||||
attachments: [], createdAt: new Date(), videoStorageStatus: null, temporaryVideoUrl: null,
|
|
||||||
permanentVideoUrl: null, archivedAt: null, videoMetadata: null }] }).catch(() => undefined);
|
|
||||||
emitAssistantText(text.startsWith(streamedText) ? text.slice(streamedText.length) : `\n\n${statusText}`);
|
|
||||||
} finally {
|
|
||||||
clearInterval(heartbeat);
|
|
||||||
writer.write({ type: "text-end", id: textId });
|
|
||||||
writer.write({ type: "finish" });
|
|
||||||
}
|
|
||||||
},
|
|
||||||
});
|
|
||||||
// Browser disconnection does not cancel paid image work. Keep consuming on the server.
|
|
||||||
const [client, background] = stream.tee();
|
|
||||||
after(async () => {
|
|
||||||
const reader = background.getReader();
|
|
||||||
try {
|
|
||||||
while (!(await reader.read()).done) {
|
|
||||||
/* Drain the server branch. */
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
reader.releaseLock();
|
|
||||||
await archiveVideoResults?.();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return createUIMessageStreamResponse({
|
|
||||||
stream: client,
|
|
||||||
headers: { "Cache-Control": "no-store", "X-Accel-Buffering": "no" },
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
return NextResponse.json(
|
|
||||||
{ error: publicAgentError(error), message: publicAgentError(error) },
|
|
||||||
{
|
|
||||||
status:
|
|
||||||
error instanceof AgentRunError ? error.status : error instanceof z.ZodError ? 400 : 503,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,65 +0,0 @@
|
|||||||
import { NextResponse } from "next/server";
|
|
||||||
import { z } from "zod";
|
|
||||||
import { auth } from "@/app/(auth)/auth";
|
|
||||||
import {
|
|
||||||
AgentRunError,
|
|
||||||
agentRunView,
|
|
||||||
assertAgentRunOwnership,
|
|
||||||
controlAgentRun,
|
|
||||||
getAgentRun,
|
|
||||||
} from "@/lib/ai/canvas-agent-runtime/store";
|
|
||||||
import { publicAgentError } from "@/lib/ai/canvas-agent-runtime/protocol";
|
|
||||||
|
|
||||||
export const dynamic = "force-dynamic";
|
|
||||||
const scope = z.object({
|
|
||||||
chatId: z.string().uuid(),
|
|
||||||
projectId: z.string().uuid(),
|
|
||||||
runId: z.string().uuid().optional(),
|
|
||||||
});
|
|
||||||
const control = scope
|
|
||||||
.extend({
|
|
||||||
runId: z.string().uuid(),
|
|
||||||
action: z.enum(["steer", "cancel"]),
|
|
||||||
inputId: z.string().uuid().optional(),
|
|
||||||
text: z.string().trim().min(1).max(2000).optional(),
|
|
||||||
})
|
|
||||||
.refine(
|
|
||||||
(input) => input.action !== "steer" || Boolean(input.inputId && input.text),
|
|
||||||
"补充要求缺少内容或请求号",
|
|
||||||
);
|
|
||||||
|
|
||||||
export async function GET(request: Request) {
|
|
||||||
const session = await auth();
|
|
||||||
if (!session?.user?.id) return NextResponse.json({ error: "未登录" }, { status: 401 });
|
|
||||||
try {
|
|
||||||
const input = scope.parse(Object.fromEntries(new URL(request.url).searchParams));
|
|
||||||
await assertAgentRunOwnership(session.user.id, input.projectId, input.chatId);
|
|
||||||
const run = await getAgentRun(session.user.id, input.chatId, input.projectId, input.runId);
|
|
||||||
return NextResponse.json(
|
|
||||||
{ run: run ? agentRunView(run) : null },
|
|
||||||
{ headers: { "Cache-Control": "private, no-store" } },
|
|
||||||
);
|
|
||||||
} catch (error) {
|
|
||||||
return failure(error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
export async function POST(request: Request) {
|
|
||||||
const session = await auth();
|
|
||||||
if (!session?.user?.id) return NextResponse.json({ error: "未登录" }, { status: 401 });
|
|
||||||
try {
|
|
||||||
const input = control.parse(await request.json());
|
|
||||||
await assertAgentRunOwnership(session.user.id, input.projectId, input.chatId);
|
|
||||||
return NextResponse.json({ run: await controlAgentRun({ ...input, userId: session.user.id }) });
|
|
||||||
} catch (error) {
|
|
||||||
return failure(error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
function failure(error: unknown) {
|
|
||||||
return NextResponse.json(
|
|
||||||
{ error: publicAgentError(error) },
|
|
||||||
{
|
|
||||||
status:
|
|
||||||
error instanceof AgentRunError ? error.status : error instanceof z.ZodError ? 400 : 503,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@ -1,112 +0,0 @@
|
|||||||
import { createHash, randomUUID } from "node:crypto";
|
|
||||||
import { mkdtemp, readFile, rm, stat, writeFile } from "node:fs/promises";
|
|
||||||
import { tmpdir } from "node:os";
|
|
||||||
import path from "node:path";
|
|
||||||
import { NextResponse } from "next/server";
|
|
||||||
import { auth } from "@/app/(auth)/auth";
|
|
||||||
import { uploadFile } from "@/lib/storage/service";
|
|
||||||
import { ffmpeg, mediaSafety, probeMedia } from "@/lib/video-canvas/smart-edit/local";
|
|
||||||
import {
|
|
||||||
remoteAudio,
|
|
||||||
remoteAudioConfig,
|
|
||||||
} from "@/lib/video-canvas/smart-edit/remote-audio";
|
|
||||||
|
|
||||||
export const runtime = "nodejs";
|
|
||||||
export const maxDuration = 600;
|
|
||||||
|
|
||||||
const running = new Set<string>();
|
|
||||||
const allowedStems = ["vocals", "background"] as const;
|
|
||||||
|
|
||||||
export async function POST(request: Request) {
|
|
||||||
const session = await auth();
|
|
||||||
const userId = session?.user?.id;
|
|
||||||
if (!userId)
|
|
||||||
return NextResponse.json({ success: false, error: "未登录" }, { status: 401 });
|
|
||||||
const body = (await request.json().catch(() => null)) as {
|
|
||||||
url?: unknown;
|
|
||||||
stem?: unknown;
|
|
||||||
} | null;
|
|
||||||
const url = typeof body?.url === "string" ? body.url.trim() : "";
|
|
||||||
const stem = typeof body?.stem === "string" ? body.stem : "";
|
|
||||||
if (!/^https?:\/\//i.test(url) || !allowedStems.includes(stem as (typeof allowedStems)[number]))
|
|
||||||
return NextResponse.json({ success: false, error: "参数不完整" }, { status: 400 });
|
|
||||||
if (!remoteAudioConfig())
|
|
||||||
return NextResponse.json(
|
|
||||||
{ success: false, error: "音频分离服务未配置,请联系管理员" },
|
|
||||||
{ status: 503 }
|
|
||||||
);
|
|
||||||
const lockKey = `${userId}:${url}:${stem}`;
|
|
||||||
if (running.has(lockKey) || running.size >= 3)
|
|
||||||
return NextResponse.json(
|
|
||||||
{ success: false, error: "已有分离任务在进行,请稍候" },
|
|
||||||
{ status: 429 }
|
|
||||||
);
|
|
||||||
running.add(lockKey);
|
|
||||||
let dir = "";
|
|
||||||
try {
|
|
||||||
dir = await mkdtemp(path.join(tmpdir(), "canvas-separate-"));
|
|
||||||
const input = path.join(dir, "input");
|
|
||||||
const response = await fetch(url, {
|
|
||||||
signal: AbortSignal.timeout(180000),
|
|
||||||
cache: "no-store",
|
|
||||||
});
|
|
||||||
if (!response.ok) throw new Error("素材下载失败,请确认素材仍可访问");
|
|
||||||
const bytes = Buffer.from(await response.arrayBuffer());
|
|
||||||
if (!bytes.length || bytes.length > 150 * 1024 * 1024)
|
|
||||||
throw new Error("素材必须非空且不超过 125 MiB");
|
|
||||||
await writeFile(input, bytes);
|
|
||||||
const info = await probeMedia(input, 600);
|
|
||||||
if (!info.hasAudio) throw new Error("当前视频没有音轨");
|
|
||||||
const inputAudio = path.join(dir, "separation-input.wav");
|
|
||||||
const durationSeconds = Math.round(info.duration / 1e6);
|
|
||||||
await ffmpeg(
|
|
||||||
[
|
|
||||||
...mediaSafety,
|
|
||||||
"-i",
|
|
||||||
input,
|
|
||||||
"-vn",
|
|
||||||
"-t",
|
|
||||||
String(durationSeconds),
|
|
||||||
"-ac",
|
|
||||||
"2",
|
|
||||||
"-ar",
|
|
||||||
"44100",
|
|
||||||
"-c:a",
|
|
||||||
"pcm_f32le",
|
|
||||||
inputAudio,
|
|
||||||
],
|
|
||||||
undefined,
|
|
||||||
240000
|
|
||||||
);
|
|
||||||
const requestKey = createHash("sha256")
|
|
||||||
.update(`${url}:${stem}:${(await stat(inputAudio)).size}`)
|
|
||||||
.digest("hex");
|
|
||||||
await remoteAudio("separate", inputAudio, userId, requestKey, dir);
|
|
||||||
const stemFile = path.join(dir, `${stem}.wav`);
|
|
||||||
const wav = await readFile(stemFile);
|
|
||||||
if (wav.length < 1024) throw new Error("分离结果异常,请稍后重试");
|
|
||||||
const uploaded = await uploadFile(
|
|
||||||
`canvas-separate-${randomUUID()}.wav`,
|
|
||||||
await new Blob([wav]).arrayBuffer()
|
|
||||||
);
|
|
||||||
return NextResponse.json({
|
|
||||||
success: true,
|
|
||||||
data: {
|
|
||||||
url: uploaded.url,
|
|
||||||
duration: Math.round(info.duration / 1e6),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
return NextResponse.json(
|
|
||||||
{
|
|
||||||
success: false,
|
|
||||||
error:
|
|
||||||
error instanceof Error ? error.message : "人声分离失败,原音轨已保留",
|
|
||||||
},
|
|
||||||
{ status: 500 }
|
|
||||||
);
|
|
||||||
} finally {
|
|
||||||
running.delete(lockKey);
|
|
||||||
if (dir) await rm(dir, { recursive: true, force: true });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,24 +0,0 @@
|
|||||||
import { NextResponse } from "next/server";
|
|
||||||
import { auth } from "@/app/(auth)/auth";
|
|
||||||
import { listCanvasAudioModels } from "@/lib/video-canvas/audio-generation";
|
|
||||||
import { audioContract } from "@/lib/video-canvas/audio-contract";
|
|
||||||
export async function GET() {
|
|
||||||
const session = await auth();
|
|
||||||
if (!session?.user?.id) return NextResponse.json({ error: "未登录" }, { status: 401 });
|
|
||||||
try {
|
|
||||||
const models = await listCanvasAudioModels();
|
|
||||||
return NextResponse.json(
|
|
||||||
{
|
|
||||||
models: models.map(({ model }) => ({
|
|
||||||
id: model.id,
|
|
||||||
modelId: model.modelId,
|
|
||||||
name: model.displayName,
|
|
||||||
...audioContract(model.modelId),
|
|
||||||
})),
|
|
||||||
},
|
|
||||||
{ headers: { "Cache-Control": "no-store" } },
|
|
||||||
);
|
|
||||||
} catch {
|
|
||||||
return NextResponse.json({ error: "音频模型读取失败,请稍后重试" }, { status: 503 });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,266 +0,0 @@
|
|||||||
import { lookup } from "node:dns/promises";
|
|
||||||
import { isIP } from "node:net";
|
|
||||||
import { NextResponse } from "next/server";
|
|
||||||
import { z } from "zod";
|
|
||||||
import { auth } from "@/app/(auth)/auth";
|
|
||||||
import { getChatById, getMessageById, getSystemSetting } from "@/lib/db/queries";
|
|
||||||
import {
|
|
||||||
normalizeOpenClawChatConfig,
|
|
||||||
OPENCLAW_CHAT_CONFIG_KEY,
|
|
||||||
} from "@/lib/openclaw-chat/config";
|
|
||||||
import { normalizeClawdTaskMetadata } from "@/lib/openclaw-chat/task-state";
|
|
||||||
|
|
||||||
export const runtime = "nodejs";
|
|
||||||
export const dynamic = "force-dynamic";
|
|
||||||
export const maxDuration = 120;
|
|
||||||
|
|
||||||
const querySchema = z.object({
|
|
||||||
chatId: z.string().uuid(),
|
|
||||||
messageId: z.string().uuid(),
|
|
||||||
index: z.coerce.number().int().min(0).max(127),
|
|
||||||
download: z.enum(["0", "1"]).optional().default("0"),
|
|
||||||
});
|
|
||||||
|
|
||||||
const MAX_REDIRECTS = 4;
|
|
||||||
const RESPONSE_HEADERS = [
|
|
||||||
"accept-ranges",
|
|
||||||
"content-length",
|
|
||||||
"content-range",
|
|
||||||
"etag",
|
|
||||||
"last-modified",
|
|
||||||
] as const;
|
|
||||||
|
|
||||||
function isPrivateIpv4(address: string) {
|
|
||||||
const octets = address.split(".").map(Number);
|
|
||||||
if (octets.length !== 4 || octets.some((value) => !Number.isInteger(value))) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
const [a, b] = octets;
|
|
||||||
return (
|
|
||||||
a === 0 ||
|
|
||||||
a === 10 ||
|
|
||||||
a === 127 ||
|
|
||||||
(a === 100 && b >= 64 && b <= 127) ||
|
|
||||||
(a === 169 && b === 254) ||
|
|
||||||
(a === 172 && b >= 16 && b <= 31) ||
|
|
||||||
(a === 192 && b === 168) ||
|
|
||||||
a >= 224
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
function isPrivateIpAddress(address: string) {
|
|
||||||
if (isIP(address) === 4) return isPrivateIpv4(address);
|
|
||||||
if (isIP(address) !== 6) return true;
|
|
||||||
const normalized = address.toLowerCase();
|
|
||||||
if (normalized.startsWith("::ffff:")) {
|
|
||||||
return isPrivateIpv4(normalized.slice("::ffff:".length));
|
|
||||||
}
|
|
||||||
return (
|
|
||||||
normalized === "::" ||
|
|
||||||
normalized === "::1" ||
|
|
||||||
normalized.startsWith("fc") ||
|
|
||||||
normalized.startsWith("fd") ||
|
|
||||||
normalized.startsWith("fe8") ||
|
|
||||||
normalized.startsWith("fe9") ||
|
|
||||||
normalized.startsWith("fea") ||
|
|
||||||
normalized.startsWith("feb") ||
|
|
||||||
normalized.startsWith("ff")
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
async function validateTargetUrl(target: URL, trustedOrigin: string) {
|
|
||||||
if (target.username || target.password) return false;
|
|
||||||
if (target.origin === trustedOrigin) {
|
|
||||||
return target.protocol === "http:" || target.protocol === "https:";
|
|
||||||
}
|
|
||||||
if (target.protocol !== "https:") return false;
|
|
||||||
const hostname = target.hostname.toLowerCase().replace(/^\[|\]$/g, "");
|
|
||||||
if (
|
|
||||||
hostname === "localhost" ||
|
|
||||||
hostname.endsWith(".localhost") ||
|
|
||||||
hostname.endsWith(".local")
|
|
||||||
) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (isIP(hostname)) return !isPrivateIpAddress(hostname);
|
|
||||||
|
|
||||||
const addresses = await lookup(hostname, { all: true, verbatim: true }).catch(
|
|
||||||
() => []
|
|
||||||
);
|
|
||||||
return (
|
|
||||||
addresses.length > 0 &&
|
|
||||||
addresses.every((entry) => !isPrivateIpAddress(entry.address))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
function normalizeRangeHeader(value: string | null) {
|
|
||||||
const range = String(value || "").trim();
|
|
||||||
if (!range) return null;
|
|
||||||
const match = range.match(/^bytes=(\d*)-(\d*)$/);
|
|
||||||
if (!match || (!match[1] && !match[2])) return "";
|
|
||||||
if (match[1] && match[2] && Number(match[1]) > Number(match[2])) return "";
|
|
||||||
return range;
|
|
||||||
}
|
|
||||||
|
|
||||||
function inferContentType(filename: string, declaredType: string) {
|
|
||||||
if (declaredType) return declaredType;
|
|
||||||
const lower = filename.toLowerCase();
|
|
||||||
if (lower.endsWith(".glb")) return "model/gltf-binary";
|
|
||||||
if (lower.endsWith(".gltf")) return "model/gltf+json";
|
|
||||||
if (lower.endsWith(".png")) return "image/png";
|
|
||||||
if (lower.endsWith(".webp")) return "image/webp";
|
|
||||||
if (lower.endsWith(".jpg") || lower.endsWith(".jpeg")) return "image/jpeg";
|
|
||||||
return "application/octet-stream";
|
|
||||||
}
|
|
||||||
|
|
||||||
function safeDownloadName(value: string) {
|
|
||||||
const normalized = value.replace(/[\r\n"\\/]/g, "_").trim() || "artifact";
|
|
||||||
return normalized.slice(0, 180);
|
|
||||||
}
|
|
||||||
|
|
||||||
async function fetchArtifact(params: {
|
|
||||||
request: Request;
|
|
||||||
initialUrl: URL;
|
|
||||||
method: "GET" | "HEAD";
|
|
||||||
range: string | null;
|
|
||||||
token: string;
|
|
||||||
trustedOrigin: string;
|
|
||||||
}) {
|
|
||||||
let target = params.initialUrl;
|
|
||||||
for (let redirects = 0; redirects <= MAX_REDIRECTS; redirects += 1) {
|
|
||||||
if (!(await validateTargetUrl(target, params.trustedOrigin))) {
|
|
||||||
throw new Error("不允许访问该文件地址");
|
|
||||||
}
|
|
||||||
const headers = new Headers({ Accept: "*/*" });
|
|
||||||
if (params.range) headers.set("Range", params.range);
|
|
||||||
if (target.origin === params.trustedOrigin && params.token) {
|
|
||||||
headers.set("Authorization", `Bearer ${params.token}`);
|
|
||||||
}
|
|
||||||
const response = await fetch(target, {
|
|
||||||
method: params.method,
|
|
||||||
headers,
|
|
||||||
cache: "no-store",
|
|
||||||
redirect: "manual",
|
|
||||||
signal: params.request.signal,
|
|
||||||
});
|
|
||||||
if (![301, 302, 303, 307, 308].includes(response.status)) {
|
|
||||||
return { response, target };
|
|
||||||
}
|
|
||||||
if (redirects === MAX_REDIRECTS) throw new Error("文件重定向次数过多");
|
|
||||||
const location = response.headers.get("location");
|
|
||||||
if (!location) throw new Error("文件重定向地址无效");
|
|
||||||
target = new URL(location, target);
|
|
||||||
}
|
|
||||||
throw new Error("文件地址无效");
|
|
||||||
}
|
|
||||||
|
|
||||||
async function handleRequest(request: Request, method: "GET" | "HEAD") {
|
|
||||||
const session = await auth().catch(() => null);
|
|
||||||
if (!session?.user?.id) {
|
|
||||||
return NextResponse.json({ error: "未登录" }, { status: 401 });
|
|
||||||
}
|
|
||||||
|
|
||||||
const url = new URL(request.url);
|
|
||||||
const parsed = querySchema.safeParse({
|
|
||||||
chatId: url.searchParams.get("chatId"),
|
|
||||||
messageId: url.searchParams.get("messageId"),
|
|
||||||
index: url.searchParams.get("index"),
|
|
||||||
download: url.searchParams.get("download") || undefined,
|
|
||||||
});
|
|
||||||
if (!parsed.success) {
|
|
||||||
return NextResponse.json({ error: "请求参数不正确" }, { status: 400 });
|
|
||||||
}
|
|
||||||
|
|
||||||
const chat = await getChatById({ id: parsed.data.chatId }).catch(() => null);
|
|
||||||
if (!chat || chat.userId !== session.user.id) {
|
|
||||||
return NextResponse.json({ error: "Forbidden" }, { status: 403 });
|
|
||||||
}
|
|
||||||
const rows = await getMessageById({ id: parsed.data.messageId }).catch(
|
|
||||||
() => []
|
|
||||||
);
|
|
||||||
const message = rows.find((row) => row.chatId === parsed.data.chatId);
|
|
||||||
const metadata = normalizeClawdTaskMetadata(message?.videoMetadata);
|
|
||||||
const artifact = metadata?.artifacts?.[parsed.data.index];
|
|
||||||
if (!message || !artifact) {
|
|
||||||
return NextResponse.json({ error: "文件不存在" }, { status: 404 });
|
|
||||||
}
|
|
||||||
|
|
||||||
const setting = await getSystemSetting({ key: OPENCLAW_CHAT_CONFIG_KEY }).catch(
|
|
||||||
() => null
|
|
||||||
);
|
|
||||||
const config = normalizeOpenClawChatConfig(setting?.value);
|
|
||||||
let target: URL;
|
|
||||||
let trustedOrigin: string;
|
|
||||||
try {
|
|
||||||
target = new URL(artifact.downloadUrl);
|
|
||||||
trustedOrigin = new URL(config.baseUrl).origin;
|
|
||||||
} catch {
|
|
||||||
return NextResponse.json({ error: "文件地址无效" }, { status: 400 });
|
|
||||||
}
|
|
||||||
|
|
||||||
const range = normalizeRangeHeader(request.headers.get("range"));
|
|
||||||
if (range === "") {
|
|
||||||
return NextResponse.json({ error: "Range 请求格式无效" }, { status: 400 });
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
const { response, target: finalTarget } = await fetchArtifact({
|
|
||||||
request,
|
|
||||||
initialUrl: target,
|
|
||||||
method,
|
|
||||||
range,
|
|
||||||
token: config.token,
|
|
||||||
trustedOrigin,
|
|
||||||
});
|
|
||||||
if (!response.ok && response.status !== 206) {
|
|
||||||
return NextResponse.json(
|
|
||||||
{ error: `文件读取失败(HTTP ${response.status})` },
|
|
||||||
{ status: response.status >= 400 && response.status < 600 ? response.status : 502 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const headers = new Headers();
|
|
||||||
for (const name of RESPONSE_HEADERS) {
|
|
||||||
const value = response.headers.get(name);
|
|
||||||
if (value) headers.set(name, value);
|
|
||||||
}
|
|
||||||
headers.set(
|
|
||||||
"content-type",
|
|
||||||
response.headers.get("content-type") ||
|
|
||||||
inferContentType(artifact.filename || finalTarget.pathname, artifact.mimeType)
|
|
||||||
);
|
|
||||||
headers.set("cache-control", "private, max-age=3600, must-revalidate");
|
|
||||||
headers.set("x-content-type-options", "nosniff");
|
|
||||||
if (parsed.data.download === "1") {
|
|
||||||
const filename = safeDownloadName(artifact.filename || artifact.displayName);
|
|
||||||
headers.set(
|
|
||||||
"content-disposition",
|
|
||||||
`attachment; filename*=UTF-8''${encodeURIComponent(filename)}`
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return new Response(method === "HEAD" ? null : response.body, {
|
|
||||||
status: response.status,
|
|
||||||
headers,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
console.warn("[Clawd Artifact] Proxy failed:", {
|
|
||||||
chatId: parsed.data.chatId,
|
|
||||||
messageId: parsed.data.messageId,
|
|
||||||
artifactIndex: parsed.data.index,
|
|
||||||
error: error instanceof Error ? error.message : String(error),
|
|
||||||
});
|
|
||||||
return NextResponse.json(
|
|
||||||
{ error: error instanceof Error ? error.message : "文件读取失败" },
|
|
||||||
{ status: 502 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function GET(request: Request) {
|
|
||||||
return handleRequest(request, "GET");
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function HEAD(request: Request) {
|
|
||||||
return handleRequest(request, "HEAD");
|
|
||||||
}
|
|
||||||
@ -1,27 +1,11 @@
|
|||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import { auth } from "@/app/(auth)/auth";
|
import { auth } from "@/app/(auth)/auth";
|
||||||
import { getChatById, getSystemSetting } from "@/lib/db/queries";
|
import { getChatById } from "@/lib/db/queries";
|
||||||
import {
|
import {
|
||||||
buildClawdCancelKey,
|
buildClawdCancelKey,
|
||||||
cancelClawdRequest,
|
cancelClawdRequest,
|
||||||
} from "@/lib/openclaw-chat/cancel-registry";
|
} from "@/lib/openclaw-chat/cancel-registry";
|
||||||
import {
|
|
||||||
OPENCLAW_AUTH_REQUIRED_CODE,
|
|
||||||
OPENCLAW_AUTH_REQUIRED_MESSAGE,
|
|
||||||
} from "@/lib/openclaw-chat/errors";
|
|
||||||
import {
|
|
||||||
normalizeOpenClawChatConfig,
|
|
||||||
OPENCLAW_CHAT_CONFIG_KEY,
|
|
||||||
} from "@/lib/openclaw-chat/config";
|
|
||||||
import {
|
|
||||||
deletePersistedClawdTask,
|
|
||||||
getPersistedClawdTask,
|
|
||||||
} from "@/lib/openclaw-chat/task-store";
|
|
||||||
import {
|
|
||||||
getPersistedUpstreamTaskState,
|
|
||||||
stopOpenClawDurableTask,
|
|
||||||
} from "@/lib/openclaw-chat/upstream-task-client";
|
|
||||||
|
|
||||||
const cancelSchema = z.object({
|
const cancelSchema = z.object({
|
||||||
id: z.string().uuid(),
|
id: z.string().uuid(),
|
||||||
@ -31,13 +15,7 @@ const cancelSchema = z.object({
|
|||||||
export async function POST(request: Request) {
|
export async function POST(request: Request) {
|
||||||
const session = await auth().catch(() => null);
|
const session = await auth().catch(() => null);
|
||||||
if (!session?.user?.id) {
|
if (!session?.user?.id) {
|
||||||
return NextResponse.json(
|
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||||
{
|
|
||||||
error: OPENCLAW_AUTH_REQUIRED_MESSAGE,
|
|
||||||
code: OPENCLAW_AUTH_REQUIRED_CODE,
|
|
||||||
},
|
|
||||||
{ status: 401 }
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const parsed = cancelSchema.safeParse(await request.json().catch(() => null));
|
const parsed = cancelSchema.safeParse(await request.json().catch(() => null));
|
||||||
@ -50,43 +28,13 @@ export async function POST(request: Request) {
|
|||||||
return NextResponse.json({ error: "Forbidden" }, { status: 403 });
|
return NextResponse.json({ error: "Forbidden" }, { status: 403 });
|
||||||
}
|
}
|
||||||
|
|
||||||
const cancelKey = buildClawdCancelKey({
|
const active = cancelClawdRequest(
|
||||||
userId: session.user.id,
|
buildClawdCancelKey({
|
||||||
chatId: parsed.data.id,
|
userId: session.user.id,
|
||||||
userMessageId: parsed.data.userMessageId,
|
chatId: parsed.data.id,
|
||||||
});
|
userMessageId: parsed.data.userMessageId,
|
||||||
const task = await getPersistedClawdTask({
|
})
|
||||||
chatId: parsed.data.id,
|
);
|
||||||
userMessageId: parsed.data.userMessageId,
|
|
||||||
}).catch(() => null);
|
|
||||||
const active = cancelClawdRequest(cancelKey);
|
|
||||||
const upstreamTask = task
|
|
||||||
? getPersistedUpstreamTaskState(task.metadata)
|
|
||||||
: null;
|
|
||||||
if (upstreamTask) {
|
|
||||||
const setting = await getSystemSetting({
|
|
||||||
key: OPENCLAW_CHAT_CONFIG_KEY,
|
|
||||||
}).catch(() => null);
|
|
||||||
const config = normalizeOpenClawChatConfig(setting?.value);
|
|
||||||
if (config.enabled && config.baseUrl && config.token) {
|
|
||||||
await stopOpenClawDurableTask({ config, upstreamTask }).catch((error) => {
|
|
||||||
console.warn("[ClawdChat Cancel] Upstream stop deferred:", {
|
|
||||||
chatId: parsed.data.id,
|
|
||||||
taskId: upstreamTask.taskId,
|
|
||||||
error,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const deleted = await deletePersistedClawdTask({
|
return NextResponse.json({ canceled: true, active });
|
||||||
chatId: parsed.data.id,
|
|
||||||
userMessageId: parsed.data.userMessageId,
|
|
||||||
}).catch(() => []);
|
|
||||||
|
|
||||||
return NextResponse.json({
|
|
||||||
canceled: true,
|
|
||||||
active,
|
|
||||||
deleted: deleted.length > 0,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,141 +0,0 @@
|
|||||||
import JSZip from "jszip";
|
|
||||||
import { NextResponse } from "next/server";
|
|
||||||
import { auth } from "@/app/(auth)/auth";
|
|
||||||
import { getAllStorageConfigs } from "@/lib/db/queries";
|
|
||||||
import {
|
|
||||||
countOpenClawArchiveRootFolders,
|
|
||||||
sanitizeOpenClawArchiveEntryPath,
|
|
||||||
sanitizeOpenClawArchiveName,
|
|
||||||
} from "@/lib/openclaw-chat/folder-archive";
|
|
||||||
import {
|
|
||||||
OPENCLAW_CHAT_FOLDER_MAX_FILES,
|
|
||||||
} from "@/lib/openclaw-chat/media-attachments";
|
|
||||||
import { createLogger } from "@/lib/observability/logger";
|
|
||||||
import {
|
|
||||||
toDbS3UploadConfig,
|
|
||||||
uploadFileWithS3Config,
|
|
||||||
} from "@/lib/storage/service";
|
|
||||||
|
|
||||||
export const runtime = "nodejs";
|
|
||||||
export const maxDuration = 600;
|
|
||||||
|
|
||||||
const logger = createLogger("openclaw-folder-upload");
|
|
||||||
const LEGACY_FOLDER_UPLOAD_MAX_BYTES = 300 * 1024 * 1024;
|
|
||||||
|
|
||||||
function isUploadedFile(value: FormDataEntryValue): value is File {
|
|
||||||
return (
|
|
||||||
typeof value === "object" &&
|
|
||||||
value !== null &&
|
|
||||||
"arrayBuffer" in value &&
|
|
||||||
"name" in value &&
|
|
||||||
"size" in value
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
function toArrayBuffer(buffer: Buffer) {
|
|
||||||
return buffer.buffer.slice(
|
|
||||||
buffer.byteOffset,
|
|
||||||
buffer.byteOffset + buffer.byteLength
|
|
||||||
) as ArrayBuffer;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function POST(request: Request) {
|
|
||||||
const session = await auth();
|
|
||||||
if (!session?.user?.id) {
|
|
||||||
return NextResponse.json({ error: "请先登录" }, { status: 401 });
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
const formData = await request.formData();
|
|
||||||
const files = formData
|
|
||||||
.getAll("files")
|
|
||||||
.filter((entry): entry is File => isUploadedFile(entry));
|
|
||||||
const relativePaths = formData
|
|
||||||
.getAll("relativePaths")
|
|
||||||
.map((entry) => (typeof entry === "string" ? entry : ""));
|
|
||||||
|
|
||||||
if (files.length === 0) {
|
|
||||||
return NextResponse.json(
|
|
||||||
{ error: "所选文件夹中没有可上传文件" },
|
|
||||||
{ status: 400 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if (files.length > OPENCLAW_CHAT_FOLDER_MAX_FILES) {
|
|
||||||
return NextResponse.json(
|
|
||||||
{ error: `单次最多打包 ${OPENCLAW_CHAT_FOLDER_MAX_FILES} 个文件` },
|
|
||||||
{ status: 413 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const originalSize = files.reduce((sum, file) => sum + file.size, 0);
|
|
||||||
if (originalSize > LEGACY_FOLDER_UPLOAD_MAX_BYTES) {
|
|
||||||
return NextResponse.json(
|
|
||||||
{ error: "请刷新页面后使用支持 2GB 和断点续传的新文件夹上传方式" },
|
|
||||||
{ status: 413 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const archivePaths = files.map((file, index) =>
|
|
||||||
sanitizeOpenClawArchiveEntryPath(
|
|
||||||
relativePaths[index] || file.name,
|
|
||||||
file.name,
|
|
||||||
index
|
|
||||||
)
|
|
||||||
);
|
|
||||||
const zip = new JSZip();
|
|
||||||
for (const [index, file] of files.entries()) {
|
|
||||||
zip.file(archivePaths[index], await file.arrayBuffer(), {
|
|
||||||
date: new Date(file.lastModified || Date.now()),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const archiveBaseName = sanitizeOpenClawArchiveName(
|
|
||||||
String(formData.get("archiveName") || archivePaths[0]?.split("/")[0] || "")
|
|
||||||
);
|
|
||||||
const archiveName = `${archiveBaseName.replace(/\.zip$/i, "")}.zip`;
|
|
||||||
const archiveBuffer = await zip.generateAsync({
|
|
||||||
type: "nodebuffer",
|
|
||||||
compression: "DEFLATE",
|
|
||||||
compressionOptions: { level: 6 },
|
|
||||||
platform: "UNIX",
|
|
||||||
});
|
|
||||||
|
|
||||||
const storageConfigs = await getAllStorageConfigs();
|
|
||||||
const qiniuStorage = storageConfigs
|
|
||||||
.filter((config) => config.type === "qiniu-kodo" && config.isEnabled)
|
|
||||||
.sort((left, right) => Number(right.isDefault) - Number(left.isDefault))
|
|
||||||
.find((config) => toDbS3UploadConfig(config));
|
|
||||||
const qiniuConfig = toDbS3UploadConfig(qiniuStorage);
|
|
||||||
if (!qiniuConfig) {
|
|
||||||
return NextResponse.json(
|
|
||||||
{ error: "系统尚未配置可用的七牛云储存桶" },
|
|
||||||
{ status: 503 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const uploaded = await uploadFileWithS3Config(
|
|
||||||
archiveName,
|
|
||||||
toArrayBuffer(archiveBuffer),
|
|
||||||
qiniuConfig
|
|
||||||
);
|
|
||||||
if (!/^https?:\/\//i.test(uploaded.url)) {
|
|
||||||
throw new Error("七牛云未返回可访问的公网地址");
|
|
||||||
}
|
|
||||||
|
|
||||||
return NextResponse.json({
|
|
||||||
url: uploaded.url,
|
|
||||||
name: archiveName,
|
|
||||||
contentType: "application/zip",
|
|
||||||
size: archiveBuffer.byteLength,
|
|
||||||
originalSize,
|
|
||||||
fileCount: files.length,
|
|
||||||
folderCount: countOpenClawArchiveRootFolders(archivePaths),
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
logger.error("Failed to package and upload Claw folders", error);
|
|
||||||
return NextResponse.json(
|
|
||||||
{ error: error instanceof Error ? error.message : "文件夹上传失败" },
|
|
||||||
{ status: 500 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,10 +1,6 @@
|
|||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
import { auth } from "@/app/(auth)/auth";
|
import { auth } from "@/app/(auth)/auth";
|
||||||
import { getClawdChatsByUserId } from "@/lib/db/queries";
|
import { getClawdChatsByUserId } from "@/lib/db/queries";
|
||||||
import {
|
|
||||||
OPENCLAW_AUTH_REQUIRED_CODE,
|
|
||||||
OPENCLAW_AUTH_REQUIRED_MESSAGE,
|
|
||||||
} from "@/lib/openclaw-chat/errors";
|
|
||||||
|
|
||||||
export const dynamic = "force-dynamic";
|
export const dynamic = "force-dynamic";
|
||||||
export const revalidate = 0;
|
export const revalidate = 0;
|
||||||
@ -13,19 +9,13 @@ export async function GET(request: Request) {
|
|||||||
const session = await auth().catch(() => null);
|
const session = await auth().catch(() => null);
|
||||||
|
|
||||||
if (!session?.user?.id) {
|
if (!session?.user?.id) {
|
||||||
return NextResponse.json(
|
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||||
{
|
|
||||||
error: OPENCLAW_AUTH_REQUIRED_MESSAGE,
|
|
||||||
code: OPENCLAW_AUTH_REQUIRED_CODE,
|
|
||||||
},
|
|
||||||
{ status: 401 }
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const url = new URL(request.url);
|
const url = new URL(request.url);
|
||||||
const limit = Math.max(
|
const limit = Math.max(
|
||||||
1,
|
1,
|
||||||
Math.min(500, Math.round(Number(url.searchParams.get("limit") || 100)))
|
Math.min(100, Math.round(Number(url.searchParams.get("limit") || 50)))
|
||||||
);
|
);
|
||||||
|
|
||||||
const chats = await getClawdChatsByUserId({
|
const chats = await getClawdChatsByUserId({
|
||||||
|
|||||||
@ -1,60 +1,34 @@
|
|||||||
import { after, NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import { auth } from "@/app/(auth)/auth";
|
import { auth } from "@/app/(auth)/auth";
|
||||||
import {
|
import {
|
||||||
getChatById,
|
getChatById,
|
||||||
|
getMessagesByChatId,
|
||||||
getSystemSetting,
|
getSystemSetting,
|
||||||
getUserById,
|
getUserById,
|
||||||
saveChat,
|
saveChat,
|
||||||
|
saveMessages,
|
||||||
} from "@/lib/db/queries";
|
} from "@/lib/db/queries";
|
||||||
|
import {
|
||||||
|
normalizeOpenClawChatConfig,
|
||||||
|
OPENCLAW_CHAT_CONFIG_KEY,
|
||||||
|
} from "@/lib/openclaw-chat/config";
|
||||||
import {
|
import {
|
||||||
buildClawdCancelKey,
|
buildClawdCancelKey,
|
||||||
|
cancelClawdRequest,
|
||||||
clearClawdRequestCancellation,
|
clearClawdRequestCancellation,
|
||||||
isClawdRequestCanceled,
|
isClawdRequestCanceled,
|
||||||
registerActiveClawdRequest,
|
registerActiveClawdRequest,
|
||||||
unregisterActiveClawdRequest,
|
unregisterActiveClawdRequest,
|
||||||
} from "@/lib/openclaw-chat/cancel-registry";
|
} from "@/lib/openclaw-chat/cancel-registry";
|
||||||
import {
|
|
||||||
normalizeOpenClawChatConfig,
|
|
||||||
OPENCLAW_CHAT_CONFIG_KEY,
|
|
||||||
} from "@/lib/openclaw-chat/config";
|
|
||||||
import {
|
|
||||||
getOpenClawTaskFailureMessage,
|
|
||||||
OPENCLAW_AUTH_REQUIRED_CODE,
|
|
||||||
OPENCLAW_AUTH_REQUIRED_MESSAGE,
|
|
||||||
OPENCLAW_UPGRADE_RETRY_MESSAGE,
|
|
||||||
} from "@/lib/openclaw-chat/errors";
|
|
||||||
import {
|
|
||||||
normalizeOpenClawChatMediaAttachments,
|
|
||||||
OPENCLAW_CHAT_MEDIA_MAX_ATTACHMENTS,
|
|
||||||
type OpenClawChatMediaAttachment,
|
|
||||||
} from "@/lib/openclaw-chat/media-attachments";
|
|
||||||
import { extractOpenClawArtifacts } from "@/lib/openclaw-chat/artifacts";
|
|
||||||
import {
|
import {
|
||||||
buildOpenClawDisplayMessage,
|
buildOpenClawDisplayMessage,
|
||||||
buildOpenClawMessageWithSpreadsheetMarkdown,
|
buildOpenClawMessageWithSpreadsheetMarkdown,
|
||||||
buildOpenClawSpreadsheetMarkdown,
|
buildOpenClawSpreadsheetMarkdown,
|
||||||
} from "@/lib/openclaw-chat/spreadsheet-markdown";
|
} from "@/lib/openclaw-chat/spreadsheet-markdown";
|
||||||
import {
|
|
||||||
appendPersistedClawdTaskProgress,
|
|
||||||
getPersistedClawdTask,
|
|
||||||
markClawdTaskCanceled,
|
|
||||||
markClawdTaskCompleted,
|
|
||||||
markClawdTaskFailed,
|
|
||||||
markClawdTaskRunning,
|
|
||||||
persistPendingClawdTask,
|
|
||||||
} from "@/lib/openclaw-chat/task-store";
|
|
||||||
import {
|
|
||||||
OpenClawTaskDetachedError,
|
|
||||||
submitOpenClawDurableTask,
|
|
||||||
waitForOpenClawDurableTask,
|
|
||||||
} from "@/lib/openclaw-chat/upstream-task-client";
|
|
||||||
import { generateUUID } from "@/lib/utils";
|
import { generateUUID } from "@/lib/utils";
|
||||||
|
|
||||||
const DEFAULT_PROCESSING_HINT =
|
const CLAW_UPGRADE_RETRY_MESSAGE = "claw正在升级更新稍等5分钟再试";
|
||||||
"已收到,这个任务稍微繁重一些,处理时间可能会长一点,请耐心等候。";
|
|
||||||
|
|
||||||
export const maxDuration = 1800;
|
|
||||||
|
|
||||||
const requestSchema = z.object({
|
const requestSchema = z.object({
|
||||||
id: z.string().uuid(),
|
id: z.string().uuid(),
|
||||||
@ -62,63 +36,12 @@ const requestSchema = z.object({
|
|||||||
displayMessage: z.string().trim().max(8000).optional(),
|
displayMessage: z.string().trim().max(8000).optional(),
|
||||||
userMessageId: z.string().uuid().optional(),
|
userMessageId: z.string().uuid().optional(),
|
||||||
metadata: z.record(z.unknown()).optional(),
|
metadata: z.record(z.unknown()).optional(),
|
||||||
sensitiveInput: z
|
|
||||||
.discriminatedUnion("type", [
|
|
||||||
z.object({
|
|
||||||
type: z.literal("taobao_credentials"),
|
|
||||||
username: z.string().min(1).max(160),
|
|
||||||
password: z.string().min(1).max(256),
|
|
||||||
}),
|
|
||||||
z.object({
|
|
||||||
type: z.literal("taobao_verification"),
|
|
||||||
verification_code: z.string().min(4).max(12),
|
|
||||||
}),
|
|
||||||
])
|
|
||||||
.optional(),
|
|
||||||
attachments: z
|
|
||||||
.array(z.unknown())
|
|
||||||
.max(OPENCLAW_CHAT_MEDIA_MAX_ATTACHMENTS)
|
|
||||||
.optional()
|
|
||||||
.default([]),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
type ParsedClawdChatRequest = Omit<
|
type ParsedClawdChatRequest = z.infer<typeof requestSchema> & {
|
||||||
z.infer<typeof requestSchema>,
|
|
||||||
"attachments"
|
|
||||||
> & {
|
|
||||||
attachments: OpenClawChatMediaAttachment[];
|
|
||||||
tableFiles: File[];
|
tableFiles: File[];
|
||||||
};
|
};
|
||||||
|
|
||||||
type ClawdSpreadsheetAttachment = {
|
|
||||||
name: string;
|
|
||||||
size: number;
|
|
||||||
contentType: string;
|
|
||||||
sheetCount: number;
|
|
||||||
rowCount: number;
|
|
||||||
truncated: boolean;
|
|
||||||
};
|
|
||||||
|
|
||||||
type ExecuteClawdTaskParams = {
|
|
||||||
assistantMessageId: string;
|
|
||||||
cancelKey: string;
|
|
||||||
chatId: string;
|
|
||||||
config: { baseUrl: string; token: string; timeoutMs: number };
|
|
||||||
conversationId: string;
|
|
||||||
departmentName: string;
|
|
||||||
metadata: Record<string, unknown> | undefined;
|
|
||||||
sensitiveInput?: z.infer<typeof requestSchema>["sensitiveInput"];
|
|
||||||
mediaAttachments: OpenClawChatMediaAttachment[];
|
|
||||||
skillTagId: string;
|
|
||||||
skillTagName: string;
|
|
||||||
spreadsheetFiles: unknown[];
|
|
||||||
spreadsheetMarkdownTruncated: boolean;
|
|
||||||
upstreamMessage: string;
|
|
||||||
userId: string;
|
|
||||||
userMessageId: string;
|
|
||||||
userName: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
function buildChatTitle(message: string) {
|
function buildChatTitle(message: string) {
|
||||||
const normalized = message.replace(/\s+/g, " ").trim();
|
const normalized = message.replace(/\s+/g, " ").trim();
|
||||||
if (!normalized) return "Claw 对话";
|
if (!normalized) return "Claw 对话";
|
||||||
@ -134,15 +57,17 @@ function getOpenClawUserName(user: {
|
|||||||
name?: string | null;
|
name?: string | null;
|
||||||
email?: string | null;
|
email?: string | null;
|
||||||
}) {
|
}) {
|
||||||
return String(user.name || user.email || user.id).trim();
|
const name = String(user.name || "").trim();
|
||||||
|
if (name) return name;
|
||||||
|
|
||||||
|
const email = String(user.email || "").trim();
|
||||||
|
if (email) return email;
|
||||||
|
|
||||||
|
return user.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getMetadataString(
|
function getOpenClawDepartmentName(department?: string | null) {
|
||||||
metadata: Record<string, unknown> | undefined,
|
return String(department || "").trim();
|
||||||
key: string
|
|
||||||
) {
|
|
||||||
const value = metadata?.[key];
|
|
||||||
return typeof value === "string" ? value.trim() : "";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function isRequestFile(value: FormDataEntryValue): value is File {
|
function isRequestFile(value: FormDataEntryValue): value is File {
|
||||||
@ -161,39 +86,25 @@ async function parseClawdChatRequest(
|
|||||||
const contentType = request.headers.get("content-type") || "";
|
const contentType = request.headers.get("content-type") || "";
|
||||||
if (!contentType.includes("multipart/form-data")) {
|
if (!contentType.includes("multipart/form-data")) {
|
||||||
const parsed = requestSchema.safeParse(await request.json().catch(() => null));
|
const parsed = requestSchema.safeParse(await request.json().catch(() => null));
|
||||||
if (!parsed.success) return null;
|
return parsed.success ? { ...parsed.data, tableFiles: [] } : null;
|
||||||
const attachments = normalizeOpenClawChatMediaAttachments(
|
|
||||||
parsed.data.attachments
|
|
||||||
);
|
|
||||||
if (attachments.length !== parsed.data.attachments.length) return null;
|
|
||||||
return { ...parsed.data, attachments, tableFiles: [] };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const formData = await request.formData().catch(() => null);
|
const formData = await request.formData().catch(() => null);
|
||||||
if (!formData) return null;
|
if (!formData) return null;
|
||||||
|
|
||||||
let metadata: Record<string, unknown> | undefined;
|
|
||||||
let attachments: unknown[] = [];
|
|
||||||
let sensitiveInput: unknown;
|
|
||||||
const metadataValue = formData.get("metadata");
|
const metadataValue = formData.get("metadata");
|
||||||
const attachmentsValue = formData.get("attachments");
|
let metadata: Record<string, unknown> | undefined;
|
||||||
const sensitiveInputValue = formData.get("sensitiveInput");
|
if (typeof metadataValue === "string" && metadataValue.trim()) {
|
||||||
try {
|
try {
|
||||||
if (typeof metadataValue === "string" && metadataValue.trim()) {
|
|
||||||
const parsedMetadata = JSON.parse(metadataValue);
|
const parsedMetadata = JSON.parse(metadataValue);
|
||||||
if (!parsedMetadata || typeof parsedMetadata !== "object") return null;
|
if (parsedMetadata && typeof parsedMetadata === "object") {
|
||||||
metadata = parsedMetadata as Record<string, unknown>;
|
metadata = parsedMetadata as Record<string, unknown>;
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
if (typeof attachmentsValue === "string" && attachmentsValue.trim()) {
|
|
||||||
const parsedAttachments = JSON.parse(attachmentsValue);
|
|
||||||
if (!Array.isArray(parsedAttachments)) return null;
|
|
||||||
attachments = parsedAttachments;
|
|
||||||
}
|
|
||||||
if (typeof sensitiveInputValue === "string" && sensitiveInputValue.trim()) {
|
|
||||||
sensitiveInput = JSON.parse(sensitiveInputValue);
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const parsed = requestSchema.safeParse({
|
const parsed = requestSchema.safeParse({
|
||||||
@ -202,279 +113,68 @@ async function parseClawdChatRequest(
|
|||||||
displayMessage: formData.get("displayMessage") || undefined,
|
displayMessage: formData.get("displayMessage") || undefined,
|
||||||
userMessageId: formData.get("userMessageId") || undefined,
|
userMessageId: formData.get("userMessageId") || undefined,
|
||||||
metadata,
|
metadata,
|
||||||
attachments,
|
|
||||||
sensitiveInput,
|
|
||||||
});
|
});
|
||||||
if (!parsed.success) return null;
|
if (!parsed.success) return null;
|
||||||
const normalizedAttachments = normalizeOpenClawChatMediaAttachments(
|
|
||||||
parsed.data.attachments
|
|
||||||
);
|
|
||||||
if (normalizedAttachments.length !== parsed.data.attachments.length) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
const tableFiles = formData
|
||||||
...parsed.data,
|
.getAll("tableFiles")
|
||||||
attachments: normalizedAttachments,
|
.filter((entry): entry is File => isRequestFile(entry) && entry.size > 0);
|
||||||
tableFiles: formData
|
|
||||||
.getAll("tableFiles")
|
return { ...parsed.data, tableFiles };
|
||||||
.filter((entry): entry is File => isRequestFile(entry) && entry.size > 0),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function readOpenClawConfig() {
|
async function readOpenClawConfig() {
|
||||||
const setting = await getSystemSetting({
|
const setting = await getSystemSetting({ key: OPENCLAW_CHAT_CONFIG_KEY }).catch(
|
||||||
key: OPENCLAW_CHAT_CONFIG_KEY,
|
() => null
|
||||||
}).catch(() => null);
|
);
|
||||||
return normalizeOpenClawChatConfig(setting?.value);
|
return normalizeOpenClawChatConfig(setting?.value);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getAnswer(payload: unknown) {
|
function getOpenClawErrorCode(payload: unknown) {
|
||||||
if (!payload || typeof payload !== "object" || Array.isArray(payload)) {
|
if (!payload || typeof payload !== "object") return "";
|
||||||
return "";
|
const raw = payload as Record<string, unknown>;
|
||||||
}
|
const nestedError =
|
||||||
return String((payload as Record<string, unknown>).answer || "").trim();
|
raw.error && typeof raw.error === "object"
|
||||||
|
? (raw.error as Record<string, unknown>)
|
||||||
|
: null;
|
||||||
|
const code = nestedError?.code || raw.code;
|
||||||
|
return typeof code === "string" ? code.trim() : "";
|
||||||
}
|
}
|
||||||
|
|
||||||
function getRequestId(payload: unknown) {
|
function getMetadataString(
|
||||||
if (!payload || typeof payload !== "object" || Array.isArray(payload)) {
|
metadata: Record<string, unknown> | undefined,
|
||||||
return null;
|
key: string
|
||||||
}
|
) {
|
||||||
const value = (payload as Record<string, unknown>).request_id;
|
const value = metadata?.[key];
|
||||||
return value ? String(value) : null;
|
return typeof value === "string" ? value.trim() : "";
|
||||||
}
|
}
|
||||||
|
|
||||||
async function executeClawdTask(params: ExecuteClawdTaskParams) {
|
function createCanceledResponse() {
|
||||||
const controller = new AbortController();
|
return NextResponse.json({ canceled: true }, { status: 499 });
|
||||||
registerActiveClawdRequest(
|
|
||||||
params.cancelKey,
|
|
||||||
controller,
|
|
||||||
Math.max(params.config.timeoutMs, 10 * 60 * 1000)
|
|
||||||
);
|
|
||||||
|
|
||||||
try {
|
|
||||||
if (isClawdRequestCanceled(params.cancelKey)) return;
|
|
||||||
await markClawdTaskRunning({
|
|
||||||
chatId: params.chatId,
|
|
||||||
assistantMessageId: params.assistantMessageId,
|
|
||||||
});
|
|
||||||
|
|
||||||
const upstreamBody: Record<string, unknown> = {
|
|
||||||
conversation_id: params.conversationId,
|
|
||||||
user_id: params.userId,
|
|
||||||
user_name: params.userName,
|
|
||||||
username: params.userName,
|
|
||||||
department: params.departmentName,
|
|
||||||
department_name: params.departmentName,
|
|
||||||
user_department: params.departmentName,
|
|
||||||
skill_tag_id: params.skillTagId || undefined,
|
|
||||||
skill_tag_name: params.skillTagName || undefined,
|
|
||||||
message: params.upstreamMessage,
|
|
||||||
attachments:
|
|
||||||
params.mediaAttachments.length > 0
|
|
||||||
? params.mediaAttachments
|
|
||||||
: undefined,
|
|
||||||
metadata: {
|
|
||||||
source: "ai_chatbot_canvas",
|
|
||||||
page: "/canvas",
|
|
||||||
tenant: "default",
|
|
||||||
chat_id: params.chatId,
|
|
||||||
user_id: params.userId,
|
|
||||||
user_name: params.userName,
|
|
||||||
username: params.userName,
|
|
||||||
department: params.departmentName,
|
|
||||||
department_name: params.departmentName,
|
|
||||||
user_department: params.departmentName,
|
|
||||||
skill_tag_id: params.skillTagId || undefined,
|
|
||||||
skill_tag_name: params.skillTagName || undefined,
|
|
||||||
media_attachments:
|
|
||||||
params.mediaAttachments.length > 0
|
|
||||||
? params.mediaAttachments
|
|
||||||
: undefined,
|
|
||||||
spreadsheet_files:
|
|
||||||
params.spreadsheetFiles.length > 0
|
|
||||||
? params.spreadsheetFiles
|
|
||||||
: undefined,
|
|
||||||
spreadsheet_markdown_truncated:
|
|
||||||
params.spreadsheetMarkdownTruncated || undefined,
|
|
||||||
...params.metadata,
|
|
||||||
},
|
|
||||||
stream_progress: !params.sensitiveInput,
|
|
||||||
sensitive_input: params.sensitiveInput,
|
|
||||||
};
|
|
||||||
|
|
||||||
if (params.sensitiveInput) {
|
|
||||||
const timeout = setTimeout(
|
|
||||||
() =>
|
|
||||||
controller.abort(
|
|
||||||
new DOMException("OpenClaw request timed out", "TimeoutError")
|
|
||||||
),
|
|
||||||
params.config.timeoutMs
|
|
||||||
);
|
|
||||||
try {
|
|
||||||
const response = await fetch(`${params.config.baseUrl}/api/chat`, {
|
|
||||||
method: "POST",
|
|
||||||
headers: {
|
|
||||||
Authorization: `Bearer ${params.config.token}`,
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
Accept: "application/json",
|
|
||||||
},
|
|
||||||
body: JSON.stringify(upstreamBody),
|
|
||||||
signal: controller.signal,
|
|
||||||
cache: "no-store",
|
|
||||||
});
|
|
||||||
const payload = (await response.json().catch(() => null)) as Record<
|
|
||||||
string,
|
|
||||||
unknown
|
|
||||||
> | null;
|
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error(`OpenClaw request failed: ${response.status}`);
|
|
||||||
}
|
|
||||||
if (!payload) throw new Error("OpenClaw returned an invalid response");
|
|
||||||
const answer = getAnswer(payload);
|
|
||||||
if (!answer) throw new Error("OpenClaw returned an empty answer");
|
|
||||||
await markClawdTaskCompleted({
|
|
||||||
chatId: params.chatId,
|
|
||||||
assistantMessageId: params.assistantMessageId,
|
|
||||||
answer,
|
|
||||||
requestId: getRequestId(payload),
|
|
||||||
artifacts: extractOpenClawArtifacts(payload, {
|
|
||||||
baseUrl: params.config.baseUrl,
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
clearClawdRequestCancellation(params.cancelKey);
|
|
||||||
return;
|
|
||||||
} finally {
|
|
||||||
clearTimeout(timeout);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const target = {
|
|
||||||
chatId: params.chatId,
|
|
||||||
assistantMessageId: params.assistantMessageId,
|
|
||||||
};
|
|
||||||
const upstreamTask = await submitOpenClawDurableTask({
|
|
||||||
config: params.config,
|
|
||||||
body: upstreamBody,
|
|
||||||
idempotencyKey: `${params.userId}:${params.chatId}:${params.userMessageId}`,
|
|
||||||
target,
|
|
||||||
signal: controller.signal,
|
|
||||||
});
|
|
||||||
const outcome = await waitForOpenClawDurableTask({
|
|
||||||
config: params.config,
|
|
||||||
upstreamTask,
|
|
||||||
target,
|
|
||||||
deadlineAt: Date.now() + params.config.timeoutMs,
|
|
||||||
signal: controller.signal,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (isClawdRequestCanceled(params.cancelKey)) return;
|
|
||||||
if (outcome.status === "completed") {
|
|
||||||
await markClawdTaskCompleted({
|
|
||||||
...target,
|
|
||||||
answer: outcome.answer,
|
|
||||||
requestId: outcome.requestId,
|
|
||||||
artifacts: outcome.artifacts,
|
|
||||||
});
|
|
||||||
} else if (outcome.status === "canceled") {
|
|
||||||
await markClawdTaskCanceled({
|
|
||||||
...target,
|
|
||||||
message: outcome.error,
|
|
||||||
});
|
|
||||||
} else if (outcome.status === "failed") {
|
|
||||||
await markClawdTaskFailed({
|
|
||||||
...target,
|
|
||||||
error: outcome.error,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
clearClawdRequestCancellation(params.cancelKey);
|
|
||||||
} catch (error) {
|
|
||||||
if (isClawdRequestCanceled(params.cancelKey)) return;
|
|
||||||
if (error instanceof OpenClawTaskDetachedError) {
|
|
||||||
await appendPersistedClawdTaskProgress({
|
|
||||||
chatId: params.chatId,
|
|
||||||
assistantMessageId: params.assistantMessageId,
|
|
||||||
progress: {
|
|
||||||
step_code: "task.detached",
|
|
||||||
step_state: "waiting",
|
|
||||||
message: "任务仍在 OpenClaw 后台执行,打开原对话后会自动续接结果",
|
|
||||||
display: "任务仍在 OpenClaw 后台执行,打开原对话后会自动续接结果",
|
|
||||||
},
|
|
||||||
}).catch(() => null);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
console.error("[ClawdChat API] Durable task failed:", error);
|
|
||||||
await markClawdTaskFailed({
|
|
||||||
chatId: params.chatId,
|
|
||||||
assistantMessageId: params.assistantMessageId,
|
|
||||||
error: getOpenClawTaskFailureMessage(error),
|
|
||||||
}).catch(() => null);
|
|
||||||
} finally {
|
|
||||||
if (params.sensitiveInput?.type === "taobao_credentials") {
|
|
||||||
params.sensitiveInput.username = "";
|
|
||||||
params.sensitiveInput.password = "";
|
|
||||||
} else if (params.sensitiveInput?.type === "taobao_verification") {
|
|
||||||
params.sensitiveInput.verification_code = "";
|
|
||||||
}
|
|
||||||
params.sensitiveInput = undefined;
|
|
||||||
unregisterActiveClawdRequest(params.cancelKey);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function buildTaskResponse(params: {
|
|
||||||
chatId: string;
|
|
||||||
userMessageId: string;
|
|
||||||
assistantMessageId: string;
|
|
||||||
status: string;
|
|
||||||
processingHint: string;
|
|
||||||
}) {
|
|
||||||
const statusUrl = `/api/canvas/clawd-chat/status?id=${encodeURIComponent(
|
|
||||||
params.chatId
|
|
||||||
)}&userMessageId=${encodeURIComponent(params.userMessageId)}`;
|
|
||||||
return {
|
|
||||||
accepted: true,
|
|
||||||
task: {
|
|
||||||
chatId: params.chatId,
|
|
||||||
userMessageId: params.userMessageId,
|
|
||||||
assistantMessageId: params.assistantMessageId,
|
|
||||||
status: params.status,
|
|
||||||
processingHint: params.processingHint,
|
|
||||||
statusUrl,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function POST(request: Request) {
|
export async function POST(request: Request) {
|
||||||
const session = await auth().catch(() => null);
|
const session = await auth().catch(() => null);
|
||||||
|
|
||||||
if (!session?.user?.id) {
|
if (!session?.user?.id) {
|
||||||
return NextResponse.json(
|
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||||
{
|
|
||||||
error: OPENCLAW_AUTH_REQUIRED_MESSAGE,
|
|
||||||
code: OPENCLAW_AUTH_REQUIRED_CODE,
|
|
||||||
},
|
|
||||||
{ status: 401 }
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const parsed = await parseClawdChatRequest(request);
|
const parsed = await parseClawdChatRequest(request);
|
||||||
if (!parsed) {
|
if (!parsed) {
|
||||||
return NextResponse.json({ error: "请求参数不正确" }, { status: 400 });
|
return NextResponse.json({ error: "请求参数不正确" }, { status: 400 });
|
||||||
}
|
}
|
||||||
if (
|
if (!parsed.message && parsed.tableFiles.length === 0) {
|
||||||
!parsed.message &&
|
return NextResponse.json({ error: "请输入问题或上传表格" }, { status: 400 });
|
||||||
parsed.tableFiles.length === 0 &&
|
|
||||||
parsed.attachments.length === 0
|
|
||||||
) {
|
|
||||||
return NextResponse.json(
|
|
||||||
{ error: "请输入问题或上传附件" },
|
|
||||||
{ status: 400 }
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const config = await readOpenClawConfig();
|
const config = await readOpenClawConfig();
|
||||||
if (!config.enabled || !config.baseUrl || !config.token) {
|
if (!config.enabled) {
|
||||||
|
return NextResponse.json({ error: CLAW_UPGRADE_RETRY_MESSAGE }, { status: 503 });
|
||||||
|
}
|
||||||
|
if (!config.baseUrl || !config.token) {
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ error: OPENCLAW_UPGRADE_RETRY_MESSAGE },
|
{ error: CLAW_UPGRADE_RETRY_MESSAGE },
|
||||||
{ status: 503 }
|
{ status: 503 }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -485,22 +185,13 @@ export async function POST(request: Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const userMessageId = parsed.userMessageId || generateUUID();
|
const userMessageId = parsed.userMessageId || generateUUID();
|
||||||
const existingTask = await getPersistedClawdTask({
|
const cancelKey = buildClawdCancelKey({
|
||||||
|
userId: session.user.id,
|
||||||
chatId: parsed.id,
|
chatId: parsed.id,
|
||||||
userMessageId,
|
userMessageId,
|
||||||
}).catch(() => null);
|
});
|
||||||
if (existingTask) {
|
if (isClawdRequestCanceled(cancelKey)) {
|
||||||
return NextResponse.json(
|
return createCanceledResponse();
|
||||||
buildTaskResponse({
|
|
||||||
chatId: parsed.id,
|
|
||||||
userMessageId,
|
|
||||||
assistantMessageId: existingTask.metadata.assistantMessageId,
|
|
||||||
status: existingTask.metadata.status,
|
|
||||||
processingHint:
|
|
||||||
existingTask.metadata.processingHint || DEFAULT_PROCESSING_HINT,
|
|
||||||
}),
|
|
||||||
{ status: 202, headers: { "Retry-After": "2" } }
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let spreadsheetMarkdownResult: Awaited<
|
let spreadsheetMarkdownResult: Awaited<
|
||||||
@ -530,23 +221,10 @@ export async function POST(request: Request) {
|
|||||||
message: parsed.message,
|
message: parsed.message,
|
||||||
spreadsheetMarkdown: spreadsheetMarkdownResult?.markdown || "",
|
spreadsheetMarkdown: spreadsheetMarkdownResult?.markdown || "",
|
||||||
});
|
});
|
||||||
const spreadsheetAttachments: ClawdSpreadsheetAttachment[] = (
|
|
||||||
spreadsheetMarkdownResult?.files || []
|
if (isClawdRequestCanceled(cancelKey)) {
|
||||||
).map((summary, index) => {
|
return createCanceledResponse();
|
||||||
const sourceFile = parsed.tableFiles[index];
|
}
|
||||||
return {
|
|
||||||
name: summary.fileName,
|
|
||||||
size: sourceFile?.size || 0,
|
|
||||||
contentType:
|
|
||||||
sourceFile?.type ||
|
|
||||||
(summary.fileName.toLowerCase().endsWith(".xlsx")
|
|
||||||
? "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
|
||||||
: "text/csv"),
|
|
||||||
sheetCount: summary.sheetCount,
|
|
||||||
rowCount: summary.rowCount,
|
|
||||||
truncated: summary.truncated,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!existingChat) {
|
if (!existingChat) {
|
||||||
await saveChat({
|
await saveChat({
|
||||||
@ -558,107 +236,220 @@ export async function POST(request: Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const assistantMessageId = generateUUID();
|
const assistantMessageId = generateUUID();
|
||||||
const skillTagId = getMetadataString(parsed.metadata, "skillTagId");
|
const conversationId = createConversationId(session.user.id, parsed.id);
|
||||||
const skillTagName = getMetadataString(parsed.metadata, "skillTagName");
|
const userProfile = await getUserById({ id: session.user.id }).catch(() => null);
|
||||||
const processingHint =
|
const openClawUserName = getOpenClawUserName({
|
||||||
getMetadataString(parsed.metadata, "processingHint") ||
|
|
||||||
DEFAULT_PROCESSING_HINT;
|
|
||||||
const messageAttachments = parsed.attachments.map((attachment) => ({
|
|
||||||
name:
|
|
||||||
attachment.name ||
|
|
||||||
(attachment.type === "video"
|
|
||||||
? "视频附件"
|
|
||||||
: attachment.type === "file"
|
|
||||||
? "文件附件"
|
|
||||||
: "图片附件"),
|
|
||||||
url: attachment.url,
|
|
||||||
contentType:
|
|
||||||
attachment.content_type ||
|
|
||||||
(attachment.type === "video"
|
|
||||||
? "video/mp4"
|
|
||||||
: attachment.type === "file"
|
|
||||||
? "application/octet-stream"
|
|
||||||
: "image/jpeg"),
|
|
||||||
}));
|
|
||||||
|
|
||||||
const persistedTask = await persistPendingClawdTask({
|
|
||||||
chatId: parsed.id,
|
|
||||||
userMessageId,
|
|
||||||
assistantMessageId,
|
|
||||||
displayMessage,
|
|
||||||
attachments: messageAttachments,
|
|
||||||
spreadsheetAttachments,
|
|
||||||
skillTagId,
|
|
||||||
skillTagName,
|
|
||||||
processingHint,
|
|
||||||
});
|
|
||||||
if (
|
|
||||||
persistedTask &&
|
|
||||||
persistedTask.assistantMessageId !== assistantMessageId
|
|
||||||
) {
|
|
||||||
return NextResponse.json(
|
|
||||||
buildTaskResponse({
|
|
||||||
chatId: parsed.id,
|
|
||||||
userMessageId,
|
|
||||||
assistantMessageId: persistedTask.assistantMessageId,
|
|
||||||
status: persistedTask.status,
|
|
||||||
processingHint:
|
|
||||||
persistedTask.processingHint || DEFAULT_PROCESSING_HINT,
|
|
||||||
}),
|
|
||||||
{ status: 202, headers: { "Retry-After": "2" } }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const userProfile = await getUserById({ id: session.user.id }).catch(
|
|
||||||
() => null
|
|
||||||
);
|
|
||||||
const userName = getOpenClawUserName({
|
|
||||||
id: session.user.id,
|
id: session.user.id,
|
||||||
name: userProfile?.name ?? session.user.name,
|
name: userProfile?.name ?? session.user.name,
|
||||||
email: userProfile?.email ?? session.user.email,
|
email: userProfile?.email ?? session.user.email,
|
||||||
});
|
});
|
||||||
const cancelKey = buildClawdCancelKey({
|
const openClawDepartmentName = getOpenClawDepartmentName(userProfile?.department);
|
||||||
userId: session.user.id,
|
const skillTagId = getMetadataString(parsed.metadata, "skillTagId");
|
||||||
chatId: parsed.id,
|
const skillTagName = getMetadataString(parsed.metadata, "skillTagName");
|
||||||
userMessageId,
|
const endpoint = `${config.baseUrl}/api/chat`;
|
||||||
});
|
|
||||||
|
|
||||||
after(async () => {
|
let upstreamPayload: unknown = null;
|
||||||
await executeClawdTask({
|
let upstreamStatus = 502;
|
||||||
assistantMessageId,
|
|
||||||
cancelKey,
|
if (isClawdRequestCanceled(cancelKey)) {
|
||||||
chatId: parsed.id,
|
return createCanceledResponse();
|
||||||
config,
|
}
|
||||||
conversationId: createConversationId(session.user.id, parsed.id),
|
|
||||||
departmentName: String(userProfile?.department || "").trim(),
|
try {
|
||||||
metadata: parsed.metadata,
|
const controller = new AbortController();
|
||||||
sensitiveInput: parsed.sensitiveInput,
|
registerActiveClawdRequest(cancelKey, controller, config.timeoutMs);
|
||||||
mediaAttachments: parsed.attachments,
|
const timeout = setTimeout(() => controller.abort(), config.timeoutMs);
|
||||||
skillTagId,
|
const handleRequestAbort = () => {
|
||||||
skillTagName,
|
cancelClawdRequest(cancelKey);
|
||||||
spreadsheetFiles: spreadsheetMarkdownResult?.files || [],
|
};
|
||||||
spreadsheetMarkdownTruncated:
|
request.signal.addEventListener("abort", handleRequestAbort, { once: true });
|
||||||
spreadsheetMarkdownResult?.truncated || false,
|
const response = await fetch(endpoint, {
|
||||||
upstreamMessage,
|
method: "POST",
|
||||||
userId: session.user.id,
|
headers: {
|
||||||
userMessageId,
|
Authorization: `Bearer ${config.token}`,
|
||||||
userName,
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
conversation_id: conversationId,
|
||||||
|
user_id: session.user.id,
|
||||||
|
user_name: openClawUserName,
|
||||||
|
username: openClawUserName,
|
||||||
|
department: openClawDepartmentName,
|
||||||
|
department_name: openClawDepartmentName,
|
||||||
|
user_department: openClawDepartmentName,
|
||||||
|
skill_tag_id: skillTagId || undefined,
|
||||||
|
skill_tag_name: skillTagName || undefined,
|
||||||
|
message: upstreamMessage,
|
||||||
|
metadata: {
|
||||||
|
source: "ai_chatbot_canvas",
|
||||||
|
page: "/canvas",
|
||||||
|
tenant: "default",
|
||||||
|
chat_id: parsed.id,
|
||||||
|
user_id: session.user.id,
|
||||||
|
user_name: openClawUserName,
|
||||||
|
username: openClawUserName,
|
||||||
|
department: openClawDepartmentName,
|
||||||
|
department_name: openClawDepartmentName,
|
||||||
|
user_department: openClawDepartmentName,
|
||||||
|
skill_tag_id: skillTagId || undefined,
|
||||||
|
skill_tag_name: skillTagName || undefined,
|
||||||
|
spreadsheet_files: spreadsheetMarkdownResult?.files || undefined,
|
||||||
|
spreadsheet_markdown_truncated:
|
||||||
|
spreadsheetMarkdownResult?.truncated || undefined,
|
||||||
|
...parsed.metadata,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
signal: controller.signal,
|
||||||
|
}).finally(() => {
|
||||||
|
clearTimeout(timeout);
|
||||||
|
request.signal.removeEventListener("abort", handleRequestAbort);
|
||||||
|
unregisterActiveClawdRequest(cancelKey);
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
const responseBody = buildTaskResponse({
|
upstreamStatus = response.status;
|
||||||
|
upstreamPayload = await response.json().catch(() => null);
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const errorCode = getOpenClawErrorCode(upstreamPayload);
|
||||||
|
console.error("[ClawdChat API] OpenClaw upstream error:", {
|
||||||
|
status: response.status,
|
||||||
|
code: errorCode || undefined,
|
||||||
|
payload: upstreamPayload,
|
||||||
|
});
|
||||||
|
return NextResponse.json(
|
||||||
|
{
|
||||||
|
error: CLAW_UPGRADE_RETRY_MESSAGE,
|
||||||
|
code: errorCode || undefined,
|
||||||
|
},
|
||||||
|
{ status: response.status }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
unregisterActiveClawdRequest(cancelKey);
|
||||||
|
const errorName =
|
||||||
|
error && typeof error === "object" && "name" in error
|
||||||
|
? String((error as { name?: unknown }).name || "")
|
||||||
|
: "";
|
||||||
|
const isAbortError = errorName === "AbortError";
|
||||||
|
if (isClawdRequestCanceled(cancelKey)) {
|
||||||
|
console.info("[ClawdChat API] OpenClaw request canceled:", {
|
||||||
|
chatId: parsed.id,
|
||||||
|
userMessageId,
|
||||||
|
});
|
||||||
|
return createCanceledResponse();
|
||||||
|
}
|
||||||
|
console.error("[ClawdChat API] OpenClaw request failed:", error);
|
||||||
|
return NextResponse.json(
|
||||||
|
{
|
||||||
|
error: CLAW_UPGRADE_RETRY_MESSAGE,
|
||||||
|
},
|
||||||
|
{ status: isAbortError ? 504 : upstreamStatus }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const answer =
|
||||||
|
upstreamPayload && typeof upstreamPayload === "object"
|
||||||
|
? String((upstreamPayload as Record<string, unknown>).answer || "").trim()
|
||||||
|
: "";
|
||||||
|
|
||||||
|
if (!answer) {
|
||||||
|
console.error("[ClawdChat API] OpenClaw empty answer:", upstreamPayload);
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: CLAW_UPGRADE_RETRY_MESSAGE },
|
||||||
|
{ status: 502 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isClawdRequestCanceled(cancelKey)) {
|
||||||
|
return createCanceledResponse();
|
||||||
|
}
|
||||||
|
|
||||||
|
const existingMessages = await getMessagesByChatId({ id: parsed.id }).catch(
|
||||||
|
() => []
|
||||||
|
);
|
||||||
|
const latestMessageTimestamp = existingMessages.reduce((latest, item) => {
|
||||||
|
const timestamp =
|
||||||
|
item.createdAt instanceof Date
|
||||||
|
? item.createdAt.getTime()
|
||||||
|
: Date.parse(String(item.createdAt || ""));
|
||||||
|
return Number.isFinite(timestamp) ? Math.max(latest, timestamp) : latest;
|
||||||
|
}, 0);
|
||||||
|
const userCreatedAt = new Date(
|
||||||
|
Math.max(Date.now(), latestMessageTimestamp + 1)
|
||||||
|
);
|
||||||
|
const assistantCreatedAt = new Date(userCreatedAt.getTime() + 1);
|
||||||
|
|
||||||
|
const userMessage = {
|
||||||
|
id: userMessageId,
|
||||||
chatId: parsed.id,
|
chatId: parsed.id,
|
||||||
userMessageId,
|
role: "user",
|
||||||
assistantMessageId,
|
parts: [{ type: "text", text: displayMessage }],
|
||||||
status: "queued",
|
attachments: [],
|
||||||
processingHint,
|
createdAt: userCreatedAt,
|
||||||
});
|
videoStorageStatus: null,
|
||||||
return NextResponse.json(responseBody, {
|
temporaryVideoUrl: null,
|
||||||
status: 202,
|
permanentVideoUrl: null,
|
||||||
headers: {
|
archivedAt: null,
|
||||||
"Cache-Control": "no-store, max-age=0",
|
videoMetadata: {
|
||||||
Location: responseBody.task.statusUrl,
|
composerMode: "clawd",
|
||||||
"Retry-After": "2",
|
source: "openclaw",
|
||||||
|
skillTagId: skillTagId || null,
|
||||||
|
skillTagName: skillTagName || null,
|
||||||
},
|
},
|
||||||
|
};
|
||||||
|
const assistantMessage = {
|
||||||
|
id: assistantMessageId,
|
||||||
|
chatId: parsed.id,
|
||||||
|
role: "assistant",
|
||||||
|
parts: [{ type: "text", text: answer }],
|
||||||
|
attachments: [],
|
||||||
|
createdAt: assistantCreatedAt,
|
||||||
|
videoStorageStatus: null,
|
||||||
|
temporaryVideoUrl: null,
|
||||||
|
permanentVideoUrl: null,
|
||||||
|
archivedAt: null,
|
||||||
|
videoMetadata: {
|
||||||
|
composerMode: "clawd",
|
||||||
|
source: "openclaw",
|
||||||
|
requestId:
|
||||||
|
upstreamPayload && typeof upstreamPayload === "object"
|
||||||
|
? (upstreamPayload as Record<string, unknown>).request_id || null
|
||||||
|
: null,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
await saveMessages({ messages: [userMessage, assistantMessage] });
|
||||||
|
clearClawdRequestCancellation(cancelKey);
|
||||||
|
|
||||||
|
return NextResponse.json({
|
||||||
|
conversationId,
|
||||||
|
requestId:
|
||||||
|
upstreamPayload && typeof upstreamPayload === "object"
|
||||||
|
? (upstreamPayload as Record<string, unknown>).request_id || null
|
||||||
|
: null,
|
||||||
|
messages: [
|
||||||
|
{
|
||||||
|
id: userMessage.id,
|
||||||
|
role: "user",
|
||||||
|
parts: userMessage.parts,
|
||||||
|
createdAt: userMessage.createdAt.toISOString(),
|
||||||
|
metadata: {
|
||||||
|
createdAt: userMessage.createdAt.toISOString(),
|
||||||
|
composerMode: "clawd",
|
||||||
|
},
|
||||||
|
videoMetadata: userMessage.videoMetadata,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: assistantMessage.id,
|
||||||
|
role: "assistant",
|
||||||
|
parts: assistantMessage.parts,
|
||||||
|
createdAt: assistantMessage.createdAt.toISOString(),
|
||||||
|
metadata: {
|
||||||
|
createdAt: assistantMessage.createdAt.toISOString(),
|
||||||
|
composerMode: "clawd",
|
||||||
|
},
|
||||||
|
videoMetadata: assistantMessage.videoMetadata,
|
||||||
|
},
|
||||||
|
],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,191 +0,0 @@
|
|||||||
import { NextResponse } from "next/server";
|
|
||||||
import { z } from "zod";
|
|
||||||
import { auth } from "@/app/(auth)/auth";
|
|
||||||
import { getChatById, getSystemSetting } from "@/lib/db/queries";
|
|
||||||
import {
|
|
||||||
buildClawdCancelKey,
|
|
||||||
hasActiveClawdRequest,
|
|
||||||
registerActiveClawdRequest,
|
|
||||||
unregisterActiveClawdRequest,
|
|
||||||
} from "@/lib/openclaw-chat/cancel-registry";
|
|
||||||
import {
|
|
||||||
normalizeOpenClawChatConfig,
|
|
||||||
OPENCLAW_CHAT_CONFIG_KEY,
|
|
||||||
} from "@/lib/openclaw-chat/config";
|
|
||||||
import {
|
|
||||||
OPENCLAW_AUTH_REQUIRED_CODE,
|
|
||||||
OPENCLAW_AUTH_REQUIRED_MESSAGE,
|
|
||||||
} from "@/lib/openclaw-chat/errors";
|
|
||||||
import {
|
|
||||||
getPersistedClawdTask,
|
|
||||||
markClawdTaskCanceled,
|
|
||||||
markClawdTaskCompleted,
|
|
||||||
markClawdTaskFailed,
|
|
||||||
} from "@/lib/openclaw-chat/task-store";
|
|
||||||
import { isPendingClawdTaskStatus } from "@/lib/openclaw-chat/task-state";
|
|
||||||
import {
|
|
||||||
getPersistedUpstreamTaskState,
|
|
||||||
refreshOpenClawDurableTask,
|
|
||||||
} from "@/lib/openclaw-chat/upstream-task-client";
|
|
||||||
|
|
||||||
const statusSchema = z.object({
|
|
||||||
id: z.string().uuid(),
|
|
||||||
userMessageId: z.string().uuid().optional(),
|
|
||||||
});
|
|
||||||
|
|
||||||
export const dynamic = "force-dynamic";
|
|
||||||
export const revalidate = 0;
|
|
||||||
export const maxDuration = 60;
|
|
||||||
|
|
||||||
async function reconcileDetachedOpenClawTask(params: {
|
|
||||||
chatId: string;
|
|
||||||
userId: string;
|
|
||||||
task: NonNullable<Awaited<ReturnType<typeof getPersistedClawdTask>>>;
|
|
||||||
}) {
|
|
||||||
if (!isPendingClawdTaskStatus(params.task.metadata.status)) return;
|
|
||||||
const upstreamTask = getPersistedUpstreamTaskState(params.task.metadata);
|
|
||||||
if (!upstreamTask) return;
|
|
||||||
const cancelKey = buildClawdCancelKey({
|
|
||||||
userId: params.userId,
|
|
||||||
chatId: params.chatId,
|
|
||||||
userMessageId: params.task.metadata.userMessageId,
|
|
||||||
});
|
|
||||||
if (hasActiveClawdRequest(cancelKey)) return;
|
|
||||||
|
|
||||||
const controller = new AbortController();
|
|
||||||
registerActiveClawdRequest(cancelKey, controller, 30_000);
|
|
||||||
const target = {
|
|
||||||
chatId: params.chatId,
|
|
||||||
assistantMessageId: params.task.metadata.assistantMessageId,
|
|
||||||
};
|
|
||||||
try {
|
|
||||||
const setting = await getSystemSetting({
|
|
||||||
key: OPENCLAW_CHAT_CONFIG_KEY,
|
|
||||||
}).catch(() => null);
|
|
||||||
const config = normalizeOpenClawChatConfig(setting?.value);
|
|
||||||
if (!config.enabled || !config.baseUrl || !config.token) return;
|
|
||||||
const outcome = await refreshOpenClawDurableTask({
|
|
||||||
config,
|
|
||||||
upstreamTask,
|
|
||||||
target,
|
|
||||||
signal: controller.signal,
|
|
||||||
});
|
|
||||||
if (outcome.status === "completed") {
|
|
||||||
await markClawdTaskCompleted({
|
|
||||||
...target,
|
|
||||||
answer: outcome.answer,
|
|
||||||
requestId: outcome.requestId,
|
|
||||||
artifacts: outcome.artifacts,
|
|
||||||
});
|
|
||||||
} else if (outcome.status === "failed") {
|
|
||||||
await markClawdTaskFailed({ ...target, error: outcome.error });
|
|
||||||
} else if (outcome.status === "canceled") {
|
|
||||||
await markClawdTaskCanceled({ ...target, message: outcome.error });
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
if (!controller.signal.aborted) {
|
|
||||||
console.warn("[ClawdChat Status] Upstream reconciliation deferred:", {
|
|
||||||
chatId: params.chatId,
|
|
||||||
taskId: upstreamTask.taskId,
|
|
||||||
error,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
unregisterActiveClawdRequest(cancelKey);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function GET(request: Request) {
|
|
||||||
const session = await auth().catch(() => null);
|
|
||||||
if (!session?.user?.id) {
|
|
||||||
return NextResponse.json(
|
|
||||||
{
|
|
||||||
error: OPENCLAW_AUTH_REQUIRED_MESSAGE,
|
|
||||||
code: OPENCLAW_AUTH_REQUIRED_CODE,
|
|
||||||
},
|
|
||||||
{ status: 401 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const url = new URL(request.url);
|
|
||||||
const parsed = statusSchema.safeParse({
|
|
||||||
id: url.searchParams.get("id"),
|
|
||||||
userMessageId: url.searchParams.get("userMessageId") || undefined,
|
|
||||||
});
|
|
||||||
if (!parsed.success) {
|
|
||||||
return NextResponse.json({ error: "请求参数不正确" }, { status: 400 });
|
|
||||||
}
|
|
||||||
|
|
||||||
const chat = await getChatById({ id: parsed.data.id }).catch(() => null);
|
|
||||||
if (!chat) {
|
|
||||||
return NextResponse.json({ task: null }, { status: 404 });
|
|
||||||
}
|
|
||||||
if (chat.userId !== session.user.id) {
|
|
||||||
return NextResponse.json({ error: "Forbidden" }, { status: 403 });
|
|
||||||
}
|
|
||||||
|
|
||||||
let task = await getPersistedClawdTask({
|
|
||||||
chatId: parsed.data.id,
|
|
||||||
userMessageId: parsed.data.userMessageId,
|
|
||||||
});
|
|
||||||
if (!task) {
|
|
||||||
return NextResponse.json(
|
|
||||||
{ task: null },
|
|
||||||
{ headers: { "Cache-Control": "no-store, max-age=0" } }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
await reconcileDetachedOpenClawTask({
|
|
||||||
chatId: parsed.data.id,
|
|
||||||
userId: session.user.id,
|
|
||||||
task,
|
|
||||||
});
|
|
||||||
task =
|
|
||||||
(await getPersistedClawdTask({
|
|
||||||
chatId: parsed.data.id,
|
|
||||||
userMessageId: parsed.data.userMessageId,
|
|
||||||
})) || task;
|
|
||||||
|
|
||||||
const userTextPart = Array.isArray(task.userMessage?.parts)
|
|
||||||
? task.userMessage.parts.find(
|
|
||||||
(part) =>
|
|
||||||
part &&
|
|
||||||
typeof part === "object" &&
|
|
||||||
!Array.isArray(part) &&
|
|
||||||
(part as { type?: unknown }).type === "text"
|
|
||||||
)
|
|
||||||
: null;
|
|
||||||
const userText =
|
|
||||||
userTextPart && typeof userTextPart === "object"
|
|
||||||
? String((userTextPart as { text?: unknown }).text || "")
|
|
||||||
: "";
|
|
||||||
|
|
||||||
return NextResponse.json(
|
|
||||||
{
|
|
||||||
task: {
|
|
||||||
chatId: parsed.data.id,
|
|
||||||
userMessageId: task.metadata.userMessageId,
|
|
||||||
assistantMessageId: task.metadata.assistantMessageId,
|
|
||||||
status: task.metadata.status,
|
|
||||||
processingHint: task.metadata.processingHint,
|
|
||||||
progressEvents: task.metadata.progressEvents,
|
|
||||||
skillTagId: task.metadata.skillTagId || null,
|
|
||||||
skillTagName: task.metadata.skillTagName || null,
|
|
||||||
requestId: task.metadata.requestId || null,
|
|
||||||
error: task.metadata.error || null,
|
|
||||||
createdAt: task.metadata.createdAt,
|
|
||||||
updatedAt: task.metadata.updatedAt,
|
|
||||||
userMessage: {
|
|
||||||
text: userText,
|
|
||||||
attachments: task.userMessage?.attachments || [],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
headers: {
|
|
||||||
"Cache-Control": "no-store, max-age=0",
|
|
||||||
"Retry-After": "2",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@ -1956,7 +1956,7 @@ export async function POST(request: Request) {
|
|||||||
(item) =>
|
(item) =>
|
||||||
!item.model.isSubModel &&
|
!item.model.isSubModel &&
|
||||||
item.model.isEnabled &&
|
item.model.isEnabled &&
|
||||||
["zhipu", "alibaba", "openai", "custom", "local", "vip98k"].includes(item.provider.type),
|
["zhipu", "alibaba", "openai", "custom", "local"].includes(item.provider.type),
|
||||||
);
|
);
|
||||||
const configuredDetailAgentConfig =
|
const configuredDetailAgentConfig =
|
||||||
currentSiteSettings?.canvasDetailAgentModelId &&
|
currentSiteSettings?.canvasDetailAgentModelId &&
|
||||||
|
|||||||
@ -204,7 +204,7 @@ export async function POST(request: Request) {
|
|||||||
(item) =>
|
(item) =>
|
||||||
!item.model.isSubModel &&
|
!item.model.isSubModel &&
|
||||||
item.model.isEnabled &&
|
item.model.isEnabled &&
|
||||||
["zhipu", "alibaba", "openai", "custom", "local", "vip98k"].includes(item.provider.type),
|
["zhipu", "alibaba", "openai", "custom", "local"].includes(item.provider.type),
|
||||||
);
|
);
|
||||||
const configuredDetailAgentConfig =
|
const configuredDetailAgentConfig =
|
||||||
currentSiteSettings?.canvasDetailAgentModelId &&
|
currentSiteSettings?.canvasDetailAgentModelId &&
|
||||||
|
|||||||
@ -1,45 +0,0 @@
|
|||||||
import { NextResponse } from "next/server";
|
|
||||||
import { auth } from "@/app/(auth)/auth";
|
|
||||||
import { uploadFile } from "@/lib/storage/service";
|
|
||||||
import {
|
|
||||||
DIRECTOR_MAX_GLB_BYTES,
|
|
||||||
validateDirectorGlb,
|
|
||||||
} from "@/lib/video-canvas/director-glb";
|
|
||||||
export const runtime = "nodejs";
|
|
||||||
export async function POST(request: Request) {
|
|
||||||
const session = await auth();
|
|
||||||
if (!session?.user?.id)
|
|
||||||
return NextResponse.json({ error: "未登录" }, { status: 401 });
|
|
||||||
try {
|
|
||||||
const form = await request.formData();
|
|
||||||
const file = form.get("file");
|
|
||||||
if (
|
|
||||||
!(file instanceof File) ||
|
|
||||||
!/\.glb$/i.test(file.name) ||
|
|
||||||
file.size > DIRECTOR_MAX_GLB_BYTES
|
|
||||||
)
|
|
||||||
return NextResponse.json(
|
|
||||||
{ error: "请上传 25 MiB 以内的 GLB 模型" },
|
|
||||||
{ status: 400 },
|
|
||||||
);
|
|
||||||
const buffer = await file.arrayBuffer();
|
|
||||||
try {
|
|
||||||
validateDirectorGlb(buffer);
|
|
||||||
} catch (error) {
|
|
||||||
return NextResponse.json(
|
|
||||||
{ error: error instanceof Error ? error.message : "无效 GLB" },
|
|
||||||
{ status: 400 },
|
|
||||||
);
|
|
||||||
}
|
|
||||||
const result = await uploadFile(
|
|
||||||
`director-${crypto.randomUUID()}.glb`,
|
|
||||||
buffer,
|
|
||||||
);
|
|
||||||
return NextResponse.json({ success: true, data: result });
|
|
||||||
} catch {
|
|
||||||
return NextResponse.json(
|
|
||||||
{ error: "模型上传失败,请重试" },
|
|
||||||
{ status: 500 },
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,9 +1,5 @@
|
|||||||
import { resolveCanvasCdnDownload } from "@/lib/storage/canvas-cdn-download";
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
import { prepareImageArchiveDownload } from "@/lib/storage/image-download-preparation";
|
|
||||||
import { after, NextRequest, NextResponse } from "next/server";
|
|
||||||
import { auth } from "@/app/(auth)/auth";
|
import { auth } from "@/app/(auth)/auth";
|
||||||
import { getDownloadPreparationQueue, type DownloadPreparationInput } from "@/lib/canvas/download-preparation";
|
|
||||||
import { extractImageProxySourceUrl, inferOriginalImageUrl } from "@/lib/image-url";
|
|
||||||
import {
|
import {
|
||||||
buildAttachmentContentDisposition,
|
buildAttachmentContentDisposition,
|
||||||
fetchCanvasDownloadAsset,
|
fetchCanvasDownloadAsset,
|
||||||
@ -13,31 +9,6 @@ import {
|
|||||||
export const runtime = "nodejs";
|
export const runtime = "nodejs";
|
||||||
export const maxDuration = 120;
|
export const maxDuration = 120;
|
||||||
|
|
||||||
async function prepareDownload(input: DownloadPreparationInput, signal: AbortSignal) {
|
|
||||||
// A proxy candidate for the same original must not repeat the same slow
|
|
||||||
// upstream request through our own server (and create another HTTP timeout).
|
|
||||||
const sources = [...new Set(input.urls.map(url => {
|
|
||||||
const source = extractImageProxySourceUrl(url) || url;
|
|
||||||
return inferOriginalImageUrl(source) || source;
|
|
||||||
}))];
|
|
||||||
for (let attempt = 0; attempt < 3; attempt++) {
|
|
||||||
signal.throwIfAborted();
|
|
||||||
for (const url of sources) {
|
|
||||||
signal.throwIfAborted();
|
|
||||||
const result = await fetchCanvasDownloadAsset({
|
|
||||||
file: { url, name: "original" }, index: 0,
|
|
||||||
requestOrigin: input.requestOrigin, cookie: input.cookie,
|
|
||||||
includeSequencePrefix: false, timeoutMs: 12_000, retryCount: 1, signal,
|
|
||||||
});
|
|
||||||
if (result.ok) return { buffer: result.bytes, contentType: result.contentType };
|
|
||||||
}
|
|
||||||
// Each pass resolves the latest archive URL inside fetchCanvasDownloadAsset.
|
|
||||||
// No AI/model request is involved and a completed source fetch is shared.
|
|
||||||
if (attempt < 2) await new Promise(resolve => setTimeout(resolve, 1_000));
|
|
||||||
}
|
|
||||||
throw new Error("Original image sources unavailable");
|
|
||||||
}
|
|
||||||
|
|
||||||
function getRequestOrigin(request: NextRequest): string {
|
function getRequestOrigin(request: NextRequest): string {
|
||||||
const forwardedHost = (request.headers.get("x-forwarded-host") || "").split(",")[0]?.trim();
|
const forwardedHost = (request.headers.get("x-forwarded-host") || "").split(",")[0]?.trim();
|
||||||
const forwardedProto = (request.headers.get("x-forwarded-proto") || "").split(",")[0]?.trim();
|
const forwardedProto = (request.headers.get("x-forwarded-proto") || "").split(",")[0]?.trim();
|
||||||
@ -84,41 +55,12 @@ export async function POST(request: NextRequest) {
|
|||||||
const body = (await request.json().catch(() => ({}))) as Record<string, unknown>;
|
const body = (await request.json().catch(() => ({}))) as Record<string, unknown>;
|
||||||
const requestOrigin = getRequestOrigin(request);
|
const requestOrigin = getRequestOrigin(request);
|
||||||
const urls = resolveDownloadUrls(body, requestOrigin);
|
const urls = resolveDownloadUrls(body, requestOrigin);
|
||||||
const requestedUrls = [...urls];
|
|
||||||
if (urls.length === 0) {
|
if (urls.length === 0) {
|
||||||
return NextResponse.json({ success: false, error: "图片地址无效" }, { status: 400 });
|
return NextResponse.json({ success: false, error: "图片地址无效" }, { status: 400 });
|
||||||
}
|
}
|
||||||
|
|
||||||
const name = sanitizeCanvasDownloadFileName(String(body.name || ""), "canvas-image");
|
const name = sanitizeCanvasDownloadFileName(String(body.name || ""), "canvas-image");
|
||||||
if (body.preferDirect === true) {
|
|
||||||
const { task, direct } = await resolveCanvasCdnDownload(urls, `${name}.png`).catch(() => ({ task: null, direct: null }));
|
|
||||||
if (direct) return NextResponse.json(direct, { headers: { "Cache-Control": "no-store" } });
|
|
||||||
if (task?.targetUrl) {
|
|
||||||
if (!urls.includes(task.targetUrl)) urls.unshift(task.targetUrl);
|
|
||||||
after(() => prepareImageArchiveDownload(task.id));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
const cookie = request.headers.get("cookie") || "";
|
const cookie = request.headers.get("cookie") || "";
|
||||||
if (body.preparation === "async") {
|
|
||||||
const result = await getDownloadPreparationQueue(prepareDownload, after).request({
|
|
||||||
userId: session.user.id, urls: requestedUrls, requestOrigin, cookie,
|
|
||||||
});
|
|
||||||
if (result.status === "preparing") {
|
|
||||||
return NextResponse.json({ delivery: "preparing", retryAfter: 3,
|
|
||||||
message: "正在准备原图,完成后将自动下载" }, {
|
|
||||||
status: 202, headers: { "Retry-After": "3", "Cache-Control": "no-store" },
|
|
||||||
});
|
|
||||||
}
|
|
||||||
if (result.status === "failed") {
|
|
||||||
return NextResponse.json({ success: false, error: result.error }, { status: 502 });
|
|
||||||
}
|
|
||||||
return new NextResponse(new Uint8Array(result.asset.buffer), { headers: {
|
|
||||||
"Content-Type": result.asset.contentType,
|
|
||||||
"Content-Length": String(result.asset.buffer.length),
|
|
||||||
"Content-Disposition": buildAttachmentContentDisposition(`${name}.png`),
|
|
||||||
"Cache-Control": "no-store",
|
|
||||||
} });
|
|
||||||
}
|
|
||||||
let result: Awaited<ReturnType<typeof fetchCanvasDownloadAsset>> | null = null;
|
let result: Awaited<ReturnType<typeof fetchCanvasDownloadAsset>> | null = null;
|
||||||
const failures: string[] = [];
|
const failures: string[] = [];
|
||||||
|
|
||||||
|
|||||||
@ -1,22 +1,54 @@
|
|||||||
import { writeDownloadZip, readDownloadZip } from "@/lib/canvas/download-zip-stream";
|
|
||||||
import { claimZipTask, loadZipTask, saveZipTask, zipTaskPath, assertZipDiskSpace, cleanupZipTasks, assertZipTaskCapacity, type ZipTask } from "@/lib/canvas/download-zip-task-store";
|
|
||||||
import { parseDownloadByteRange } from "@/lib/canvas/download-byte-range";
|
|
||||||
import { createReadStream } from "node:fs";
|
|
||||||
import { mkdir, stat, writeFile, rm } from "node:fs/promises";
|
|
||||||
import { join } from "node:path";
|
|
||||||
import { randomUUID } from "node:crypto";
|
import { randomUUID } from "node:crypto";
|
||||||
import { after, NextRequest, NextResponse } from "next/server";
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
import JSZip from "jszip";
|
import JSZip from "jszip";
|
||||||
import { auth } from "@/app/(auth)/auth";
|
import { auth } from "@/app/(auth)/auth";
|
||||||
import { fetchCanvasDownloadAsset } from "@/lib/canvas/photoshop-safe-download";
|
import { fetchCanvasDownloadAsset } from "@/lib/canvas/photoshop-safe-download";
|
||||||
import { publishCanvasZip } from "@/lib/storage/canvas-zip-delivery";
|
|
||||||
export const runtime = "nodejs";
|
export const runtime = "nodejs";
|
||||||
export const maxDuration = 300;
|
export const maxDuration = 300;
|
||||||
type DownloadFileItem = { url: string; name?: string };
|
|
||||||
|
type DownloadFileItem = {
|
||||||
|
url: string;
|
||||||
|
name?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
type ZipTaskStatus = "pending" | "running" | "completed" | "failed";
|
||||||
|
|
||||||
|
type ZipTask = {
|
||||||
|
id: string;
|
||||||
|
userId: string;
|
||||||
|
folderName: string;
|
||||||
|
files: DownloadFileItem[];
|
||||||
|
totalCount: number;
|
||||||
|
processedCount: number;
|
||||||
|
successCount: number;
|
||||||
|
failedCount: number;
|
||||||
|
compressPercent: number;
|
||||||
|
status: ZipTaskStatus;
|
||||||
|
stage: string;
|
||||||
|
error?: string;
|
||||||
|
zipBuffer?: Buffer;
|
||||||
|
contentDisposition?: string;
|
||||||
|
createdAt: number;
|
||||||
|
updatedAt: number;
|
||||||
|
expiresAt: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
const MAX_CONCURRENCY = 8;
|
||||||
const MAX_DOWNLOAD_FILES = 200;
|
const MAX_DOWNLOAD_FILES = 200;
|
||||||
const TASK_TTL_MS = 60 * 60_000;
|
const TASK_TTL_MS = 30 * 60 * 1000;
|
||||||
const running = (globalThis as typeof globalThis & { __canvasZipRunning?: Set<string> });
|
|
||||||
const active = running.__canvasZipRunning ??= new Set<string>();
|
const globalTaskStore = globalThis as typeof globalThis & {
|
||||||
|
__canvasDownloadZipTasks?: Map<string, ZipTask>;
|
||||||
|
};
|
||||||
|
|
||||||
|
function getTaskStore() {
|
||||||
|
if (!globalTaskStore.__canvasDownloadZipTasks) {
|
||||||
|
globalTaskStore.__canvasDownloadZipTasks = new Map<string, ZipTask>();
|
||||||
|
}
|
||||||
|
return globalTaskStore.__canvasDownloadZipTasks;
|
||||||
|
}
|
||||||
|
|
||||||
function sanitizeFolderName(value: string): string {
|
function sanitizeFolderName(value: string): string {
|
||||||
const cleaned = String(value || "")
|
const cleaned = String(value || "")
|
||||||
.replace(/[\\/:*?"<>|]+/g, "-")
|
.replace(/[\\/:*?"<>|]+/g, "-")
|
||||||
@ -24,7 +56,7 @@ function sanitizeFolderName(value: string): string {
|
|||||||
.replace(/-+/g, "-")
|
.replace(/-+/g, "-")
|
||||||
.replace(/^-+|-+$/g, "")
|
.replace(/^-+|-+$/g, "")
|
||||||
.trim();
|
.trim();
|
||||||
return cleaned && !/^\.+$/.test(cleaned) ? cleaned.slice(0, 100) : `canvas-images-${Date.now()}`;
|
return cleaned || `canvas-images-${Date.now()}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function buildContentDisposition(folderName: string): string {
|
function buildContentDisposition(folderName: string): string {
|
||||||
@ -78,161 +110,258 @@ function toDownloadItems(input: unknown, origin: string): DownloadFileItem[] {
|
|||||||
.slice(0, MAX_DOWNLOAD_FILES);
|
.slice(0, MAX_DOWNLOAD_FILES);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function cleanupExpiredTasks() {
|
||||||
|
const now = Date.now();
|
||||||
|
const store = getTaskStore();
|
||||||
|
for (const [taskId, task] of store.entries()) {
|
||||||
|
if (task.expiresAt <= now) {
|
||||||
|
store.delete(taskId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function getTaskProgress(task: ZipTask) {
|
||||||
|
if (task.status === "completed") {
|
||||||
|
return 100;
|
||||||
|
}
|
||||||
|
if (task.status === "failed") {
|
||||||
|
return Math.max(
|
||||||
|
0,
|
||||||
|
Math.min(100, Math.round((task.processedCount / Math.max(1, task.totalCount)) * 85))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (task.processedCount >= task.totalCount && task.totalCount > 0) {
|
||||||
|
return Math.min(99, 85 + Math.round((task.compressPercent / 100) * 14));
|
||||||
|
}
|
||||||
|
return Math.max(
|
||||||
|
4,
|
||||||
|
Math.min(85, Math.round((task.processedCount / Math.max(1, task.totalCount)) * 85))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
function serializeTask(task: ZipTask) {
|
function serializeTask(task: ZipTask) {
|
||||||
return { id: task.id, folderName: task.folderName, status: task.status, stage: task.stage,
|
const downloadUrl =
|
||||||
totalCount: task.totalCount, processedCount: task.processedCount, successCount: task.successCount,
|
task.status === "completed"
|
||||||
failedCount: task.failedCount, progress: task.status === "completed" ? 100 : Math.min(99,
|
? `/api/canvas/download-zip-task?taskId=${encodeURIComponent(task.id)}&download=1`
|
||||||
Math.round(task.processedCount / Math.max(1, task.totalCount) * 85 + task.compressPercent * .14)),
|
: "";
|
||||||
error: task.error || "", expiresAt: task.expiresAt,
|
return {
|
||||||
downloadUrl: task.status === "completed" ? `/api/canvas/download-zip-task?taskId=${task.id}&download=1` : "" };
|
id: task.id,
|
||||||
|
folderName: task.folderName,
|
||||||
|
status: task.status,
|
||||||
|
stage: task.stage,
|
||||||
|
totalCount: task.totalCount,
|
||||||
|
processedCount: task.processedCount,
|
||||||
|
successCount: task.successCount,
|
||||||
|
failedCount: task.failedCount,
|
||||||
|
progress: getTaskProgress(task),
|
||||||
|
error: task.error || "",
|
||||||
|
downloadUrl,
|
||||||
|
createdAt: task.createdAt,
|
||||||
|
updatedAt: task.updatedAt,
|
||||||
|
expiresAt: task.expiresAt,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
async function processTask(id: string, userId: string, requestOrigin: string, cookie: string) {
|
|
||||||
if (active.has(id) || active.size >= 2) return;
|
async function processZipTask(params: {
|
||||||
active.add(id);
|
task: ZipTask;
|
||||||
let release: (() => Promise<void>) | null = null;
|
requestOrigin: string;
|
||||||
let task: ZipTask | null = null;
|
cookie: string;
|
||||||
|
}) {
|
||||||
|
const { task, requestOrigin, cookie } = params;
|
||||||
|
const store = getTaskStore();
|
||||||
try {
|
try {
|
||||||
release = await claimZipTask(id);
|
task.status = "running";
|
||||||
if (!release) return;
|
task.stage = "正在读取源图";
|
||||||
task = await loadZipTask(id, userId);
|
task.updatedAt = Date.now();
|
||||||
if (!task || task.status === "completed" || task.status === "failed") return;
|
|
||||||
await assertZipDiskSpace();
|
const zip = new JSZip();
|
||||||
task.status = "running"; task.stage = "正在准备原图"; task.updatedAt = Date.now();
|
const folder = zip.folder(task.folderName) || zip;
|
||||||
await saveZipTask(task);
|
const results: Array<
|
||||||
// Stage each image on disk, not all PNG buffers in the web process heap.
|
| Awaited<ReturnType<typeof fetchCanvasDownloadAsset>>
|
||||||
// A restart resumes from these files using the current owner's session.
|
| null
|
||||||
|
> = new Array(task.files.length).fill(null);
|
||||||
let cursor = 0;
|
let cursor = 0;
|
||||||
let writes = Promise.resolve();
|
const workerCount = Math.min(MAX_CONCURRENCY, task.files.length);
|
||||||
const persist = () => { const current = task!; writes = writes.then(() => saveZipTask(current)); return writes; };
|
|
||||||
const results = await Promise.allSettled(Array.from({ length: Math.min(2, task.files.length) }, async () => {
|
await Promise.all(
|
||||||
while (cursor < task!.files.length) {
|
Array.from({ length: workerCount }, async () => {
|
||||||
const index = cursor++; const file = task!.files[index];
|
while (true) {
|
||||||
const path = join(zipTaskPath(id), `${index}.image`);
|
const index = cursor;
|
||||||
if (file.prepared && file.filename && (await stat(path).catch(() => null))?.size) continue;
|
cursor += 1;
|
||||||
delete file.prepared; delete file.filename;
|
if (index >= task.files.length) break;
|
||||||
const result = await fetchCanvasDownloadAsset({ file, index, requestOrigin, cookie,
|
|
||||||
signal: AbortSignal.timeout(120_000) }).catch(() => ({ ok: false as const, error: "原图读取超时或源地址暂时不可用" }));
|
const file = task.files[index]!;
|
||||||
if (result.ok) {
|
results[index] = await fetchCanvasDownloadAsset({
|
||||||
await assertZipDiskSpace(result.bytes.length);
|
file,
|
||||||
await writeFile(path, result.bytes, { mode: 0o600 });
|
index,
|
||||||
file.prepared = `${index}.image`; file.filename = result.filename; delete file.error;
|
requestOrigin,
|
||||||
} else file.error = result.error;
|
cookie,
|
||||||
task!.processedCount = task!.files.filter(f => f.prepared || f.error).length;
|
});
|
||||||
task!.successCount = task!.files.filter(f => f.prepared).length;
|
|
||||||
task!.failedCount = task!.files.filter(f => f.error).length;
|
const result = results[index];
|
||||||
task!.stage = `正在准备原图 ${task!.processedCount}/${task!.totalCount}`;
|
task.processedCount += 1;
|
||||||
task!.updatedAt = Date.now(); await persist();
|
if (result?.ok) {
|
||||||
|
task.successCount += 1;
|
||||||
|
} else {
|
||||||
|
task.failedCount += 1;
|
||||||
|
}
|
||||||
|
task.stage = `正在处理 ${task.processedCount}/${task.totalCount} 张`;
|
||||||
|
task.updatedAt = Date.now();
|
||||||
|
}
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
const failures: Array<{ index: number; url: string; name?: string; error: string }> = [];
|
||||||
|
for (const [index, item] of results.entries()) {
|
||||||
|
if (!item || !item.ok) {
|
||||||
|
failures.push({
|
||||||
|
index: index + 1,
|
||||||
|
url: item?.ok === false ? item.url : task.files[index]?.url || "",
|
||||||
|
name: item?.ok === false ? item.name : task.files[index]?.name,
|
||||||
|
error: item?.ok === false ? item.error : "未知错误",
|
||||||
|
});
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
}));
|
folder.file(item.filename, item.bytes);
|
||||||
const rejected = results.find(result => result.status === "rejected");
|
}
|
||||||
if (rejected?.status === "rejected") throw rejected.reason;
|
|
||||||
if (!task.successCount) throw new Error("所选原图暂时都无法读取,请稍后重试;画布图片不会被删除");
|
if (failures.length > 0) {
|
||||||
const zip = new JSZip(); const folder = zip.folder(task.folderName) || zip;
|
folder.file(
|
||||||
for (const file of task.files) if (file.prepared && file.filename)
|
"_download-failures.txt",
|
||||||
folder.file(file.filename, createReadStream(join(zipTaskPath(id), file.prepared))
|
[
|
||||||
.once("close", () => { void rm(join(zipTaskPath(id), file.prepared!), { force: true }).catch(() => undefined); }));
|
"以下图片暂时无法下载。可稍后重试,或复制链接检查源图是否仍可访问。",
|
||||||
if (task.failedCount) folder.file("_download-failures.txt", task.files.filter(f => f.error)
|
"",
|
||||||
.map(f => `${f.name || "图片"}:${f.error}`).join("\n"));
|
...failures.map((item) => {
|
||||||
task.stage = "正在生成完整 ZIP 文件"; await saveZipTask(task);
|
const label = item.name ? `${item.index}. ${item.name}` : `${item.index}. image`;
|
||||||
task.zipFile = await writeDownloadZip(zip, task.id, percent => { task!.compressPercent = percent; });
|
return `${label}\nURL: ${item.url}\n原因: ${item.error}`;
|
||||||
|
}),
|
||||||
|
].join("\n\n")
|
||||||
|
);
|
||||||
|
console.warn("[CanvasDownloadZipTask] Partial download failures:", failures);
|
||||||
|
}
|
||||||
|
|
||||||
|
task.stage = "正在生成 ZIP 文件";
|
||||||
|
task.updatedAt = Date.now();
|
||||||
|
task.zipBuffer = await zip.generateAsync(
|
||||||
|
{
|
||||||
|
type: "nodebuffer",
|
||||||
|
compression: "DEFLATE",
|
||||||
|
compressionOptions: { level: 6 },
|
||||||
|
},
|
||||||
|
(metadata) => {
|
||||||
|
task.compressPercent = Math.max(0, Math.min(100, metadata.percent || 0));
|
||||||
|
task.updatedAt = Date.now();
|
||||||
|
}
|
||||||
|
);
|
||||||
task.contentDisposition = buildContentDisposition(task.folderName);
|
task.contentDisposition = buildContentDisposition(task.folderName);
|
||||||
task.stage = "正在准备下载链接"; await saveZipTask(task);
|
task.status = "completed";
|
||||||
try {
|
task.stage = "打包完成";
|
||||||
const cdn = await publishCanvasZip({ id: task.id, ...task.zipFile, disposition: task.contentDisposition });
|
task.compressPercent = 100;
|
||||||
if (cdn) {
|
task.updatedAt = Date.now();
|
||||||
task.cdnObject = { storageConfigId: cdn.storageConfigId, key: cdn.key };
|
task.expiresAt = Date.now() + TASK_TTL_MS;
|
||||||
if (cdn.url) task.cdnUrl = cdn.url;
|
|
||||||
}
|
|
||||||
} catch { console.warn("[CanvasDownloadZipTask] CDN delivery unavailable; complete local ZIP retained", { taskId: id }); }
|
|
||||||
task.status = "completed"; task.stage = task.failedCount ? `已打包 ${task.successCount} 张,${task.failedCount} 张未能读取` : "打包完成";
|
|
||||||
task.compressPercent = 100; task.updatedAt = Date.now(); task.expiresAt = Date.now() + TASK_TTL_MS;
|
|
||||||
await saveZipTask(task);
|
|
||||||
// The complete ZIP is enough for retries; release staged duplicate bytes.
|
|
||||||
await Promise.all(task.files.filter(f => f.prepared).map(f => rm(join(zipTaskPath(id), f.prepared!), { force: true }).catch(() => undefined)));
|
|
||||||
console.info("[CanvasDownloadZipTask] Complete", { taskId: id, count: task.successCount, failed: task.failedCount,
|
|
||||||
bytes: task.zipFile.bytes, delivery: task.cdnUrl ? "cdn" : "local" });
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (task) {
|
console.error("[CanvasDownloadZipTask] Failed:", error);
|
||||||
task.status = "failed"; task.stage = "打包未完成";
|
task.status = "failed";
|
||||||
task.error = error instanceof Error ? error.message : "打包失败,请稍后重试";
|
task.stage = "打包失败";
|
||||||
task.updatedAt = Date.now(); task.expiresAt = Date.now() + TASK_TTL_MS; await saveZipTask(task).catch(() => undefined);
|
task.error = error instanceof Error ? error.message : "打包下载失败";
|
||||||
}
|
task.updatedAt = Date.now();
|
||||||
console.error("[CanvasDownloadZipTask] Preparation failed", { taskId: id, message: task?.error });
|
task.expiresAt = Date.now() + TASK_TTL_MS;
|
||||||
} finally { await release?.().catch(() => undefined); active.delete(id); }
|
} finally {
|
||||||
}
|
store.set(task.id, task);
|
||||||
function schedule(task: ZipTask, request: NextRequest) {
|
}
|
||||||
const run = () => processTask(task.id, task.userId, getRequestOrigin(request), request.headers.get("cookie") || "");
|
|
||||||
after(run);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function POST(request: NextRequest) {
|
export async function POST(request: NextRequest) {
|
||||||
const session = await auth();
|
|
||||||
if (!session?.user?.id) return NextResponse.json({ error: "未登录" }, { status: 401 });
|
|
||||||
try {
|
try {
|
||||||
const body = await request.json();
|
cleanupExpiredTasks();
|
||||||
let id = typeof body.taskId === "string" ? body.taskId : randomUUID();
|
const session = await auth();
|
||||||
let task = await loadZipTask(id, session.user.id);
|
if (!session?.user?.id) {
|
||||||
if (!task) {
|
return NextResponse.json({ success: false, error: "未登录" }, { status: 401 });
|
||||||
await assertZipTaskCapacity(session.user.id);
|
|
||||||
if (await stat(zipTaskPath(id)).catch(() => null)) id = randomUUID();
|
|
||||||
const files = toDownloadItems(body.files, getRequestOrigin(request));
|
|
||||||
if (!files.length || !Array.isArray(body.files) || body.files.length > MAX_DOWNLOAD_FILES || files.length !== body.files.length)
|
|
||||||
return NextResponse.json({ error: "请选择 1 到 200 张可下载图片" }, { status: 400 });
|
|
||||||
// Exclusive creation prevents an arbitrary taskId from overwriting another owner's manifest.
|
|
||||||
await mkdir(zipTaskPath(id), { recursive: false, mode: 0o700 }).catch(async error => {
|
|
||||||
if (error.code !== "ENOENT") throw error;
|
|
||||||
await mkdir((await import("@/lib/canvas/download-zip-stream")).downloadZipDirectory, { recursive: true, mode: 0o700 });
|
|
||||||
await mkdir(zipTaskPath(id), { mode: 0o700 });
|
|
||||||
});
|
|
||||||
const now = Date.now();
|
|
||||||
task = { id, userId: session.user.id, folderName: sanitizeFolderName(String(body.folderName || "")), files,
|
|
||||||
totalCount: files.length, processedCount: 0, successCount: 0, failedCount: 0, compressPercent: 0,
|
|
||||||
status: "pending", stage: "等待后台准备", createdAt: now, updatedAt: now, expiresAt: now + 6 * TASK_TTL_MS };
|
|
||||||
await saveZipTask(task);
|
|
||||||
} else if (task.status === "failed" && body.retry === true) {
|
|
||||||
task.status = "pending"; task.error = undefined;
|
|
||||||
await assertZipTaskCapacity(session.user.id);
|
|
||||||
for (const file of task.files) delete file.error;
|
|
||||||
task.processedCount = task.successCount = task.files.filter(file => file.prepared).length;
|
|
||||||
task.failedCount = 0; task.updatedAt = Date.now(); task.expiresAt = Date.now() + 6 * TASK_TTL_MS;
|
|
||||||
await saveZipTask(task);
|
|
||||||
}
|
}
|
||||||
if (task.status === "pending" || task.status === "running") schedule(task, request);
|
|
||||||
after(() => cleanupZipTasks().catch(() => { console.warn("[CanvasDownloadZipTask] Expired file cleanup deferred"); }));
|
const body = (await request.json().catch(() => ({}))) as Record<string, unknown>;
|
||||||
return NextResponse.json({ success: true, task: serializeTask(task) }, { status: 202, headers: { "Cache-Control": "no-store" } });
|
const requestOrigin = getRequestOrigin(request);
|
||||||
|
const files = toDownloadItems(body.files, requestOrigin);
|
||||||
|
const folderName = sanitizeFolderName(String(body.folderName || ""));
|
||||||
|
if (files.length === 0) {
|
||||||
|
return NextResponse.json({ success: false, error: "没有可下载的图片" }, { status: 400 });
|
||||||
|
}
|
||||||
|
|
||||||
|
const task: ZipTask = {
|
||||||
|
id: randomUUID(),
|
||||||
|
userId: session.user.id,
|
||||||
|
folderName,
|
||||||
|
files,
|
||||||
|
totalCount: files.length,
|
||||||
|
processedCount: 0,
|
||||||
|
successCount: 0,
|
||||||
|
failedCount: 0,
|
||||||
|
compressPercent: 0,
|
||||||
|
status: "pending",
|
||||||
|
stage: "等待服务端开始打包",
|
||||||
|
createdAt: Date.now(),
|
||||||
|
updatedAt: Date.now(),
|
||||||
|
expiresAt: Date.now() + TASK_TTL_MS,
|
||||||
|
};
|
||||||
|
getTaskStore().set(task.id, task);
|
||||||
|
|
||||||
|
void processZipTask({
|
||||||
|
task,
|
||||||
|
requestOrigin,
|
||||||
|
cookie: request.headers.get("cookie") || "",
|
||||||
|
});
|
||||||
|
|
||||||
|
return NextResponse.json({
|
||||||
|
success: true,
|
||||||
|
task: serializeTask(task),
|
||||||
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.warn("[CanvasDownloadZipTask] Create rejected", { message: (error as Error).message });
|
console.error("[CanvasDownloadZipTask] Create failed:", error);
|
||||||
return NextResponse.json({ error: "无法创建打包任务,请稍后重试" }, { status: 400 });
|
return NextResponse.json({ success: false, error: "创建打包任务失败" }, { status: 500 });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function GET(request: NextRequest) {
|
export async function GET(request: NextRequest) {
|
||||||
const session = await auth();
|
|
||||||
if (!session?.user?.id) return NextResponse.json({ error: "未登录" }, { status: 401 });
|
|
||||||
try {
|
try {
|
||||||
const id = request.nextUrl.searchParams.get("taskId") || "";
|
cleanupExpiredTasks();
|
||||||
const task = await loadZipTask(id, session.user.id);
|
const session = await auth();
|
||||||
if (!task) return NextResponse.json({ error: "打包任务不存在或已过期,请重新打包" }, { status: 404 });
|
if (!session?.user?.id) {
|
||||||
if (request.nextUrl.searchParams.get("download") === "1") {
|
return NextResponse.json({ success: false, error: "未登录" }, { status: 401 });
|
||||||
if (task.status !== "completed" || !task.zipFile) return NextResponse.json({ error: "ZIP 尚未准备好" }, { status: 409 });
|
|
||||||
if (task.cdnUrl && request.nextUrl.searchParams.get("local") !== "1")
|
|
||||||
return NextResponse.redirect(task.cdnUrl, 307);
|
|
||||||
const info = await stat(task.zipFile.path).catch(() => null);
|
|
||||||
if (!info || info.size !== task.zipFile.bytes) return NextResponse.json({ error: "下载文件已过期,请重新打包" }, { status: 410 });
|
|
||||||
const etag = `"${task.id}-${info.size}"`;
|
|
||||||
const headers: Record<string, string> = { "Content-Type": "application/zip", "Content-Disposition": task.contentDisposition!,
|
|
||||||
"Accept-Ranges": "bytes", "ETag": etag, "Cache-Control": "private, no-store",
|
|
||||||
"X-Download-Success-Count": String(task.successCount), "X-Download-Failed-Count": String(task.failedCount) };
|
|
||||||
const ifRange = request.headers.get("if-range");
|
|
||||||
let range;
|
|
||||||
try { range = parseDownloadByteRange(!ifRange || ifRange === etag ? request.headers.get("range") : null, info.size); }
|
|
||||||
catch { return new NextResponse(null, { status: 416, headers: { ...headers, "Content-Range": `bytes */${info.size}` } }); }
|
|
||||||
headers["Content-Length"] = String(range ? range.end - range.start + 1 : info.size);
|
|
||||||
if (range) headers["Content-Range"] = `bytes ${range.start}-${range.end}/${info.size}`;
|
|
||||||
return new NextResponse(readDownloadZip(task.zipFile.path, range || undefined), { status: range ? 206 : 200, headers });
|
|
||||||
}
|
}
|
||||||
if (task.status === "pending" || task.status === "running") schedule(task, request);
|
|
||||||
return NextResponse.json({ success: true, task: serializeTask(task) }, { headers: { "Cache-Control": "no-store" } });
|
const taskId = String(request.nextUrl.searchParams.get("taskId") || "").trim();
|
||||||
} catch {
|
if (!taskId) {
|
||||||
return NextResponse.json({ error: "查询打包任务失败,请稍后重试" }, { status: 500 });
|
return NextResponse.json({ success: false, error: "缺少任务 ID" }, { status: 400 });
|
||||||
|
}
|
||||||
|
|
||||||
|
const task = getTaskStore().get(taskId);
|
||||||
|
if (!task || task.userId !== session.user.id) {
|
||||||
|
return NextResponse.json({ success: false, error: "打包任务不存在或已过期" }, { status: 404 });
|
||||||
|
}
|
||||||
|
|
||||||
|
if (request.nextUrl.searchParams.get("download") === "1") {
|
||||||
|
if (task.status !== "completed" || !task.zipBuffer) {
|
||||||
|
return NextResponse.json({ success: false, error: "ZIP 文件尚未准备好" }, { status: 409 });
|
||||||
|
}
|
||||||
|
return new NextResponse(task.zipBuffer, {
|
||||||
|
status: 200,
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/zip",
|
||||||
|
"Content-Disposition": task.contentDisposition || buildContentDisposition(task.folderName),
|
||||||
|
"Content-Length": String(task.zipBuffer.length),
|
||||||
|
"Cache-Control": "no-store",
|
||||||
|
"X-Download-Success-Count": String(task.successCount),
|
||||||
|
"X-Download-Failed-Count": String(task.failedCount),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return NextResponse.json({
|
||||||
|
success: true,
|
||||||
|
task: serializeTask(task),
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error("[CanvasDownloadZipTask] Query failed:", error);
|
||||||
|
return NextResponse.json({ success: false, error: "查询打包任务失败" }, { status: 500 });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,4 +1,3 @@
|
|||||||
import { streamDownloadZip } from "@/lib/canvas/download-zip-stream";
|
|
||||||
import { NextRequest, NextResponse } from "next/server";
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
import JSZip from "jszip";
|
import JSZip from "jszip";
|
||||||
import { auth } from "@/app/(auth)/auth";
|
import { auth } from "@/app/(auth)/auth";
|
||||||
@ -160,7 +159,13 @@ export async function POST(request: NextRequest) {
|
|||||||
console.warn("[CanvasDownloadZip] Partial download failures:", failures);
|
console.warn("[CanvasDownloadZip] Partial download failures:", failures);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new NextResponse(streamDownloadZip(zip), {
|
const zipBuffer = await zip.generateAsync({
|
||||||
|
type: "nodebuffer",
|
||||||
|
compression: "DEFLATE",
|
||||||
|
compressionOptions: { level: 6 },
|
||||||
|
});
|
||||||
|
|
||||||
|
return new NextResponse(zipBuffer, {
|
||||||
status: 200,
|
status: 200,
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/zip",
|
"Content-Type": "application/zip",
|
||||||
|
|||||||
@ -1,10 +1,62 @@
|
|||||||
import { executeCanvasImageResponse } from "@/lib/canvas/generate-image-response";
|
|
||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
import { auth } from "@/app/(auth)/auth";
|
import { auth } from "@/app/(auth)/auth";
|
||||||
import { getCanvasImageTaskForUser } from "@/lib/canvas/image-task-queue";
|
import {
|
||||||
|
createCanvasImageTask,
|
||||||
|
enqueueCanvasImageTask,
|
||||||
|
getCanvasImageTaskForUser,
|
||||||
|
} from "@/lib/canvas/image-task-queue";
|
||||||
|
import {
|
||||||
|
generateCanvasImage,
|
||||||
|
type CanvasGenerateImageRequest,
|
||||||
|
} from "@/lib/canvas/generate-image";
|
||||||
|
import { getPointsSystemConfig } from "@/lib/db/queries";
|
||||||
|
import { prepareFeaturePointsCharge } from "@/lib/points/feature-charge";
|
||||||
|
import {
|
||||||
|
getImageToolCreditCost,
|
||||||
|
type ImageToolCreditFeature,
|
||||||
|
} from "@/lib/points/config";
|
||||||
|
|
||||||
export const maxDuration = 1200;
|
export const maxDuration = 1200;
|
||||||
|
|
||||||
|
interface GenerateImageRequest extends CanvasGenerateImageRequest {
|
||||||
|
async?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
function normalizeBillingFeature(
|
||||||
|
value: unknown
|
||||||
|
): Extract<ImageToolCreditFeature, "multi-view" | "repaint"> | null {
|
||||||
|
if (value === "multi-view" || value === "repaint") return value;
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
function toGenerateImagePayload(body: GenerateImageRequest): CanvasGenerateImageRequest {
|
||||||
|
return {
|
||||||
|
prompt: String(body.prompt || "").trim(),
|
||||||
|
negativePrompt:
|
||||||
|
typeof body.negativePrompt === "string" ? body.negativePrompt : undefined,
|
||||||
|
model: typeof body.model === "string" ? body.model : undefined,
|
||||||
|
aspectRatio:
|
||||||
|
typeof body.aspectRatio === "string" && body.aspectRatio.trim()
|
||||||
|
? body.aspectRatio.trim()
|
||||||
|
: "auto",
|
||||||
|
imageSize:
|
||||||
|
typeof body.imageSize === "string" && body.imageSize.trim()
|
||||||
|
? body.imageSize.trim()
|
||||||
|
: undefined,
|
||||||
|
imageCount:
|
||||||
|
typeof body.imageCount === "number" && Number.isFinite(body.imageCount)
|
||||||
|
? body.imageCount
|
||||||
|
: 1,
|
||||||
|
promptExtend:
|
||||||
|
typeof body.promptExtend === "boolean" ? body.promptExtend : undefined,
|
||||||
|
referenceImage:
|
||||||
|
typeof body.referenceImage === "string" ? body.referenceImage : undefined,
|
||||||
|
referenceImages: Array.isArray(body.referenceImages)
|
||||||
|
? body.referenceImages.map((item) => String(item || "").trim()).filter(Boolean)
|
||||||
|
: [],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export async function GET(request: Request) {
|
export async function GET(request: Request) {
|
||||||
try {
|
try {
|
||||||
const session = await auth();
|
const session = await auth();
|
||||||
@ -15,7 +67,10 @@ export async function GET(request: Request) {
|
|||||||
const { searchParams } = new URL(request.url);
|
const { searchParams } = new URL(request.url);
|
||||||
const taskId = String(searchParams.get("taskId") || "").trim();
|
const taskId = String(searchParams.get("taskId") || "").trim();
|
||||||
if (!taskId) {
|
if (!taskId) {
|
||||||
return NextResponse.json({ success: false, error: "缺少 taskId 参数" }, { status: 400 });
|
return NextResponse.json(
|
||||||
|
{ success: false, error: "缺少 taskId 参数" },
|
||||||
|
{ status: 400 }
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const task = await getCanvasImageTaskForUser({ taskId, userId: session.user.id });
|
const task = await getCanvasImageTaskForUser({ taskId, userId: session.user.id });
|
||||||
@ -44,15 +99,105 @@ export async function GET(request: Request) {
|
|||||||
success: false,
|
success: false,
|
||||||
error: error instanceof Error ? error.message : "查询任务失败",
|
error: error instanceof Error ? error.message : "查询任务失败",
|
||||||
},
|
},
|
||||||
{ status: 500 },
|
{ status: 500 }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function POST(request: Request) {
|
export async function POST(request: Request) {
|
||||||
const session = await auth();
|
try {
|
||||||
if (!session?.user?.id)
|
const session = await auth();
|
||||||
return NextResponse.json({ success: false, error: "未登录" }, { status: 401 });
|
if (!session?.user?.id) {
|
||||||
const body = await request.json().catch(() => ({}));
|
return NextResponse.json(
|
||||||
return executeCanvasImageResponse(body, session.user.id);
|
{ success: false, error: "未登录" },
|
||||||
|
{ status: 401 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const body: GenerateImageRequest = await request.json();
|
||||||
|
const payload: CanvasGenerateImageRequest = {
|
||||||
|
...toGenerateImagePayload(body),
|
||||||
|
ownerKey: session.user.id,
|
||||||
|
};
|
||||||
|
if (!payload.prompt) {
|
||||||
|
return NextResponse.json(
|
||||||
|
{ success: false, error: "请输入提示词" },
|
||||||
|
{ status: 400 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const billingFeature = normalizeBillingFeature(body.billingFeature);
|
||||||
|
const pointsCharge = billingFeature
|
||||||
|
? await (async () => {
|
||||||
|
const pointsConfig = await getPointsSystemConfig();
|
||||||
|
return prepareFeaturePointsCharge({
|
||||||
|
userId: session.user.id,
|
||||||
|
unitCost: getImageToolCreditCost(pointsConfig, billingFeature),
|
||||||
|
reason:
|
||||||
|
billingFeature === "multi-view"
|
||||||
|
? "image_multi_view"
|
||||||
|
: "image_repaint",
|
||||||
|
modelId: "canvas-generate-image",
|
||||||
|
note:
|
||||||
|
billingFeature === "multi-view"
|
||||||
|
? "图片多视角生成消耗积分"
|
||||||
|
: "图片重绘消耗积分",
|
||||||
|
pointsConfig,
|
||||||
|
});
|
||||||
|
})()
|
||||||
|
: null;
|
||||||
|
if (pointsCharge && !pointsCharge.ok) {
|
||||||
|
return NextResponse.json(pointsCharge.payload, { status: pointsCharge.status });
|
||||||
|
}
|
||||||
|
|
||||||
|
const asyncMode = body.async === true;
|
||||||
|
if (asyncMode) {
|
||||||
|
const task = await createCanvasImageTask({
|
||||||
|
userId: session.user.id,
|
||||||
|
request: payload as unknown as Record<string, unknown>,
|
||||||
|
});
|
||||||
|
await enqueueCanvasImageTask(task.id);
|
||||||
|
const currentTask = await getCanvasImageTaskForUser({
|
||||||
|
taskId: task.id,
|
||||||
|
userId: session.user.id,
|
||||||
|
});
|
||||||
|
if (pointsCharge?.chargeable) {
|
||||||
|
try {
|
||||||
|
await pointsCharge.consume({
|
||||||
|
note:
|
||||||
|
billingFeature === "multi-view"
|
||||||
|
? `图片多视角生成消耗积分(${task.id})`
|
||||||
|
: `图片重绘消耗积分(${task.id})`,
|
||||||
|
});
|
||||||
|
} catch (pointsError) {
|
||||||
|
console.error("[Points] Failed to consume canvas image task points:", pointsError);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return NextResponse.json({
|
||||||
|
success: true,
|
||||||
|
taskId: task.id,
|
||||||
|
status: currentTask?.status || task.status,
|
||||||
|
queueStatus: currentTask?.queueStatus,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = await generateCanvasImage(payload);
|
||||||
|
if (pointsCharge?.chargeable) {
|
||||||
|
try {
|
||||||
|
await pointsCharge.consume();
|
||||||
|
} catch (pointsError) {
|
||||||
|
console.error("[Points] Failed to consume canvas image points:", pointsError);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return NextResponse.json({
|
||||||
|
success: true,
|
||||||
|
data: result,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error("[Canvas Generate Image] Error:", error);
|
||||||
|
return NextResponse.json(
|
||||||
|
{ success: false, error: error instanceof Error ? error.message : "生成失败" },
|
||||||
|
{ status: 500 }
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,8 +1,7 @@
|
|||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
import { auth } from "@/app/(auth)/auth";
|
import { auth } from "@/app/(auth)/auth";
|
||||||
import { executeDirectVideoGeneration } from "@/lib/ai/tools/generate-video";
|
import { executeDirectVideoGeneration } from "@/lib/ai/tools/generate-video";
|
||||||
import { probeRemoteUrl } from "@/lib/net/url-access";
|
import { filterReachableRemoteUrls } from "@/lib/net/url-access";
|
||||||
import { validateVideoReferences } from "@/lib/video-canvas/reference-input";
|
|
||||||
import type { VideoGenerationMode, VideoRatio, VideoResolution } from "@/types/ai";
|
import type { VideoGenerationMode, VideoRatio, VideoResolution } from "@/types/ai";
|
||||||
|
|
||||||
export const maxDuration = 300;
|
export const maxDuration = 300;
|
||||||
@ -11,8 +10,6 @@ interface GenerateVideoRequest {
|
|||||||
prompt?: string;
|
prompt?: string;
|
||||||
mode?: VideoGenerationMode;
|
mode?: VideoGenerationMode;
|
||||||
images?: string[];
|
images?: string[];
|
||||||
audioUrls?: string[];
|
|
||||||
generateAudio?: boolean;
|
|
||||||
resolution?: VideoResolution;
|
resolution?: VideoResolution;
|
||||||
ratio?: VideoRatio;
|
ratio?: VideoRatio;
|
||||||
duration?: number;
|
duration?: number;
|
||||||
@ -49,18 +46,23 @@ export async function POST(request: Request) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const referenceCheck = await validateVideoReferences(body.images, url => probeRemoteUrl(url, { timeoutMs: 8000 }));
|
const inputImages = Array.isArray(body.images)
|
||||||
if (referenceCheck.error) {
|
? body.images.map((item) => String(item || "").trim()).filter(Boolean).slice(0, 4)
|
||||||
return NextResponse.json({ success: false, error: referenceCheck.error }, { status: referenceCheck.status });
|
: [];
|
||||||
|
const { kept: reachableImages, dropped: unreachableImages } =
|
||||||
|
await filterReachableRemoteUrls(inputImages, { timeoutMs: 3000 });
|
||||||
|
|
||||||
|
if (inputImages.length > 0 && reachableImages.length === 0) {
|
||||||
|
return NextResponse.json(
|
||||||
|
{ success: false, error: "参考图片链接不可访问,请重新上传后再试" },
|
||||||
|
{ status: 400 }
|
||||||
|
);
|
||||||
}
|
}
|
||||||
const reachableImages = referenceCheck.images;
|
|
||||||
|
|
||||||
const mode = body.mode || inferMode(reachableImages);
|
const mode = body.mode || inferMode(reachableImages);
|
||||||
const result = await executeDirectVideoGeneration({
|
const result = await executeDirectVideoGeneration({
|
||||||
prompt,
|
prompt,
|
||||||
mode,
|
mode,
|
||||||
audioUrls: Array.isArray(body.audioUrls) ? body.audioUrls.filter(url => typeof url === "string" && /^https?:\/\//i.test(url)).slice(0, 3) : undefined,
|
|
||||||
generateAudio: body.generateAudio,
|
|
||||||
images: reachableImages,
|
images: reachableImages,
|
||||||
resolution: body.resolution,
|
resolution: body.resolution,
|
||||||
ratio: body.ratio,
|
ratio: body.ratio,
|
||||||
@ -88,13 +90,13 @@ export async function POST(request: Request) {
|
|||||||
success: true,
|
success: true,
|
||||||
data: {
|
data: {
|
||||||
url: result.videoUrl,
|
url: result.videoUrl,
|
||||||
metadata: { ...result.metadata, temporaryVideoUrl: result.videoUrl, storageStatus: result.metadata?.permanentVideoUrl === result.videoUrl ? "archived" : "temporary" },
|
metadata: result.metadata || null,
|
||||||
modelDisplayName: result.modelDisplayName,
|
modelDisplayName: result.modelDisplayName,
|
||||||
modelId: result.modelId,
|
modelId: result.modelId,
|
||||||
modelRef: result.modelRef,
|
modelRef: result.modelRef,
|
||||||
mode,
|
mode,
|
||||||
images: reachableImages,
|
images: reachableImages,
|
||||||
droppedImages: [],
|
droppedImages: unreachableImages,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@ -1,130 +0,0 @@
|
|||||||
import { videoUpscaleSchema } from "@/lib/video-canvas/video-upscale-contract";
|
|
||||||
import { smartEditRequestSchema } from "@/lib/video-canvas/smart-edit/contract";
|
|
||||||
import { audioRequestSchema } from "@/lib/video-canvas/audio-contract";
|
|
||||||
import { canResumeCanvasTask } from "@/lib/video-canvas/task-recovery";
|
|
||||||
import { imageOperationSchema } from "@/lib/video-canvas/image-operation";
|
|
||||||
import { after, NextResponse } from "next/server";
|
|
||||||
import { auth } from "@/app/(auth)/auth";
|
|
||||||
import { z } from "zod";
|
|
||||||
import { canvasTaskRequestSchema } from "@/lib/video-canvas/task-contract";
|
|
||||||
import {
|
|
||||||
cancelCanvasGenerationTask,
|
|
||||||
createCanvasGenerationTask,
|
|
||||||
listCanvasGenerationTasks,
|
|
||||||
ownsTaskProject,
|
|
||||||
} from "@/lib/video-canvas/task-store";
|
|
||||||
import { runCanvasGenerationQueue } from "@/lib/video-canvas/task-worker";
|
|
||||||
import { preflightCanvasProductionRequests } from "@/lib/video-canvas/production-services";
|
|
||||||
export const runtime = "nodejs";
|
|
||||||
export const maxDuration = 900;
|
|
||||||
|
|
||||||
async function owner(projectId: unknown) {
|
|
||||||
const session = await auth();
|
|
||||||
if (!session?.user?.id) throw Object.assign(Error("未登录"), { status: 401 });
|
|
||||||
if (!z.string().uuid().safeParse(projectId).success)
|
|
||||||
throw Object.assign(Error("画布 ID 无效"), { status: 400 });
|
|
||||||
if (!(await ownsTaskProject(session.user.id, projectId as string)))
|
|
||||||
throw Object.assign(Error("画布不存在"), { status: 404 });
|
|
||||||
return session.user.id;
|
|
||||||
}
|
|
||||||
const fail = (error: unknown) =>
|
|
||||||
NextResponse.json(
|
|
||||||
{ success: false, error: error instanceof Error ? error.message : "任务操作失败", code: (error as any)?.code },
|
|
||||||
{ status: Number((error as any)?.status) || (error instanceof SyntaxError ? 400 : 500) },
|
|
||||||
);
|
|
||||||
const kick = (userId: string, projectId: string) =>
|
|
||||||
after(() =>
|
|
||||||
runCanvasGenerationQueue(userId, projectId).catch((error) =>
|
|
||||||
console.error("[Canvas tasks]", error),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
export async function POST(request: Request) {
|
|
||||||
try {
|
|
||||||
const text = await request.text();
|
|
||||||
if (new TextEncoder().encode(text).byteLength > 262144)
|
|
||||||
throw Object.assign(Error("任务数据过大,请使用已上传的素材链接"), { status: 413 });
|
|
||||||
const parsed = canvasTaskRequestSchema.safeParse(JSON.parse(text));
|
|
||||||
if (!parsed.success) throw Object.assign(Error("任务参数不合法"), { status: 400 });
|
|
||||||
if(parsed.data.workflow)throw Object.assign(Error("编组任务必须使用整组提交接口"),{status:400});
|
|
||||||
if (
|
|
||||||
parsed.data.kind === "image-operation" &&
|
|
||||||
!imageOperationSchema.safeParse(parsed.data.params).success
|
|
||||||
)
|
|
||||||
throw Object.assign(Error("图片处理参数不合法"), { status: 400 });
|
|
||||||
if (
|
|
||||||
parsed.data.attachment &&
|
|
||||||
((parsed.data.attachment.type === "story-asset" && parsed.data.kind !== "image") ||
|
|
||||||
(parsed.data.attachment.type === "story-prompts" && parsed.data.kind !== "analysis"))
|
|
||||||
)
|
|
||||||
throw Object.assign(Error("脚本结果类型不匹配"), { status: 400 });
|
|
||||||
if (parsed.data.kind === "audio" && !audioRequestSchema.safeParse(parsed.data.params).success)
|
|
||||||
throw Object.assign(Error("音频参数不合法"), { status: 400 });
|
|
||||||
if (
|
|
||||||
parsed.data.kind === "smart-edit" &&
|
|
||||||
!smartEditRequestSchema.safeParse(parsed.data.params).success
|
|
||||||
)
|
|
||||||
throw Object.assign(Error("剪辑参数无效"), { status: 400 });
|
|
||||||
if (parsed.data.kind === "video-upscale" && !videoUpscaleSchema.safeParse(parsed.data.params).success) throw Object.assign(Error("视频高清参数无效"), {status:400});
|
|
||||||
const userId = await owner(parsed.data.projectId);
|
|
||||||
const task = await createCanvasGenerationTask(userId, parsed.data,
|
|
||||||
() => preflightCanvasProductionRequests([parsed.data]));
|
|
||||||
kick(userId, parsed.data.projectId);
|
|
||||||
return NextResponse.json({ success: true, task }, { status: 202 });
|
|
||||||
} catch (error) {
|
|
||||||
return fail(error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
export async function GET(request: Request) {
|
|
||||||
try {
|
|
||||||
const projectId = new URL(request.url).searchParams.get("projectId") || "";
|
|
||||||
const userId = await owner(projectId);
|
|
||||||
const tasks = await listCanvasGenerationTasks(userId, projectId);
|
|
||||||
if (tasks.some((task) => task.status === "pending" || canResumeCanvasTask(task)))
|
|
||||||
kick(userId, projectId);
|
|
||||||
return NextResponse.json(
|
|
||||||
{ success: true, tasks },
|
|
||||||
{ headers: { "Cache-Control": "no-store" } },
|
|
||||||
);
|
|
||||||
} catch (error) {
|
|
||||||
return fail(error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
export async function DELETE(request: Request) {
|
|
||||||
try {
|
|
||||||
const { projectId, taskId } = await request.json();
|
|
||||||
const userId = await owner(projectId);
|
|
||||||
if (!z.string().uuid().safeParse(taskId).success)
|
|
||||||
throw Object.assign(Error("任务 ID 无效"), { status: 400 });
|
|
||||||
const result = await cancelCanvasGenerationTask(userId, projectId, taskId);
|
|
||||||
if (!result.task)
|
|
||||||
return NextResponse.json({ success: false, error: "任务不存在" }, { status: 404 });
|
|
||||||
if (!result.ok)
|
|
||||||
return NextResponse.json(
|
|
||||||
{
|
|
||||||
success: false,
|
|
||||||
error:
|
|
||||||
result.task.status === "running"
|
|
||||||
? "任务已提交供应商,当前线路不支持中止。结果会继续保存,请勿重复生成。"
|
|
||||||
: "任务已结束或状态待核实,请先刷新任务状态。",
|
|
||||||
task: result.task,
|
|
||||||
},
|
|
||||||
{ status: 409 },
|
|
||||||
);
|
|
||||||
return NextResponse.json({ success: true, task: result.task });
|
|
||||||
} catch (error) {
|
|
||||||
return fail(error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
export async function PATCH(request: Request) {
|
|
||||||
try {
|
|
||||||
const { projectId, taskId, nodeId } = await request.json();
|
|
||||||
const userId = await owner(projectId);
|
|
||||||
if (!z.string().uuid().safeParse(taskId).success || typeof nodeId !== "string")
|
|
||||||
return NextResponse.json({ success: false }, { status: 400 });
|
|
||||||
const { hideCanvasGenerationNode } = await import("@/lib/video-canvas/task-store");
|
|
||||||
const success = await hideCanvasGenerationNode(userId, projectId, taskId, nodeId);
|
|
||||||
return NextResponse.json({ success }, { status: success ? 200 : 404 });
|
|
||||||
} catch (error) {
|
|
||||||
return fail(error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,41 +0,0 @@
|
|||||||
import { auth } from "@/app/(auth)/auth";
|
|
||||||
import { after, NextResponse } from "next/server";
|
|
||||||
import { canvasTaskRequestSchema } from "@/lib/video-canvas/task-contract";
|
|
||||||
import {
|
|
||||||
createCanvasGroupTasks,
|
|
||||||
ownsTaskProject,
|
|
||||||
} from "@/lib/video-canvas/task-store";
|
|
||||||
import { runCanvasGenerationQueue } from "@/lib/video-canvas/task-worker";
|
|
||||||
import { preflightCanvasProductionRequests } from "@/lib/video-canvas/production-services";
|
|
||||||
export const runtime = "nodejs",
|
|
||||||
maxDuration = 900;
|
|
||||||
export async function POST(request: Request) {
|
|
||||||
try {
|
|
||||||
const session = await auth();
|
|
||||||
if (!session?.user?.id)
|
|
||||||
return NextResponse.json({ error: "未登录" }, { status: 401 });
|
|
||||||
const text = await request.text();
|
|
||||||
if (Buffer.byteLength(text) > 1048576)
|
|
||||||
return NextResponse.json({ error: "编组数据过大" }, { status: 413 });
|
|
||||||
const parsed = canvasTaskRequestSchema
|
|
||||||
.array()
|
|
||||||
.min(1)
|
|
||||||
.max(40)
|
|
||||||
.safeParse(JSON.parse(text));
|
|
||||||
if (!parsed.success)
|
|
||||||
return NextResponse.json({ error: "编组参数无效" }, { status: 400 });
|
|
||||||
const projectId = parsed.data[0].projectId,
|
|
||||||
userId = session.user.id;
|
|
||||||
if (!(await ownsTaskProject(userId, projectId)))
|
|
||||||
return NextResponse.json({ error: "画布不存在" }, { status: 404 });
|
|
||||||
const tasks = await createCanvasGroupTasks(userId, parsed.data,
|
|
||||||
() => preflightCanvasProductionRequests(parsed.data));
|
|
||||||
after(() => runCanvasGenerationQueue(userId, projectId));
|
|
||||||
return NextResponse.json({ success: true, tasks }, { status: 202 });
|
|
||||||
} catch (e) {
|
|
||||||
return NextResponse.json(
|
|
||||||
{ error: e instanceof Error ? e.message : "编组执行失败", code: (e as any)?.code },
|
|
||||||
{ status: Number((e as any)?.status) || 400 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,40 +0,0 @@
|
|||||||
import { auth } from "@/app/(auth)/auth";
|
|
||||||
import { db } from "@/lib/db";
|
|
||||||
import { canvasProject } from "@/lib/db/schema";
|
|
||||||
import { eq, and, sql, desc } from "drizzle-orm";
|
|
||||||
import { NextResponse } from "next/server";
|
|
||||||
export async function GET() {
|
|
||||||
const session = await auth();
|
|
||||||
if (!session?.user?.id)
|
|
||||||
return NextResponse.json({ error: "未登录" }, { status: 401 });
|
|
||||||
const rows = await db
|
|
||||||
.select({
|
|
||||||
projectId: canvasProject.id,
|
|
||||||
title: canvasProject.title,
|
|
||||||
templates: sql<any[]>`${canvasProject.canvasData}->'groupTemplates'`,
|
|
||||||
})
|
|
||||||
.from(canvasProject)
|
|
||||||
.where(
|
|
||||||
and(
|
|
||||||
eq(canvasProject.userId, session.user.id),
|
|
||||||
eq(canvasProject.workspaceType, "video"),
|
|
||||||
sql`${canvasProject.canvasData} ? 'groupTemplates'`
|
|
||||||
)
|
|
||||||
)
|
|
||||||
.orderBy(desc(canvasProject.updatedAt))
|
|
||||||
.limit(100);
|
|
||||||
return NextResponse.json(
|
|
||||||
{
|
|
||||||
templates: rows.flatMap((row) =>
|
|
||||||
Array.isArray(row.templates)
|
|
||||||
? row.templates.map((template) => ({
|
|
||||||
...template,
|
|
||||||
projectId: row.projectId,
|
|
||||||
projectTitle: row.title,
|
|
||||||
}))
|
|
||||||
: []
|
|
||||||
),
|
|
||||||
},
|
|
||||||
{ headers: { "Cache-Control": "no-store" } }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@ -1,143 +0,0 @@
|
|||||||
import { NextResponse } from "next/server";
|
|
||||||
import { auth } from "@/app/(auth)/auth";
|
|
||||||
import {
|
|
||||||
getCanvasLayoutReplaceBatchActiveTaskId,
|
|
||||||
getCanvasLayoutReplaceBatchForUser,
|
|
||||||
markCanvasLayoutReplaceBatchItemsAdded,
|
|
||||||
retryCanvasLayoutReplaceBatchItem,
|
|
||||||
} from "@/lib/canvas/layout-replace-batch";
|
|
||||||
import {
|
|
||||||
enqueueCanvasImageTask,
|
|
||||||
ensureCanvasImageTaskActive,
|
|
||||||
} from "@/lib/canvas/image-task-queue";
|
|
||||||
|
|
||||||
function normalizeUuid(value: unknown) {
|
|
||||||
const id = String(value || "").trim();
|
|
||||||
return /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(id)
|
|
||||||
? id
|
|
||||||
: "";
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function GET(
|
|
||||||
_request: Request,
|
|
||||||
{ params }: { params: Promise<{ batchId: string }> }
|
|
||||||
) {
|
|
||||||
try {
|
|
||||||
const session = await auth();
|
|
||||||
if (!session?.user?.id) {
|
|
||||||
return NextResponse.json({ success: false, error: "未登录" }, { status: 401 });
|
|
||||||
}
|
|
||||||
const batchId = normalizeUuid((await params).batchId);
|
|
||||||
if (!batchId) {
|
|
||||||
return NextResponse.json({ success: false, error: "批次ID无效" }, { status: 400 });
|
|
||||||
}
|
|
||||||
|
|
||||||
let batch = await getCanvasLayoutReplaceBatchForUser({
|
|
||||||
batchId,
|
|
||||||
userId: session.user.id,
|
|
||||||
});
|
|
||||||
if (!batch) {
|
|
||||||
return NextResponse.json({ success: false, error: "批次不存在" }, { status: 404 });
|
|
||||||
}
|
|
||||||
const activeTaskId = getCanvasLayoutReplaceBatchActiveTaskId(batch);
|
|
||||||
if (activeTaskId) {
|
|
||||||
await ensureCanvasImageTaskActive({
|
|
||||||
taskId: activeTaskId,
|
|
||||||
userId: session.user.id,
|
|
||||||
});
|
|
||||||
batch = await getCanvasLayoutReplaceBatchForUser({
|
|
||||||
batchId,
|
|
||||||
userId: session.user.id,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return NextResponse.json({ success: true, batch });
|
|
||||||
} catch (error) {
|
|
||||||
console.error("[Canvas Layout Replace Batch][Detail] Error:", error);
|
|
||||||
return NextResponse.json(
|
|
||||||
{
|
|
||||||
success: false,
|
|
||||||
error: error instanceof Error ? error.message : "查询批次失败",
|
|
||||||
},
|
|
||||||
{ status: 500 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function PATCH(
|
|
||||||
request: Request,
|
|
||||||
{ params }: { params: Promise<{ batchId: string }> }
|
|
||||||
) {
|
|
||||||
try {
|
|
||||||
const session = await auth();
|
|
||||||
if (!session?.user?.id) {
|
|
||||||
return NextResponse.json({ success: false, error: "未登录" }, { status: 401 });
|
|
||||||
}
|
|
||||||
const batchId = normalizeUuid((await params).batchId);
|
|
||||||
const body = await request.json().catch(() => ({}));
|
|
||||||
if (body?.action === "retry") {
|
|
||||||
const itemId = normalizeUuid(body?.itemId);
|
|
||||||
if (!batchId || !itemId) {
|
|
||||||
return NextResponse.json(
|
|
||||||
{ success: false, error: "缺少需要重试的结果" },
|
|
||||||
{ status: 400 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
const taskId = await retryCanvasLayoutReplaceBatchItem({
|
|
||||||
batchId,
|
|
||||||
itemId,
|
|
||||||
userId: session.user.id,
|
|
||||||
});
|
|
||||||
if (!taskId) {
|
|
||||||
return NextResponse.json(
|
|
||||||
{ success: false, error: "该结果当前无法重试" },
|
|
||||||
{ status: 409 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
await enqueueCanvasImageTask(taskId);
|
|
||||||
return NextResponse.json({ success: true, taskId });
|
|
||||||
}
|
|
||||||
|
|
||||||
const items = Array.isArray(body?.items)
|
|
||||||
? body.items
|
|
||||||
.map((item: unknown) => {
|
|
||||||
const record =
|
|
||||||
item && typeof item === "object" && !Array.isArray(item)
|
|
||||||
? (item as Record<string, unknown>)
|
|
||||||
: {};
|
|
||||||
return {
|
|
||||||
itemId: normalizeUuid(record.itemId),
|
|
||||||
canvasNodeId: String(record.canvasNodeId || "").trim(),
|
|
||||||
};
|
|
||||||
})
|
|
||||||
.filter(
|
|
||||||
(item: { itemId: string; canvasNodeId: string }) =>
|
|
||||||
Boolean(item.itemId && item.canvasNodeId)
|
|
||||||
)
|
|
||||||
: [];
|
|
||||||
if (!batchId || items.length === 0) {
|
|
||||||
return NextResponse.json(
|
|
||||||
{ success: false, error: "缺少已添加的结果信息" },
|
|
||||||
{ status: 400 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
const updated = await markCanvasLayoutReplaceBatchItemsAdded({
|
|
||||||
batchId,
|
|
||||||
userId: session.user.id,
|
|
||||||
items,
|
|
||||||
});
|
|
||||||
if (!updated) {
|
|
||||||
return NextResponse.json({ success: false, error: "批次不存在" }, { status: 404 });
|
|
||||||
}
|
|
||||||
return NextResponse.json({ success: true });
|
|
||||||
} catch (error) {
|
|
||||||
console.error("[Canvas Layout Replace Batch][PATCH] Error:", error);
|
|
||||||
return NextResponse.json(
|
|
||||||
{
|
|
||||||
success: false,
|
|
||||||
error: error instanceof Error ? error.message : "更新批次失败",
|
|
||||||
},
|
|
||||||
{ status: 500 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,201 +0,0 @@
|
|||||||
import { and, eq } from "drizzle-orm";
|
|
||||||
import { NextResponse } from "next/server";
|
|
||||||
import { auth } from "@/app/(auth)/auth";
|
|
||||||
import {
|
|
||||||
CANVAS_LAYOUT_REPLACE_BATCH_LIMIT,
|
|
||||||
createCanvasLayoutReplaceBatch,
|
|
||||||
getCanvasLayoutReplaceBatchActiveTaskId,
|
|
||||||
getLatestCanvasLayoutReplaceBatchForUser,
|
|
||||||
type CanvasLayoutReplaceBatchSource,
|
|
||||||
} from "@/lib/canvas/layout-replace-batch";
|
|
||||||
import {
|
|
||||||
enqueueCanvasImageTask,
|
|
||||||
ensureCanvasImageTaskActive,
|
|
||||||
} from "@/lib/canvas/image-task-queue";
|
|
||||||
import { db } from "@/lib/db";
|
|
||||||
import { canvasProject } from "@/lib/db/schema";
|
|
||||||
import { getPointsSystemConfig } from "@/lib/db/queries";
|
|
||||||
import { prepareFeaturePointsCharge } from "@/lib/points/feature-charge";
|
|
||||||
import { getImageToolCreditCost } from "@/lib/points/config";
|
|
||||||
|
|
||||||
export const maxDuration = 1200;
|
|
||||||
|
|
||||||
function normalizeHttpUrl(value: unknown) {
|
|
||||||
const url = String(value || "").trim();
|
|
||||||
return /^https?:\/\//i.test(url) ? url : "";
|
|
||||||
}
|
|
||||||
|
|
||||||
function normalizeUuid(value: unknown) {
|
|
||||||
const id = String(value || "").trim();
|
|
||||||
return /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(id)
|
|
||||||
? id
|
|
||||||
: "";
|
|
||||||
}
|
|
||||||
|
|
||||||
function normalizeSources(value: unknown): CanvasLayoutReplaceBatchSource[] {
|
|
||||||
if (!Array.isArray(value)) return [];
|
|
||||||
const seen = new Set<string>();
|
|
||||||
const sources: CanvasLayoutReplaceBatchSource[] = [];
|
|
||||||
for (const item of value) {
|
|
||||||
if (!item || typeof item !== "object" || Array.isArray(item)) continue;
|
|
||||||
const record = item as Record<string, unknown>;
|
|
||||||
const sourceImageUrl = normalizeHttpUrl(record.sourceImageUrl);
|
|
||||||
if (!sourceImageUrl || seen.has(sourceImageUrl)) continue;
|
|
||||||
seen.add(sourceImageUrl);
|
|
||||||
sources.push({
|
|
||||||
sourceImageUrl,
|
|
||||||
sourceKind: record.sourceKind === "upload" ? "upload" : "canvas",
|
|
||||||
sourceNodeId: String(record.sourceNodeId || "").trim() || undefined,
|
|
||||||
sourceTitle: String(record.sourceTitle || "").trim().slice(0, 256) || undefined,
|
|
||||||
});
|
|
||||||
if (sources.length >= CANVAS_LAYOUT_REPLACE_BATCH_LIMIT) break;
|
|
||||||
}
|
|
||||||
return sources;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function getOwnedProject(projectId: string, userId: string) {
|
|
||||||
const [project] = await db
|
|
||||||
.select({ id: canvasProject.id })
|
|
||||||
.from(canvasProject)
|
|
||||||
.where(and(eq(canvasProject.id, projectId), eq(canvasProject.userId, userId)))
|
|
||||||
.limit(1);
|
|
||||||
return project || null;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function GET(request: Request) {
|
|
||||||
try {
|
|
||||||
const session = await auth();
|
|
||||||
if (!session?.user?.id) {
|
|
||||||
return NextResponse.json({ success: false, error: "未登录" }, { status: 401 });
|
|
||||||
}
|
|
||||||
|
|
||||||
const { searchParams } = new URL(request.url);
|
|
||||||
const projectId = normalizeUuid(searchParams.get("projectId"));
|
|
||||||
const sourceNodeId = String(searchParams.get("sourceNodeId") || "").trim();
|
|
||||||
if (!projectId) {
|
|
||||||
return NextResponse.json(
|
|
||||||
{ success: false, error: "缺少有效的画布项目" },
|
|
||||||
{ status: 400 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if (!(await getOwnedProject(projectId, session.user.id))) {
|
|
||||||
return NextResponse.json({ success: false, error: "画布不存在" }, { status: 404 });
|
|
||||||
}
|
|
||||||
|
|
||||||
let batch = await getLatestCanvasLayoutReplaceBatchForUser({
|
|
||||||
projectId,
|
|
||||||
userId: session.user.id,
|
|
||||||
sourceNodeId: sourceNodeId || undefined,
|
|
||||||
});
|
|
||||||
const activeTaskId = batch
|
|
||||||
? getCanvasLayoutReplaceBatchActiveTaskId(batch)
|
|
||||||
: null;
|
|
||||||
if (activeTaskId) {
|
|
||||||
await ensureCanvasImageTaskActive({
|
|
||||||
taskId: activeTaskId,
|
|
||||||
userId: session.user.id,
|
|
||||||
});
|
|
||||||
batch = await getLatestCanvasLayoutReplaceBatchForUser({
|
|
||||||
projectId,
|
|
||||||
userId: session.user.id,
|
|
||||||
sourceNodeId: sourceNodeId || undefined,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return NextResponse.json({ success: true, batch: batch || null });
|
|
||||||
} catch (error) {
|
|
||||||
console.error("[Canvas Layout Replace Batch][GET] Error:", error);
|
|
||||||
return NextResponse.json(
|
|
||||||
{
|
|
||||||
success: false,
|
|
||||||
error: error instanceof Error ? error.message : "查询批次失败",
|
|
||||||
},
|
|
||||||
{ status: 500 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function POST(request: Request) {
|
|
||||||
try {
|
|
||||||
const session = await auth();
|
|
||||||
if (!session?.user?.id) {
|
|
||||||
return NextResponse.json({ success: false, error: "未登录" }, { status: 401 });
|
|
||||||
}
|
|
||||||
|
|
||||||
const body = await request.json().catch(() => ({}));
|
|
||||||
const projectId = normalizeUuid(body?.projectId);
|
|
||||||
const referenceImageUrl = normalizeHttpUrl(body?.referenceImageUrl);
|
|
||||||
const sources = normalizeSources(body?.sources);
|
|
||||||
if (!projectId || !(await getOwnedProject(projectId, session.user.id))) {
|
|
||||||
return NextResponse.json({ success: false, error: "画布不存在" }, { status: 404 });
|
|
||||||
}
|
|
||||||
if (sources.length === 0) {
|
|
||||||
return NextResponse.json(
|
|
||||||
{ success: false, error: "请至少添加一张待处理图片" },
|
|
||||||
{ status: 400 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if (!referenceImageUrl) {
|
|
||||||
return NextResponse.json(
|
|
||||||
{ success: false, error: "请上传目标模版图" },
|
|
||||||
{ status: 400 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const pointsConfig = await getPointsSystemConfig();
|
|
||||||
const perImageCost = getImageToolCreditCost(pointsConfig, "repaint");
|
|
||||||
const totalCost = perImageCost * sources.length;
|
|
||||||
const pointsCharge = await prepareFeaturePointsCharge({
|
|
||||||
userId: session.user.id,
|
|
||||||
unitCost: totalCost,
|
|
||||||
reason: "image_repaint",
|
|
||||||
modelId: "canvas-layout-replace-batch",
|
|
||||||
note: `批量排版替换 ${sources.length} 张`,
|
|
||||||
pointsConfig,
|
|
||||||
});
|
|
||||||
if (!pointsCharge.ok) {
|
|
||||||
return NextResponse.json(pointsCharge.payload, { status: pointsCharge.status });
|
|
||||||
}
|
|
||||||
|
|
||||||
const created = await createCanvasLayoutReplaceBatch({
|
|
||||||
userId: session.user.id,
|
|
||||||
projectId,
|
|
||||||
sources,
|
|
||||||
referenceImageUrl,
|
|
||||||
supplementalPrompt:
|
|
||||||
typeof body?.supplementalPrompt === "string"
|
|
||||||
? body.supplementalPrompt
|
|
||||||
: undefined,
|
|
||||||
imageSize: typeof body?.imageSize === "string" ? body.imageSize : "2K",
|
|
||||||
model: typeof body?.model === "string" ? body.model : undefined,
|
|
||||||
});
|
|
||||||
await enqueueCanvasImageTask(created.firstTaskId);
|
|
||||||
|
|
||||||
if (pointsCharge.chargeable) {
|
|
||||||
try {
|
|
||||||
await pointsCharge.consume({
|
|
||||||
note: `批量排版替换 ${sources.length} 张(${created.batchId})`,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
console.error("[Points] Failed to consume layout batch points:", error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return NextResponse.json({
|
|
||||||
success: true,
|
|
||||||
batchId: created.batchId,
|
|
||||||
firstTaskId: created.firstTaskId,
|
|
||||||
sourceCount: sources.length,
|
|
||||||
pointsCost: totalCost,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
console.error("[Canvas Layout Replace Batch][POST] Error:", error);
|
|
||||||
return NextResponse.json(
|
|
||||||
{
|
|
||||||
success: false,
|
|
||||||
error: error instanceof Error ? error.message : "创建批次失败",
|
|
||||||
},
|
|
||||||
{ status: 500 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,124 +0,0 @@
|
|||||||
import { NextResponse } from "next/server";
|
|
||||||
import { auth } from "@/app/(auth)/auth";
|
|
||||||
import {
|
|
||||||
createCanvasImageTask,
|
|
||||||
enqueueCanvasImageTask,
|
|
||||||
} from "@/lib/canvas/image-task-queue";
|
|
||||||
import { getPointsSystemConfig } from "@/lib/db/queries";
|
|
||||||
import { prepareFeaturePointsCharge } from "@/lib/points/feature-charge";
|
|
||||||
import { getImageToolCreditCost } from "@/lib/points/config";
|
|
||||||
import type { CanvasLayoutReplaceTaskRequest } from "@/lib/canvas/layout-replace-task";
|
|
||||||
|
|
||||||
export const maxDuration = 1200;
|
|
||||||
|
|
||||||
function normalizeHttpUrl(value: unknown) {
|
|
||||||
const url = String(value || "").trim();
|
|
||||||
return /^https?:\/\//i.test(url) ? url : "";
|
|
||||||
}
|
|
||||||
|
|
||||||
function normalizeUuid(value: unknown) {
|
|
||||||
const uuid = String(value || "").trim();
|
|
||||||
return /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(uuid)
|
|
||||||
? uuid
|
|
||||||
: undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function POST(request: Request) {
|
|
||||||
try {
|
|
||||||
const session = await auth();
|
|
||||||
if (!session?.user?.id) {
|
|
||||||
return NextResponse.json(
|
|
||||||
{ success: false, error: "未登录" },
|
|
||||||
{ status: 401 },
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const body = await request.json().catch(() => ({}));
|
|
||||||
const sourceImageUrl = normalizeHttpUrl(body?.sourceImageUrl);
|
|
||||||
const referenceImageUrl = normalizeHttpUrl(body?.referenceImageUrl);
|
|
||||||
if (!sourceImageUrl) {
|
|
||||||
return NextResponse.json(
|
|
||||||
{ success: false, error: "缺少当前产品图片" },
|
|
||||||
{ status: 400 },
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if (!referenceImageUrl) {
|
|
||||||
return NextResponse.json(
|
|
||||||
{ success: false, error: "缺少排版参考图" },
|
|
||||||
{ status: 400 },
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const pointsConfig = await getPointsSystemConfig();
|
|
||||||
const pointsCharge = await prepareFeaturePointsCharge({
|
|
||||||
userId: session.user.id,
|
|
||||||
unitCost: getImageToolCreditCost(pointsConfig, "repaint"),
|
|
||||||
reason: "image_repaint",
|
|
||||||
modelId: "canvas-layout-replace",
|
|
||||||
note: "排版替换消耗积分",
|
|
||||||
pointsConfig,
|
|
||||||
});
|
|
||||||
if (!pointsCharge.ok) {
|
|
||||||
return NextResponse.json(pointsCharge.payload, {
|
|
||||||
status: pointsCharge.status,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const taskRequest: CanvasLayoutReplaceTaskRequest = {
|
|
||||||
taskKind: "canvas-layout-replace",
|
|
||||||
sourceImageUrl,
|
|
||||||
referenceImageUrl,
|
|
||||||
supplementalPrompt:
|
|
||||||
typeof body?.supplementalPrompt === "string"
|
|
||||||
? body.supplementalPrompt
|
|
||||||
: undefined,
|
|
||||||
model: typeof body?.model === "string" ? body.model : undefined,
|
|
||||||
imageSize:
|
|
||||||
typeof body?.imageSize === "string" && body.imageSize.trim()
|
|
||||||
? body.imageSize.trim()
|
|
||||||
: "2K",
|
|
||||||
sourceNodeId:
|
|
||||||
typeof body?.sourceNodeId === "string" ? body.sourceNodeId : undefined,
|
|
||||||
agentProjectId: normalizeUuid(body?.agentProjectId),
|
|
||||||
referenceLabel:
|
|
||||||
typeof body?.referenceLabel === "string" ? body.referenceLabel : undefined,
|
|
||||||
debugMeta:
|
|
||||||
body?.debugMeta &&
|
|
||||||
typeof body.debugMeta === "object" &&
|
|
||||||
!Array.isArray(body.debugMeta)
|
|
||||||
? body.debugMeta
|
|
||||||
: undefined,
|
|
||||||
};
|
|
||||||
|
|
||||||
const task = await createCanvasImageTask({
|
|
||||||
userId: session.user.id,
|
|
||||||
request: taskRequest as unknown as Record<string, unknown>,
|
|
||||||
});
|
|
||||||
await enqueueCanvasImageTask(task.id);
|
|
||||||
|
|
||||||
if (pointsCharge.chargeable) {
|
|
||||||
try {
|
|
||||||
await pointsCharge.consume({
|
|
||||||
note: `排版替换消耗积分(${task.id})`,
|
|
||||||
});
|
|
||||||
} catch (pointsError) {
|
|
||||||
console.error("[Points] Failed to consume canvas layout replace points:", pointsError);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return NextResponse.json({
|
|
||||||
success: true,
|
|
||||||
taskId: task.id,
|
|
||||||
status: task.status,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
console.error("[Canvas Layout Replace] Error:", error);
|
|
||||||
return NextResponse.json(
|
|
||||||
{
|
|
||||||
success: false,
|
|
||||||
error: error instanceof Error ? error.message : "排版替换提交失败",
|
|
||||||
},
|
|
||||||
{ status: 500 },
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -21,13 +21,7 @@ import {
|
|||||||
} from "@/lib/canvas/scale-adjust-semantic";
|
} from "@/lib/canvas/scale-adjust-semantic";
|
||||||
import {
|
import {
|
||||||
getPointsSystemConfig,
|
getPointsSystemConfig,
|
||||||
getSiteSettings,
|
|
||||||
getSystemSetting,
|
|
||||||
} from "@/lib/db/queries";
|
} from "@/lib/db/queries";
|
||||||
import {
|
|
||||||
CANVAS_LOCAL_EDIT_CONFIG_KEY,
|
|
||||||
normalizeCanvasLocalEditConfig,
|
|
||||||
} from "@/lib/canvas/local-edit-config";
|
|
||||||
import { getImageToolCreditCost } from "@/lib/points/config";
|
import { getImageToolCreditCost } from "@/lib/points/config";
|
||||||
import { prepareFeaturePointsCharge } from "@/lib/points/feature-charge";
|
import { prepareFeaturePointsCharge } from "@/lib/points/feature-charge";
|
||||||
import { uploadFile } from "@/lib/storage/service";
|
import { uploadFile } from "@/lib/storage/service";
|
||||||
@ -624,23 +618,7 @@ export async function POST(request: NextRequest) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const [pointsConfig, siteSettingsForModel, localEditConfigSetting] = await Promise.all([
|
const pointsConfig = await getPointsSystemConfig();
|
||||||
getPointsSystemConfig(),
|
|
||||||
getSiteSettings().catch(() => null),
|
|
||||||
getSystemSetting({ key: CANVAS_LOCAL_EDIT_CONFIG_KEY }).catch(() => null),
|
|
||||||
]);
|
|
||||||
const localEditConfig = normalizeCanvasLocalEditConfig(
|
|
||||||
localEditConfigSetting?.value
|
|
||||||
);
|
|
||||||
const siteDetailDefaultModelId =
|
|
||||||
typeof siteSettingsForModel?.canvasDetailDefaultModelId === "string"
|
|
||||||
? siteSettingsForModel.canvasDetailDefaultModelId.trim()
|
|
||||||
: "";
|
|
||||||
const configuredLocalEditModelId = localEditConfig.modelId;
|
|
||||||
const configuredScaleAdjustModelId =
|
|
||||||
localEditConfig.scaleAdjustModelId ||
|
|
||||||
siteDetailDefaultModelId ||
|
|
||||||
SCALE_ADJUST_REPAIR_MODEL_REF;
|
|
||||||
const pointsCharge = await prepareFeaturePointsCharge({
|
const pointsCharge = await prepareFeaturePointsCharge({
|
||||||
userId: session.user.id,
|
userId: session.user.id,
|
||||||
unitCost: getImageToolCreditCost(pointsConfig, "repaint"),
|
unitCost: getImageToolCreditCost(pointsConfig, "repaint"),
|
||||||
@ -809,7 +787,7 @@ export async function POST(request: NextRequest) {
|
|||||||
operation,
|
operation,
|
||||||
requestedModel: requestedModel || null,
|
requestedModel: requestedModel || null,
|
||||||
resolvedAspectRatio: hasPositionMove ? "auto" : resolvedAspectRatio,
|
resolvedAspectRatio: hasPositionMove ? "auto" : resolvedAspectRatio,
|
||||||
resolvedModel: configuredScaleAdjustModelId,
|
resolvedModel: SCALE_ADJUST_REPAIR_MODEL_REF,
|
||||||
scaleAdjustProductLockMaskEditApplied: true,
|
scaleAdjustProductLockMaskEditApplied: true,
|
||||||
scaleAdjustPersonExclusionApplied:
|
scaleAdjustPersonExclusionApplied:
|
||||||
productLockAssets.personExclusionApplied,
|
productLockAssets.personExclusionApplied,
|
||||||
@ -836,7 +814,7 @@ export async function POST(request: NextRequest) {
|
|||||||
displayPrompt,
|
displayPrompt,
|
||||||
imageCount: 1,
|
imageCount: 1,
|
||||||
imageSize: requestedImageSize,
|
imageSize: requestedImageSize,
|
||||||
model: configuredScaleAdjustModelId,
|
model: SCALE_ADJUST_REPAIR_MODEL_REF,
|
||||||
negativePrompt:
|
negativePrompt:
|
||||||
"框线、箭头、坐标、标注、文字、UI、缩放整张图、裁剪整张图、拉伸主体、复制主体、替换主体、旧主体残影",
|
"框线、箭头、坐标、标注、文字、UI、缩放整张图、裁剪整张图、拉伸主体、复制主体、替换主体、旧主体残影",
|
||||||
prompt,
|
prompt,
|
||||||
@ -869,7 +847,7 @@ export async function POST(request: NextRequest) {
|
|||||||
sourceCropImageUrl: semanticSourceCropUploads[index]?.url || "",
|
sourceCropImageUrl: semanticSourceCropUploads[index]?.url || "",
|
||||||
target: region.targetRect,
|
target: region.targetRect,
|
||||||
})),
|
})),
|
||||||
relightModel: configuredScaleAdjustModelId,
|
relightModel: SCALE_ADJUST_REPAIR_MODEL_REF,
|
||||||
sourceImageUrl,
|
sourceImageUrl,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -898,7 +876,7 @@ export async function POST(request: NextRequest) {
|
|||||||
...(hasPositionMove
|
...(hasPositionMove
|
||||||
? {
|
? {
|
||||||
scaleAdjustModelRelight: {
|
scaleAdjustModelRelight: {
|
||||||
model: configuredScaleAdjustModelId,
|
model: SCALE_ADJUST_REPAIR_MODEL_REF,
|
||||||
overlayImageUrl: overlayUpload.url,
|
overlayImageUrl: overlayUpload.url,
|
||||||
quality: "medium" as const,
|
quality: "medium" as const,
|
||||||
regions: scaleAdjustIntentRegions.map((region) => ({
|
regions: scaleAdjustIntentRegions.map((region) => ({
|
||||||
@ -997,7 +975,6 @@ export async function POST(request: NextRequest) {
|
|||||||
displayPrompt,
|
displayPrompt,
|
||||||
operation,
|
operation,
|
||||||
requestedModel: requestedModel || null,
|
requestedModel: requestedModel || null,
|
||||||
resolvedModel: requestedModel || configuredLocalEditModelId || null,
|
|
||||||
resolvedAspectRatio,
|
resolvedAspectRatio,
|
||||||
manualRegionReferenceImageCount: manualRegionReferenceImageUrls.length,
|
manualRegionReferenceImageCount: manualRegionReferenceImageUrls.length,
|
||||||
regionReferenceImageCount: referenceImages.length,
|
regionReferenceImageCount: referenceImages.length,
|
||||||
@ -1009,7 +986,7 @@ export async function POST(request: NextRequest) {
|
|||||||
displayPrompt,
|
displayPrompt,
|
||||||
imageCount: 1,
|
imageCount: 1,
|
||||||
imageSize: requestedImageSize,
|
imageSize: requestedImageSize,
|
||||||
model: requestedModel || configuredLocalEditModelId || undefined,
|
model: requestedModel || undefined,
|
||||||
negativePrompt:
|
negativePrompt:
|
||||||
"彩色框、蓝色边框、绿色边框、红色边框、橙色边框、矩形框、编号圆点、提示词气泡、标注文字、UI 控件、半透明色块、选区边框、白边、画幅压扁、画面拉伸、主体截断、贴边裁切、矩形贴片、硬边、拼接线、补丁感、色差边缘、背景错位、地面纹理断裂、椅脚错位",
|
"彩色框、蓝色边框、绿色边框、红色边框、橙色边框、矩形框、编号圆点、提示词气泡、标注文字、UI 控件、半透明色块、选区边框、白边、画幅压扁、画面拉伸、主体截断、贴边裁切、矩形贴片、硬边、拼接线、补丁感、色差边缘、背景错位、地面纹理断裂、椅脚错位",
|
||||||
prompt,
|
prompt,
|
||||||
|
|||||||
@ -1,65 +0,0 @@
|
|||||||
import { execFile } from "node:child_process";
|
|
||||||
import { randomUUID } from "node:crypto";
|
|
||||||
import { mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
||||||
import { tmpdir } from "node:os";
|
|
||||||
import { join } from "node:path";
|
|
||||||
import { promisify } from "node:util";
|
|
||||||
import { NextResponse } from "next/server";
|
|
||||||
import { auth } from "@/app/(auth)/auth";
|
|
||||||
import { uploadFile } from "@/lib/storage/service";
|
|
||||||
|
|
||||||
export const runtime = "nodejs";
|
|
||||||
export const maxDuration = 180;
|
|
||||||
const probe = promisify(execFile);
|
|
||||||
const running = new Set<string>();
|
|
||||||
const formats: Record<string, string> = {
|
|
||||||
"video/mp4": "mp4", "video/quicktime": "mov", "video/webm": "webm",
|
|
||||||
"audio/mpeg": "mp3", "audio/mp3": "mp3", "audio/mp4": "m4a", "audio/x-m4a": "m4a",
|
|
||||||
"audio/wav": "wav", "audio/x-wav": "wav", "audio/ogg": "ogg", "audio/flac": "flac", "audio/webm": "webm",
|
|
||||||
};
|
|
||||||
|
|
||||||
export async function POST(request: Request) {
|
|
||||||
const session = await auth();
|
|
||||||
if (!session?.user?.id) return NextResponse.json({ success: false, error: "未登录" }, { status: 401 });
|
|
||||||
if (running.has(session.user.id) || running.size >= 4) return NextResponse.json({ success: false, error: "正在上传,请稍后重试" }, { status: 429 });
|
|
||||||
running.add(session.user.id);
|
|
||||||
let dir = "";
|
|
||||||
try {
|
|
||||||
const form = await request.formData();
|
|
||||||
const file = form.get("file");
|
|
||||||
if (!(file instanceof File) || !formats[file.type]) return NextResponse.json({ success: false, error: "请上传 MP4、MOV、WebM 视频,或 MP3、M4A、WAV、OGG、FLAC 音频" }, { status: 400 });
|
|
||||||
if (!file.size || file.size > 125 * 1024 * 1024) return NextResponse.json({ success: false, error: "素材必须非空且不超过 125 MiB" }, { status: 413 });
|
|
||||||
dir = await mkdtemp(join(tmpdir(), "canvas-upload-"));
|
|
||||||
const input = join(dir, "input");
|
|
||||||
const buffer = await file.arrayBuffer();
|
|
||||||
await writeFile(input, Buffer.from(buffer));
|
|
||||||
let stdout: string;
|
|
||||||
try {
|
|
||||||
({ stdout } = await probe("ffprobe", ["-v", "error", "-protocol_whitelist", "file", "-format_whitelist", "mov,matroska,webm,mp3,wav,ogg,flac", "-show_streams", "-show_format", "-of", "json", input], { timeout: 15000, maxBuffer: 1024 * 1024 }));
|
|
||||||
} catch (error) {
|
|
||||||
if ((error as NodeJS.ErrnoException).code === "ENOENT") {
|
|
||||||
return NextResponse.json({ success: false, error: "服务器缺少 ffprobe 视频解析组件,请联系管理员安装 FFmpeg" }, { status: 503 });
|
|
||||||
}
|
|
||||||
return NextResponse.json({ success: false, error: "视频文件无法解析,请确认素材完整且格式受支持" }, { status: 400 });
|
|
||||||
}
|
|
||||||
const info = JSON.parse(stdout);
|
|
||||||
const kind = file.type.startsWith("video/") ? "video" : "audio";
|
|
||||||
const stream = info.streams?.find((item: { codec_type: string }) => item.codec_type === kind);
|
|
||||||
const duration = Number(info.format?.duration);
|
|
||||||
if (!stream || !Number.isFinite(duration) || duration <= 0 || duration > 600) return NextResponse.json({ success: false, error: "请上传内容完整、10 分钟以内的音视频" }, { status: 400 });
|
|
||||||
let uploaded;
|
|
||||||
try {
|
|
||||||
uploaded = await uploadFile(`canvas-${randomUUID()}.${formats[file.type]}`, buffer);
|
|
||||||
} catch (error) {
|
|
||||||
console.error("[Canvas media upload] Storage unavailable:", error);
|
|
||||||
return NextResponse.json({ success: false, error: "媒体存储暂不可用,请稍后重试" }, { status: 503 });
|
|
||||||
}
|
|
||||||
return NextResponse.json({ success: true, data: { url: uploaded.url, duration, width: stream.width, height: stream.height } });
|
|
||||||
} catch (error) {
|
|
||||||
console.error("[Canvas media upload] Failed:", error);
|
|
||||||
return NextResponse.json({ success: false, error: "素材上传失败,请确认文件完整或稍后重试" }, { status: 400 });
|
|
||||||
} finally {
|
|
||||||
running.delete(session.user.id);
|
|
||||||
if (dir) await rm(dir, { recursive: true, force: true });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,61 +0,0 @@
|
|||||||
import { generateText } from "ai";
|
|
||||||
import { createLogger } from "@/lib/observability/logger";
|
|
||||||
const logger = createLogger("canvas-prompt-tools");
|
|
||||||
import { NextResponse } from "next/server";
|
|
||||||
import { auth } from "@/app/(auth)/auth";
|
|
||||||
import { getLanguageModelById } from "@/lib/ai/dynamic-provider";
|
|
||||||
import { getEnabledAiModelsWithProvider, getSystemSetting } from "@/lib/db/queries";
|
|
||||||
import { VIDEO_CANVAS_FUNCTION_CONFIG_KEY, normalizeVideoCanvasFunctionConfig } from "@/lib/video-canvas/config";
|
|
||||||
import { promptToolErrorMessage, promptToolFunctionKey, promptToolSchema, selectPromptToolModel } from "@/lib/video-canvas/prompt-tools";
|
|
||||||
|
|
||||||
export const runtime = "nodejs";
|
|
||||||
export const maxDuration = 120;
|
|
||||||
const running = new Set<string>();
|
|
||||||
|
|
||||||
export async function POST(request: Request) {
|
|
||||||
const session = await auth();
|
|
||||||
if (!session?.user?.id) return NextResponse.json({ success: false, error: "未登录" }, { status: 401 });
|
|
||||||
const parsed = promptToolSchema.safeParse(await request.json().catch(() => null));
|
|
||||||
if (!parsed.success) return NextResponse.json({ success: false, error: parsed.error.issues[0]?.message || "参数无效" }, { status: 400 });
|
|
||||||
if (running.has(session.user.id)) return NextResponse.json({ success: false, error: "提示词处理中,请稍候" }, { status: 429 });
|
|
||||||
running.add(session.user.id);
|
|
||||||
try {
|
|
||||||
const [setting, available] = await Promise.all([
|
|
||||||
getSystemSetting({ key: VIDEO_CANVAS_FUNCTION_CONFIG_KEY }),
|
|
||||||
getEnabledAiModelsWithProvider(false),
|
|
||||||
]);
|
|
||||||
const config = normalizeVideoCanvasFunctionConfig(setting?.value);
|
|
||||||
const definition = config.functions[promptToolFunctionKey(parsed.data.mode)];
|
|
||||||
if (!config.enabled || !definition?.enabled) return NextResponse.json({ success: false, error: "此提示词功能已停用" }, { status: 403 });
|
|
||||||
const requested = parsed.data.model && parsed.data.model !== "auto"
|
|
||||||
? parsed.data.model : definition.model && definition.model !== "auto"
|
|
||||||
? definition.model : config.defaultMultimodalModel;
|
|
||||||
let selected;
|
|
||||||
try {
|
|
||||||
selected = selectPromptToolModel(available
|
|
||||||
.filter(item => item.provider.isEnabled && item.model.isEnabled && !item.model.isSubModel)
|
|
||||||
.map(item => ({ ...item.model, providerType: item.provider.type })), requested);
|
|
||||||
} catch (error) {
|
|
||||||
return NextResponse.json({ success: false, error: (error as Error).message }, { status: 400 });
|
|
||||||
}
|
|
||||||
const model = await getLanguageModelById(selected.id);
|
|
||||||
if (!model) return NextResponse.json({ success: false, error: "提示词模型初始化失败" }, { status: 502 });
|
|
||||||
const { text } = await generateText({
|
|
||||||
model,
|
|
||||||
system: definition.prompt,
|
|
||||||
prompt: parsed.data.prompt,
|
|
||||||
temperature: parsed.data.mode === "translate" ? 0.1 : 0.4,
|
|
||||||
maxOutputTokens: 4000,
|
|
||||||
maxRetries: 0,
|
|
||||||
abortSignal: AbortSignal.any([request.signal, AbortSignal.timeout(90_000)]),
|
|
||||||
});
|
|
||||||
if (!text.trim()) throw new Error("empty model output");
|
|
||||||
return NextResponse.json({ success: true, data: { text: text.trim(), model: selected.id, modelName: selected.displayName || selected.modelId } });
|
|
||||||
} catch (error) {
|
|
||||||
const detail = error as { name?: string; statusCode?: number; cause?: { code?: string }; data?: { error?: { code?: string } } };
|
|
||||||
logger.warn("Prompt tool request failed", { name: detail?.name, statusCode: detail?.statusCode, code: detail?.data?.error?.code || detail?.cause?.code });
|
|
||||||
return NextResponse.json({ success: false, error: promptToolErrorMessage(error) }, { status: 502 });
|
|
||||||
} finally {
|
|
||||||
running.delete(session.user.id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -100,8 +100,6 @@ export async function POST(request: NextRequest) {
|
|||||||
.insert(canvasProject)
|
.insert(canvasProject)
|
||||||
.values({
|
.values({
|
||||||
userId: session.user.id,
|
userId: session.user.id,
|
||||||
createdAt: new Date(),
|
|
||||||
updatedAt: new Date(),
|
|
||||||
title: title || getDefaultWorkspaceTitle(workspaceType),
|
title: title || getDefaultWorkspaceTitle(workspaceType),
|
||||||
description,
|
description,
|
||||||
workspaceType,
|
workspaceType,
|
||||||
|
|||||||
@ -1,68 +0,0 @@
|
|||||||
import { remoteAudioCapabilities } from "@/lib/video-canvas/smart-edit/remote-audio";
|
|
||||||
import { auth } from "@/app/(auth)/auth";
|
|
||||||
import { NextResponse } from "next/server";
|
|
||||||
import { execFile } from "node:child_process";
|
|
||||||
import { promisify } from "node:util";
|
|
||||||
import path from "node:path";
|
|
||||||
import { getEnabledAiModelsWithProvider } from "@/lib/db/queries";
|
|
||||||
import { isCanvasVisionModel } from "@/lib/video-canvas/model-matching";
|
|
||||||
import { remoteRenderConfig } from "@/lib/video-canvas/smart-edit/remote-render";
|
|
||||||
export async function GET() {
|
|
||||||
const session = await auth();
|
|
||||||
if (!session?.user?.id)
|
|
||||||
return NextResponse.json({ error: "未登录" }, { status: 401 });
|
|
||||||
try {
|
|
||||||
let capabilities = {
|
|
||||||
separation: false,
|
|
||||||
scenes: false,
|
|
||||||
beats: false,
|
|
||||||
asr: false,
|
|
||||||
ffmpeg: false,
|
|
||||||
ffprobe: false,
|
|
||||||
};
|
|
||||||
try {
|
|
||||||
const { stdout } = await promisify(execFile)(
|
|
||||||
process.env.SMART_EDIT_PYTHON || "python3",
|
|
||||||
[path.resolve("services/smart-edit/analyze.py"), "--check"],
|
|
||||||
{ timeout: 10000, maxBuffer: 65536 }
|
|
||||||
);
|
|
||||||
capabilities = JSON.parse(stdout);
|
|
||||||
} catch {}
|
|
||||||
const audio = await remoteAudioCapabilities();
|
|
||||||
if (audio) capabilities = { ...capabilities, ...audio };
|
|
||||||
let renderAvailable = capabilities.ffmpeg;
|
|
||||||
const remote = remoteRenderConfig();
|
|
||||||
if (remote) {
|
|
||||||
try {
|
|
||||||
const response = await fetch(remote.base + "/health", {
|
|
||||||
headers: { authorization: "Bearer " + remote.token },
|
|
||||||
signal: AbortSignal.timeout(3000),
|
|
||||||
redirect: "error",
|
|
||||||
cache: "no-store",
|
|
||||||
});
|
|
||||||
renderAvailable =
|
|
||||||
response.ok && (await response.json()).status === "ok";
|
|
||||||
} catch {
|
|
||||||
renderAvailable = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
const models = (await getEnabledAiModelsWithProvider(false))
|
|
||||||
.filter((item) =>
|
|
||||||
isCanvasVisionModel({ ...item.model, providerType: item.provider.type })
|
|
||||||
)
|
|
||||||
.map(({ model }) => ({
|
|
||||||
id: model.id,
|
|
||||||
name: model.displayName,
|
|
||||||
modelId: model.modelId,
|
|
||||||
}));
|
|
||||||
return NextResponse.json(
|
|
||||||
{ capabilities: { ...capabilities, render: renderAvailable }, models },
|
|
||||||
{ headers: { "Cache-Control": "no-store" } }
|
|
||||||
);
|
|
||||||
} catch {
|
|
||||||
return NextResponse.json(
|
|
||||||
{ error: "剪辑服务配置读取失败" },
|
|
||||||
{ status: 503 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,22 +0,0 @@
|
|||||||
import { auth } from "@/app/(auth)/auth";
|
|
||||||
import { captionFont } from "@/lib/video-canvas/smart-edit/fonts";
|
|
||||||
import { captionFontFile } from "@/lib/video-canvas/smart-edit/font-files";
|
|
||||||
import { readFile } from "node:fs/promises";
|
|
||||||
export const runtime = "nodejs";
|
|
||||||
export const maxDuration = 180;
|
|
||||||
export async function GET(_request: Request, { params }: { params: Promise<{ id: string }> }) {
|
|
||||||
if (!(await auth())?.user?.id) return Response.json({ error: "未登录" }, { status: 401 });
|
|
||||||
const { id } = await params;
|
|
||||||
if (!captionFont(id)) return Response.json({ error: "字体不存在" }, { status: 404 });
|
|
||||||
try {
|
|
||||||
const data = await readFile(await captionFontFile(id));
|
|
||||||
return new Response(new Uint8Array(data), {
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "font/ttf",
|
|
||||||
"Cache-Control": "private, max-age=31536000, immutable",
|
|
||||||
},
|
|
||||||
});
|
|
||||||
} catch {
|
|
||||||
return Response.json({ error: "字体加载失败,请检查字体服务配置后重试" }, { status: 503 });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -12,9 +12,6 @@ import { createFalImage } from "@/lib/ai/providers/fal-image";
|
|||||||
import { createSnapgenImage } from "@/lib/ai/providers/snapgen-image";
|
import { createSnapgenImage } from "@/lib/ai/providers/snapgen-image";
|
||||||
import { createOpenAIImage } from "@/lib/ai/providers/openai-image";
|
import { createOpenAIImage } from "@/lib/ai/providers/openai-image";
|
||||||
import { createBananaImage } from "@/lib/ai/providers/bananaimage";
|
import { createBananaImage } from "@/lib/ai/providers/bananaimage";
|
||||||
import { createVisionary } from "@/lib/ai/providers/visionary";
|
|
||||||
import { createAtlasCloud } from "@/lib/ai/providers/atlascloud";
|
|
||||||
import { createVip98k } from "@/lib/ai/providers/vip98k";
|
|
||||||
import { createStepfunImage } from "@/lib/ai/providers/stepfun-image";
|
import { createStepfunImage } from "@/lib/ai/providers/stepfun-image";
|
||||||
import { createMidjourney } from "@/lib/ai/providers/midjourney";
|
import { createMidjourney } from "@/lib/ai/providers/midjourney";
|
||||||
import { createRunway } from "@/lib/ai/providers/runway";
|
import { createRunway } from "@/lib/ai/providers/runway";
|
||||||
@ -29,9 +26,8 @@ export const runtime = "nodejs";
|
|||||||
export const maxDuration = 1200;
|
export const maxDuration = 1200;
|
||||||
|
|
||||||
const logger = createLogger("canvas-text-edit");
|
const logger = createLogger("canvas-text-edit");
|
||||||
const TEXT_EDIT_SOURCE_ARCHIVE_TIMEOUT_MS = 120_000;
|
const TEXT_EDIT_SOURCE_ARCHIVE_TIMEOUT_MS = 20_000;
|
||||||
const TEXT_EDIT_MODEL_FALLBACK_LIMIT = 3;
|
const TEXT_EDIT_MODEL_FALLBACK_LIMIT = 3;
|
||||||
const TEXT_EDIT_GENERATION_TIMEOUT_MS = 180_000;
|
|
||||||
|
|
||||||
const textItemSchema = z.object({
|
const textItemSchema = z.object({
|
||||||
id: z.string().min(1),
|
id: z.string().min(1),
|
||||||
@ -57,9 +53,6 @@ const SUPPORTED_TEXT_EDIT_PROVIDER_TYPES = new Set([
|
|||||||
"snapgen-image",
|
"snapgen-image",
|
||||||
"openai-image",
|
"openai-image",
|
||||||
"bananaimage",
|
"bananaimage",
|
||||||
"visionary",
|
|
||||||
"atlascloud",
|
|
||||||
"vip98k",
|
|
||||||
"stepfun-image",
|
"stepfun-image",
|
||||||
"midjourney",
|
"midjourney",
|
||||||
"runway",
|
"runway",
|
||||||
@ -69,13 +62,6 @@ const SUPPORTED_TEXT_EDIT_PROVIDER_TYPES = new Set([
|
|||||||
|
|
||||||
type TextEditModelCandidate = Awaited<ReturnType<typeof getEnabledAiModelsWithProvider>>[number];
|
type TextEditModelCandidate = Awaited<ReturnType<typeof getEnabledAiModelsWithProvider>>[number];
|
||||||
|
|
||||||
function isExplicitTextEditModel(candidate: TextEditModelCandidate) {
|
|
||||||
const capabilities = candidate.model.capabilities as Record<string, unknown> | null;
|
|
||||||
const kind = String(capabilities?.kind || "").toLowerCase();
|
|
||||||
const modelId = String(candidate.model.modelId || "").toLowerCase();
|
|
||||||
return kind === "image-edit" || /(?:^|[/_-])edit(?:$|[/_-])/.test(modelId);
|
|
||||||
}
|
|
||||||
|
|
||||||
function getTextEditModelCandidates(
|
function getTextEditModelCandidates(
|
||||||
models: Awaited<ReturnType<typeof getEnabledAiModelsWithProvider>>
|
models: Awaited<ReturnType<typeof getEnabledAiModelsWithProvider>>
|
||||||
) {
|
) {
|
||||||
@ -90,19 +76,16 @@ function getTextEditModelCandidates(
|
|||||||
}) &&
|
}) &&
|
||||||
item.model.isEnabled &&
|
item.model.isEnabled &&
|
||||||
item.provider.isEnabled &&
|
item.provider.isEnabled &&
|
||||||
(!item.model.excludeFromFailover || item.model.isDefaultTextEditModel) &&
|
|
||||||
SUPPORTED_TEXT_EDIT_PROVIDER_TYPES.has(item.provider.type),
|
SUPPORTED_TEXT_EDIT_PROVIDER_TYPES.has(item.provider.type),
|
||||||
)
|
)
|
||||||
.sort((a, b) => {
|
.sort((a, b) => {
|
||||||
const aScore =
|
const aScore =
|
||||||
(a.model.isDefaultTextEditModel ? 100 : 0) +
|
(a.model.isDefaultTextEditModel ? 100 : 0) +
|
||||||
(isExplicitTextEditModel(a) ? 30 : 0) +
|
|
||||||
(a.model.isDefault ? 10 : 0) +
|
(a.model.isDefault ? 10 : 0) +
|
||||||
(a.model.modelId?.toLowerCase().includes("seedream") ? 5 : 0) +
|
(a.model.modelId?.toLowerCase().includes("seedream") ? 5 : 0) +
|
||||||
(a.model.displayName?.includes("火山") ? 3 : 0);
|
(a.model.displayName?.includes("火山") ? 3 : 0);
|
||||||
const bScore =
|
const bScore =
|
||||||
(b.model.isDefaultTextEditModel ? 100 : 0) +
|
(b.model.isDefaultTextEditModel ? 100 : 0) +
|
||||||
(isExplicitTextEditModel(b) ? 30 : 0) +
|
|
||||||
(b.model.isDefault ? 10 : 0) +
|
(b.model.isDefault ? 10 : 0) +
|
||||||
(b.model.modelId?.toLowerCase().includes("seedream") ? 5 : 0) +
|
(b.model.modelId?.toLowerCase().includes("seedream") ? 5 : 0) +
|
||||||
(b.model.displayName?.includes("火山") ? 3 : 0);
|
(b.model.displayName?.includes("火山") ? 3 : 0);
|
||||||
@ -136,12 +119,6 @@ function createTextEditProvider(
|
|||||||
return createOpenAIImage({ baseURL, apiKey });
|
return createOpenAIImage({ baseURL, apiKey });
|
||||||
case "bananaimage":
|
case "bananaimage":
|
||||||
return createBananaImage({ baseURL, apiKey });
|
return createBananaImage({ baseURL, apiKey });
|
||||||
case "visionary":
|
|
||||||
return createVisionary({ baseURL, apiKey });
|
|
||||||
case "atlascloud":
|
|
||||||
return createAtlasCloud({ baseURL, apiKey });
|
|
||||||
case "vip98k":
|
|
||||||
return createVip98k({ baseURL, apiKey });
|
|
||||||
case "stepfun-image":
|
case "stepfun-image":
|
||||||
return createStepfunImage({ baseURL, apiKey });
|
return createStepfunImage({ baseURL, apiKey });
|
||||||
case "midjourney":
|
case "midjourney":
|
||||||
@ -231,13 +208,6 @@ async function resolveTextEditSourceImageUrl(imageUrl: string) {
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.warn("source_image_archive_failed", {
|
logger.warn("source_image_archive_failed", {
|
||||||
error: error instanceof Error ? error.message : String(error),
|
error: error instanceof Error ? error.message : String(error),
|
||||||
sourceHost: (() => {
|
|
||||||
try {
|
|
||||||
return new URL(imageUrl).host;
|
|
||||||
} catch {
|
|
||||||
return "invalid-url";
|
|
||||||
}
|
|
||||||
})(),
|
|
||||||
});
|
});
|
||||||
return imageUrl;
|
return imageUrl;
|
||||||
}
|
}
|
||||||
@ -501,7 +471,7 @@ async function generateTextEditImageWithFailover(params: {
|
|||||||
const runtimeModelId = resolveModelRuntimeId(selected.model, {
|
const runtimeModelId = resolveModelRuntimeId(selected.model, {
|
||||||
providerType: selected.provider.type,
|
providerType: selected.provider.type,
|
||||||
});
|
});
|
||||||
const generationOptions: any = {
|
const result = await provider.chat(runtimeModelId).doGenerate({
|
||||||
messages: [
|
messages: [
|
||||||
{
|
{
|
||||||
role: "user",
|
role: "user",
|
||||||
@ -513,20 +483,8 @@ async function generateTextEditImageWithFailover(params: {
|
|||||||
],
|
],
|
||||||
imageParams: {
|
imageParams: {
|
||||||
imageCount: 1,
|
imageCount: 1,
|
||||||
timeoutMs: TEXT_EDIT_GENERATION_TIMEOUT_MS,
|
|
||||||
},
|
},
|
||||||
};
|
});
|
||||||
if (selected.provider.type === "atlascloud" || selected.provider.type === "vip98k") {
|
|
||||||
generationOptions.imageGenConfig = selected.model.imageGenConfig || null;
|
|
||||||
}
|
|
||||||
const modelInstance =
|
|
||||||
selected.provider.type === "vip98k"
|
|
||||||
? (provider as any).chat(runtimeModelId, {
|
|
||||||
capabilities: selected.model.capabilities || null,
|
|
||||||
imageGenConfig: selected.model.imageGenConfig || null,
|
|
||||||
})
|
|
||||||
: provider.chat(runtimeModelId);
|
|
||||||
const result = await modelInstance.doGenerate(generationOptions);
|
|
||||||
|
|
||||||
const resultText = String(result?.text || "").trim();
|
const resultText = String(result?.text || "").trim();
|
||||||
const finishReason = String(result?.finishReason || "").toLowerCase();
|
const finishReason = String(result?.finishReason || "").toLowerCase();
|
||||||
|
|||||||
@ -1,78 +0,0 @@
|
|||||||
import { NextResponse } from "next/server";
|
|
||||||
import { z } from "zod";
|
|
||||||
import { auth } from "@/app/(auth)/auth";
|
|
||||||
import { getChatById, getMessagesByChatId } from "@/lib/db/queries";
|
|
||||||
import {
|
|
||||||
readOwnedVideoCanvas,
|
|
||||||
saveVideoAgentCanvas,
|
|
||||||
} from "@/lib/ai/canvas-agent-runtime/video-store";
|
|
||||||
import { AgentRunError } from "@/lib/ai/canvas-agent-runtime/store";
|
|
||||||
import { publicAgentError } from "@/lib/ai/canvas-agent-runtime/protocol";
|
|
||||||
|
|
||||||
export const dynamic = "force-dynamic";
|
|
||||||
export async function GET(request: Request) {
|
|
||||||
const session = await auth();
|
|
||||||
if (!session?.user?.id) return NextResponse.json({ error: "未登录" }, { status: 401 });
|
|
||||||
try {
|
|
||||||
const projectId = z.string().uuid().parse(new URL(request.url).searchParams.get("projectId"));
|
|
||||||
await readOwnedVideoCanvas(session.user.id, projectId);
|
|
||||||
// One stable conversation per video workspace survives browser reloads.
|
|
||||||
const chat = await getChatById({ id: projectId });
|
|
||||||
if (chat && chat.userId !== session.user.id) throw new AgentRunError("对话不存在", 404);
|
|
||||||
const messages = chat ? (await getMessagesByChatId({ id: projectId })).slice(-40) : [];
|
|
||||||
return NextResponse.json(
|
|
||||||
{ messages: messages.map(({ id, role, parts }) => ({ id, role, parts })) },
|
|
||||||
{ headers: { "Cache-Control": "private, no-store" } },
|
|
||||||
);
|
|
||||||
} catch (error) {
|
|
||||||
return NextResponse.json(
|
|
||||||
{ error: publicAgentError(error) },
|
|
||||||
{
|
|
||||||
status:
|
|
||||||
error instanceof AgentRunError ? error.status : error instanceof z.ZodError ? 400 : 503,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const saveSchema = z.object({
|
|
||||||
projectId: z.string().uuid(),
|
|
||||||
baseRevision: z.string().datetime(),
|
|
||||||
canvasData: z.object({
|
|
||||||
nodes: z.array(z.record(z.unknown())).max(2000),
|
|
||||||
edges: z
|
|
||||||
.array(
|
|
||||||
z.object({
|
|
||||||
id: z.string(),
|
|
||||||
source: z.string(),
|
|
||||||
target: z.string(),
|
|
||||||
type: z.string().optional(),
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
.max(4000),
|
|
||||||
selectedNodeIds: z.array(z.string()).max(20),
|
|
||||||
viewport: z.object({
|
|
||||||
x: z.number().finite(),
|
|
||||||
y: z.number().finite(),
|
|
||||||
zoom: z.number().positive(),
|
|
||||||
}),
|
|
||||||
tool: z.literal("select"),
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
export async function POST(request: Request) {
|
|
||||||
const session = await auth();
|
|
||||||
if (!session?.user?.id) return NextResponse.json({ error: "未登录" }, { status: 401 });
|
|
||||||
try {
|
|
||||||
const body = saveSchema.parse(await request.json());
|
|
||||||
await saveVideoAgentCanvas(session.user.id, body.projectId, body.baseRevision, body.canvasData);
|
|
||||||
return NextResponse.json({ success: true });
|
|
||||||
} catch (error) {
|
|
||||||
return NextResponse.json(
|
|
||||||
{ error: publicAgentError(error) },
|
|
||||||
{
|
|
||||||
status:
|
|
||||||
error instanceof AgentRunError ? error.status : error instanceof z.ZodError ? 400 : 503,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,39 +0,0 @@
|
|||||||
import { auth } from "@/app/(auth)/auth";
|
|
||||||
import { upscaleConfig } from "@/lib/video-canvas/video-upscale-remote";
|
|
||||||
import { NextResponse } from "next/server";
|
|
||||||
export async function GET() {
|
|
||||||
if (!(await auth())?.user?.id)
|
|
||||||
return NextResponse.json({ error: "未登录" }, { status: 401 });
|
|
||||||
try {
|
|
||||||
const config = upscaleConfig();
|
|
||||||
if (!config)
|
|
||||||
return NextResponse.json({
|
|
||||||
available: false,
|
|
||||||
message: "视频高清服务尚未接通",
|
|
||||||
});
|
|
||||||
const response = await fetch(config.base + "/health", {
|
|
||||||
headers: { authorization: "Bearer " + config.token },
|
|
||||||
signal: AbortSignal.timeout(5000),
|
|
||||||
redirect: "error",
|
|
||||||
cache: "no-store",
|
|
||||||
});
|
|
||||||
if (!response.ok) throw Error("unavailable");
|
|
||||||
const data = await response.json();
|
|
||||||
return NextResponse.json(
|
|
||||||
{
|
|
||||||
available: data.status === "ok",
|
|
||||||
engine: "Real-ESRGAN",
|
|
||||||
resolutions: ["1080P", "2K", "4K"],
|
|
||||||
maxScale: 4,
|
|
||||||
maxSeconds: 120,
|
|
||||||
queued: data.queued,
|
|
||||||
},
|
|
||||||
{ headers: { "Cache-Control": "no-store" } }
|
|
||||||
);
|
|
||||||
} catch {
|
|
||||||
return NextResponse.json(
|
|
||||||
{ available: false, message: "视频高清服务暂时不可用,请稍后重试" },
|
|
||||||
{ headers: { "Cache-Control": "no-store" } }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,23 +0,0 @@
|
|||||||
import { NextResponse } from "next/server";
|
|
||||||
import { z } from "zod";
|
|
||||||
import { auth } from "@/app/(auth)/auth";
|
|
||||||
import { archiveCanvasVideo } from "@/lib/video-canvas/archive-video";
|
|
||||||
|
|
||||||
export const runtime = "nodejs";
|
|
||||||
export const maxDuration = 300;
|
|
||||||
const running = new Set<string>();
|
|
||||||
const schema = z.object({ url: z.string().url().max(12000) });
|
|
||||||
export async function POST(request: Request) {
|
|
||||||
const session = await auth();
|
|
||||||
if (!session?.user?.id) return NextResponse.json({ success: false, error: "未登录" }, { status: 401 });
|
|
||||||
const parsed = schema.safeParse(await request.json().catch(() => null));
|
|
||||||
if (!parsed.success) return NextResponse.json({ success: false, error: "视频地址无效" }, { status: 400 });
|
|
||||||
if (running.has(session.user.id)) return NextResponse.json({ success: false, error: "视频正在保存,请稍候" }, { status: 429 });
|
|
||||||
running.add(session.user.id);
|
|
||||||
try {
|
|
||||||
const url = await archiveCanvasVideo(parsed.data.url);
|
|
||||||
return NextResponse.json({ success: true, data: { url, storageStatus: "archived", permanentVideoUrl: url } });
|
|
||||||
} catch {
|
|
||||||
return NextResponse.json({ success: false, error: "视频转存失败,原视频仍保留;请稍后重试保存,无需重新生成" }, { status: 502 });
|
|
||||||
} finally { running.delete(session.user.id); }
|
|
||||||
}
|
|
||||||
@ -1,96 +0,0 @@
|
|||||||
import { auth } from "@/app/(auth)/auth";
|
|
||||||
import { NextResponse } from "next/server";
|
|
||||||
import { mkdtemp, readFile, rm } from "node:fs/promises";
|
|
||||||
import { tmpdir } from "node:os";
|
|
||||||
import { join } from "node:path";
|
|
||||||
import { randomUUID } from "node:crypto";
|
|
||||||
import { uploadFile } from "@/lib/storage/service";
|
|
||||||
import { downloadCanvasMedia } from "@/lib/video-canvas/download-media";
|
|
||||||
import { processVideoFile } from "@/lib/video-canvas/media-processing";
|
|
||||||
import { videoProcessSchema } from "@/lib/video-canvas/media-contract";
|
|
||||||
export const runtime = "nodejs";
|
|
||||||
export const maxDuration = 300;
|
|
||||||
const running = new Set<string>();
|
|
||||||
export async function POST(request: Request) {
|
|
||||||
const session = await auth();
|
|
||||||
if (!session?.user?.id)
|
|
||||||
return NextResponse.json({ success: false, error: "未登录" }, { status: 401 });
|
|
||||||
const parsed = videoProcessSchema.safeParse(await request.json().catch(() => null));
|
|
||||||
if (!parsed.success)
|
|
||||||
return NextResponse.json({ success: false, error: "视频处理参数无效" }, { status: 400 });
|
|
||||||
if (running.has(session.user.id) || running.size >= 2)
|
|
||||||
return NextResponse.json(
|
|
||||||
{ success: false, error: "视频处理中,请稍后再提交" },
|
|
||||||
{ status: 429 },
|
|
||||||
);
|
|
||||||
running.add(session.user.id);
|
|
||||||
let dir = "";
|
|
||||||
try {
|
|
||||||
dir = await mkdtemp(join(tmpdir(), "canvas-video-"));
|
|
||||||
const input = join(dir, "input");
|
|
||||||
await downloadCanvasMedia(parsed.data.url, input);
|
|
||||||
const save = async (name: string) => {
|
|
||||||
const buffer = await readFile(join(dir, name));
|
|
||||||
return (
|
|
||||||
await uploadFile(`canvas-video-${randomUUID()}-${name}`, new Uint8Array(buffer).buffer)
|
|
||||||
).url;
|
|
||||||
};
|
|
||||||
if (parsed.data.operation === "frame" || parsed.data.operation === "slow") {
|
|
||||||
const name = parsed.data.operation === "frame" ? "frame.png" : "slow.mp4";
|
|
||||||
const metadata = await processVideoFile(
|
|
||||||
input,
|
|
||||||
join(dir, name),
|
|
||||||
parsed.data.operation,
|
|
||||||
undefined,
|
|
||||||
undefined,
|
|
||||||
parsed.data,
|
|
||||||
);
|
|
||||||
return NextResponse.json({ success: true, data: { url: await save(name), ...metadata } });
|
|
||||||
}
|
|
||||||
if (parsed.data.operation === "trim") {
|
|
||||||
const metadata = await processVideoFile(
|
|
||||||
input,
|
|
||||||
join(dir, "clip.mp4"),
|
|
||||||
"trim",
|
|
||||||
undefined,
|
|
||||||
parsed.data.range,
|
|
||||||
);
|
|
||||||
return NextResponse.json({
|
|
||||||
success: true,
|
|
||||||
data: { url: await save("clip.mp4"), ...metadata },
|
|
||||||
});
|
|
||||||
}
|
|
||||||
if (parsed.data.operation === "crop") {
|
|
||||||
const metadata = await processVideoFile(
|
|
||||||
input,
|
|
||||||
join(dir, "crop.mp4"),
|
|
||||||
"crop",
|
|
||||||
parsed.data.rect,
|
|
||||||
);
|
|
||||||
return NextResponse.json({
|
|
||||||
success: true,
|
|
||||||
data: { url: await save("crop.mp4"), ...metadata },
|
|
||||||
});
|
|
||||||
}
|
|
||||||
const metadata = await processVideoFile(input, join(dir, "audio.m4a"), "extract-audio");
|
|
||||||
await processVideoFile(input, join(dir, "silent.mp4"), "silent-video");
|
|
||||||
return NextResponse.json({
|
|
||||||
success: true,
|
|
||||||
data: { audioUrl: await save("audio.m4a"), url: await save("silent.mp4"), ...metadata },
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
const message = error instanceof Error ? error.message : "视频处理失败";
|
|
||||||
return NextResponse.json(
|
|
||||||
{
|
|
||||||
success: false,
|
|
||||||
error: message.startsWith("Command failed")
|
|
||||||
? "视频处理失败,请确认文件完整且格式受支持"
|
|
||||||
: message,
|
|
||||||
},
|
|
||||||
{ status: 400 },
|
|
||||||
);
|
|
||||||
} finally {
|
|
||||||
running.delete(session.user.id);
|
|
||||||
if (dir) await rm(dir, { recursive: true, force: true });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
File diff suppressed because it is too large
Load Diff
@ -7,16 +7,9 @@ import { resolveThreeDModelAliasId } from "@/lib/model3d/model-alias-config";
|
|||||||
import { getThreeDModelAliasConfigForRuntime } from "@/lib/model3d/model-alias-config-server";
|
import { getThreeDModelAliasConfigForRuntime } from "@/lib/model3d/model-alias-config-server";
|
||||||
import { createLogger } from "@/lib/observability/logger";
|
import { createLogger } from "@/lib/observability/logger";
|
||||||
import { uploadFile } from "@/lib/storage/service";
|
import { uploadFile } from "@/lib/storage/service";
|
||||||
import {
|
import { WorldLabsApiError, worldLabsRequest } from "@/lib/worldlabs/client";
|
||||||
isWorldLabsExternalTaskApi,
|
|
||||||
WORLDLABS_EXTERNAL_TASK_MODEL,
|
|
||||||
WorldLabsApiError,
|
|
||||||
worldLabsRequest,
|
|
||||||
} from "@/lib/worldlabs/client";
|
|
||||||
import { getWorldLabsProviderConfig } from "@/lib/worldlabs/server";
|
import { getWorldLabsProviderConfig } from "@/lib/worldlabs/server";
|
||||||
import type {
|
import type {
|
||||||
WorldLabsExternalTaskResponse,
|
|
||||||
WorldLabsExternalTaskResultData,
|
|
||||||
WorldLabsOperation,
|
WorldLabsOperation,
|
||||||
WorldLabsWorld,
|
WorldLabsWorld,
|
||||||
WorldLabsWorldAssets,
|
WorldLabsWorldAssets,
|
||||||
@ -190,48 +183,6 @@ function assertArchivedAssetBuffer(params: {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function verifyArchivedAssetUpload(params: {
|
|
||||||
url: string;
|
|
||||||
expectedBytes: number;
|
|
||||||
validateAs?: ArchiveValidationKind;
|
|
||||||
}) {
|
|
||||||
const headResponse = await fetch(params.url, {
|
|
||||||
method: "HEAD",
|
|
||||||
cache: "no-store",
|
|
||||||
});
|
|
||||||
if (!headResponse.ok) {
|
|
||||||
throw new Error(`归档文件校验失败(HTTP ${headResponse.status})`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const archivedLength = Number(headResponse.headers.get("content-length"));
|
|
||||||
if (
|
|
||||||
Number.isFinite(archivedLength) &&
|
|
||||||
archivedLength > 0 &&
|
|
||||||
archivedLength !== params.expectedBytes
|
|
||||||
) {
|
|
||||||
throw new Error(
|
|
||||||
`归档文件大小不一致(源文件 ${params.expectedBytes},归档文件 ${archivedLength})`
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!params.validateAs) return;
|
|
||||||
|
|
||||||
const rangeResponse = await fetch(params.url, {
|
|
||||||
method: "GET",
|
|
||||||
headers: { Range: "bytes=0-63" },
|
|
||||||
cache: "no-store",
|
|
||||||
});
|
|
||||||
if (!rangeResponse.ok) {
|
|
||||||
throw new Error(`归档文件头校验失败(HTTP ${rangeResponse.status})`);
|
|
||||||
}
|
|
||||||
const headerBuffer = await rangeResponse.arrayBuffer();
|
|
||||||
assertArchivedAssetBuffer({
|
|
||||||
buffer: headerBuffer,
|
|
||||||
contentType: rangeResponse.headers.get("content-type"),
|
|
||||||
validateAs: params.validateAs,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async function archiveRemoteAssetIfNeeded(params: {
|
async function archiveRemoteAssetIfNeeded(params: {
|
||||||
cacheKey: string;
|
cacheKey: string;
|
||||||
sourceUrl?: string | null;
|
sourceUrl?: string | null;
|
||||||
@ -291,11 +242,6 @@ async function archiveRemoteAssetIfNeeded(params: {
|
|||||||
);
|
);
|
||||||
const filename = `${params.filenamePrefix}.${ext}`;
|
const filename = `${params.filenamePrefix}.${ext}`;
|
||||||
const uploaded = await uploadFile(filename, assetBuffer);
|
const uploaded = await uploadFile(filename, assetBuffer);
|
||||||
await verifyArchivedAssetUpload({
|
|
||||||
url: uploaded.url,
|
|
||||||
expectedBytes: assetBuffer.byteLength,
|
|
||||||
validateAs: params.validateAs,
|
|
||||||
});
|
|
||||||
cache.set(params.cacheKey, {
|
cache.set(params.cacheKey, {
|
||||||
sourceUrl,
|
sourceUrl,
|
||||||
archivedUrl: uploaded.url,
|
archivedUrl: uploaded.url,
|
||||||
@ -346,8 +292,7 @@ async function archiveRemoteAssetMapIfNeeded(params: {
|
|||||||
)
|
)
|
||||||
).filter((entry): entry is readonly [string, string] => Boolean(entry));
|
).filter((entry): entry is readonly [string, string] => Boolean(entry));
|
||||||
|
|
||||||
if (archivedEntries.length !== entries.length) return null;
|
return archivedEntries.length > 0 ? Object.fromEntries(archivedEntries) : null;
|
||||||
return Object.fromEntries(archivedEntries);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getStringValue(...values: unknown[]) {
|
function getStringValue(...values: unknown[]) {
|
||||||
@ -416,96 +361,33 @@ async function updatePersistedWorldAsset(params: {
|
|||||||
function extractWorldId(operation: WorldLabsOperation) {
|
function extractWorldId(operation: WorldLabsOperation) {
|
||||||
const response = getObjectValue(operation.response);
|
const response = getObjectValue(operation.response);
|
||||||
const metadata = getObjectValue(operation.metadata);
|
const metadata = getObjectValue(operation.metadata);
|
||||||
const nestedWorldId =
|
return getStringValue(
|
||||||
getStringValue(response?.world_id, response?.worldId) ||
|
response?.world_id,
|
||||||
findNestedString(response, ["world_id", "worldId"]);
|
response?.worldId,
|
||||||
const metadataWorldId =
|
metadata?.world_id,
|
||||||
getStringValue(metadata?.world_id, metadata?.worldId) ||
|
metadata?.worldId
|
||||||
findNestedString(metadata, ["world_id", "worldId"]);
|
);
|
||||||
|
|
||||||
return getStringValue(nestedWorldId, metadataWorldId);
|
|
||||||
}
|
|
||||||
|
|
||||||
function isWorldLikePayload(value: unknown): value is WorldLabsWorld {
|
|
||||||
const record = getObjectValue(value);
|
|
||||||
if (!record) return false;
|
|
||||||
|
|
||||||
const hasCoreWorldField =
|
|
||||||
typeof record.world_id === "string" ||
|
|
||||||
typeof record.world_marble_url === "string" ||
|
|
||||||
typeof record.display_name === "string" ||
|
|
||||||
typeof record.worldId === "string";
|
|
||||||
const hasPossibleWorldContainerFields = record.assets || record.permission || record.tags || record.world_prompt;
|
|
||||||
const hasModelAliasOnly =
|
|
||||||
typeof record.model === "string" && /world/i.test(record.model);
|
|
||||||
|
|
||||||
return Boolean(hasCoreWorldField || hasPossibleWorldContainerFields || hasModelAliasOnly);
|
|
||||||
}
|
|
||||||
|
|
||||||
function extractWorldCandidate(value: unknown, depth = 0): WorldLabsWorld | null {
|
|
||||||
if (depth > 8) return null;
|
|
||||||
const record = getObjectValue(value);
|
|
||||||
if (!record) return null;
|
|
||||||
|
|
||||||
if (isWorldLikePayload(record)) return record;
|
|
||||||
|
|
||||||
const childCandidates: Array<Record<string, unknown> | null> = [
|
|
||||||
getObjectValue(record.world),
|
|
||||||
getObjectValue(record.result),
|
|
||||||
getObjectValue(record.output),
|
|
||||||
getObjectValue(record.data),
|
|
||||||
getObjectValue(record.payload),
|
|
||||||
];
|
|
||||||
|
|
||||||
for (const child of childCandidates) {
|
|
||||||
if (!child) continue;
|
|
||||||
if (isWorldLikePayload(child)) return child;
|
|
||||||
const nested = extractWorldCandidate(child, depth + 1);
|
|
||||||
if (nested) return nested;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const child of Object.values(record)) {
|
|
||||||
const nested = extractWorldCandidate(child, depth + 1);
|
|
||||||
if (nested) return nested;
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function extractWorldFromOperation(operation: WorldLabsOperation) {
|
function extractWorldFromOperation(operation: WorldLabsOperation) {
|
||||||
return extractWorldCandidate(operation.response);
|
const response = operation.response;
|
||||||
|
if (response && typeof response === "object" && "world_marble_url" in (response as Record<string, unknown>)) {
|
||||||
|
return response as WorldLabsWorld;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
function hasUsableWorldAssets(world?: WorldLabsWorld | null) {
|
function hasUsableWorldAssets(world?: WorldLabsWorld | null) {
|
||||||
const assets = world?.assets || null;
|
const assets = world?.assets || null;
|
||||||
if (!assets) return false;
|
if (!assets) return false;
|
||||||
const meshRecord = getObjectValue(assets.mesh);
|
const spzUrls = assets.splats?.spz_urls;
|
||||||
const imageryRecord = getObjectValue(assets.imagery);
|
|
||||||
const splatsRecord = getObjectValue(assets.splats);
|
|
||||||
const spzUrls =
|
|
||||||
splatsRecord?.spz_urls || splatsRecord?.spzUrls || splatsRecord?.splat_urls;
|
|
||||||
return Boolean(
|
return Boolean(
|
||||||
getStringValue(assets.thumbnail_url) ||
|
getStringValue(assets.thumbnail_url) ||
|
||||||
getStringValue(
|
getStringValue(assets.imagery?.pano_url) ||
|
||||||
imageryRecord?.pano_url,
|
getStringValue(assets.mesh?.collider_mesh_url) ||
|
||||||
imageryRecord?.panoUrl,
|
|
||||||
imageryRecord?.panorama_url,
|
|
||||||
imageryRecord?.panoramaUrl
|
|
||||||
) ||
|
|
||||||
getStringValue(
|
|
||||||
meshRecord?.collider_mesh_url,
|
|
||||||
meshRecord?.colliderMeshUrl,
|
|
||||||
meshRecord?.collider_mesh,
|
|
||||||
meshRecord?.mesh_url,
|
|
||||||
meshRecord?.meshUrl,
|
|
||||||
meshRecord?.model_url,
|
|
||||||
meshRecord?.modelUrl
|
|
||||||
) ||
|
|
||||||
(spzUrls &&
|
(spzUrls &&
|
||||||
typeof spzUrls === "object" &&
|
typeof spzUrls === "object" &&
|
||||||
Object.values(spzUrls).some(
|
Object.values(spzUrls).some((value) => typeof value === "string" && value.trim()))
|
||||||
(value) => typeof value === "string" && value.trim()
|
|
||||||
))
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -537,214 +419,6 @@ function buildAssetPayload(
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function unwrapExternalTaskData<T extends Record<string, unknown>>(payload: unknown) {
|
|
||||||
const root = getObjectValue(payload);
|
|
||||||
const data = getObjectValue(root?.data);
|
|
||||||
return (data || root || {}) as T;
|
|
||||||
}
|
|
||||||
|
|
||||||
function normalizeExternalTaskStatus(status?: string | null, done?: boolean) {
|
|
||||||
const normalized = String(status || "").trim().toLowerCase();
|
|
||||||
if (done === true || ["succeeded", "success", "completed", "done"].includes(normalized)) {
|
|
||||||
return "success" as const;
|
|
||||||
}
|
|
||||||
if (["failed", "failure", "error", "cancelled", "canceled"].includes(normalized)) {
|
|
||||||
return "failed" as const;
|
|
||||||
}
|
|
||||||
return "running" as const;
|
|
||||||
}
|
|
||||||
|
|
||||||
function readStringRecord(value: unknown) {
|
|
||||||
const record = getObjectValue(value);
|
|
||||||
if (!record) return null;
|
|
||||||
const entries = Object.entries(record).filter(
|
|
||||||
(entry): entry is [string, string] =>
|
|
||||||
Boolean(entry[0]) && typeof entry[1] === "string" && entry[1].trim().length > 0
|
|
||||||
);
|
|
||||||
return entries.length > 0 ? Object.fromEntries(entries) : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
function findNestedString(
|
|
||||||
value: unknown,
|
|
||||||
keys: string[],
|
|
||||||
depth = 0
|
|
||||||
): string | null {
|
|
||||||
if (depth > 8) return null;
|
|
||||||
const record = getObjectValue(value);
|
|
||||||
if (!record) return null;
|
|
||||||
|
|
||||||
for (const key of keys) {
|
|
||||||
const directValue = getStringValue(record[key]);
|
|
||||||
if (directValue) return directValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const child of Object.values(record)) {
|
|
||||||
const found = findNestedString(child, keys, depth + 1);
|
|
||||||
if (found) return found;
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
function findNestedObject(
|
|
||||||
value: unknown,
|
|
||||||
keys: string[],
|
|
||||||
depth = 0
|
|
||||||
): Record<string, unknown> | null {
|
|
||||||
if (depth > 8) return null;
|
|
||||||
const record = getObjectValue(value);
|
|
||||||
if (!record) return null;
|
|
||||||
|
|
||||||
for (const key of keys) {
|
|
||||||
const directValue = getObjectValue(record[key]);
|
|
||||||
if (directValue) return directValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const child of Object.values(record)) {
|
|
||||||
const found = findNestedObject(child, keys, depth + 1);
|
|
||||||
if (found) return found;
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
function findNestedStringRecord(
|
|
||||||
value: unknown,
|
|
||||||
keys: string[],
|
|
||||||
depth = 0
|
|
||||||
): Record<string, string> | null {
|
|
||||||
if (depth > 8) return null;
|
|
||||||
const record = getObjectValue(value);
|
|
||||||
if (!record) return null;
|
|
||||||
|
|
||||||
for (const key of keys) {
|
|
||||||
const directValue = readStringRecord(record[key]);
|
|
||||||
if (directValue) return directValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const child of Object.values(record)) {
|
|
||||||
const found = findNestedStringRecord(child, keys, depth + 1);
|
|
||||||
if (found) return found;
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
function extractExternalTaskErrorMessage(
|
|
||||||
data: WorldLabsExternalTaskResultData,
|
|
||||||
payload: WorldLabsExternalTaskResponse<WorldLabsExternalTaskResultData>
|
|
||||||
) {
|
|
||||||
const dataError = getObjectValue(data.error);
|
|
||||||
const payloadError = getObjectValue(payload.error);
|
|
||||||
return (
|
|
||||||
getStringValue(
|
|
||||||
data.errorMessage,
|
|
||||||
data.message,
|
|
||||||
dataError?.message,
|
|
||||||
payload.message,
|
|
||||||
payloadError?.message,
|
|
||||||
payload.error
|
|
||||||
) || "World Labs 外部任务失败"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
function extractExternalWorldAssetPayload(data: WorldLabsExternalTaskResultData) {
|
|
||||||
const root = getObjectValue(data) || {};
|
|
||||||
const resultRoot =
|
|
||||||
getObjectValue(root.result) ||
|
|
||||||
getObjectValue(root.output) ||
|
|
||||||
getObjectValue(root.world) ||
|
|
||||||
root;
|
|
||||||
const assets = findNestedObject(resultRoot, ["assets"]);
|
|
||||||
const worldAssets = assets as Partial<WorldLabsWorldAssets> | null;
|
|
||||||
const splats = getObjectValue(worldAssets?.splats);
|
|
||||||
|
|
||||||
return {
|
|
||||||
worldId: getStringValue(
|
|
||||||
findNestedString(resultRoot, ["worldId", "world_id"]),
|
|
||||||
findNestedString(root, ["worldId", "world_id"])
|
|
||||||
),
|
|
||||||
worldUrl: getStringValue(
|
|
||||||
findNestedString(resultRoot, [
|
|
||||||
"worldUrl",
|
|
||||||
"world_marble_url",
|
|
||||||
"sourceWorldUrl",
|
|
||||||
"viewerUrl",
|
|
||||||
"marbleUrl",
|
|
||||||
]),
|
|
||||||
findNestedString(root, [
|
|
||||||
"worldUrl",
|
|
||||||
"world_marble_url",
|
|
||||||
"sourceWorldUrl",
|
|
||||||
"viewerUrl",
|
|
||||||
"marbleUrl",
|
|
||||||
])
|
|
||||||
),
|
|
||||||
displayName: getStringValue(
|
|
||||||
findNestedString(resultRoot, ["displayName", "display_name"]),
|
|
||||||
findNestedString(root, ["displayName", "display_name"])
|
|
||||||
),
|
|
||||||
model:
|
|
||||||
getStringValue(
|
|
||||||
findNestedString(resultRoot, ["model", "modelId", "model_id"]),
|
|
||||||
findNestedString(root, ["model", "modelId", "model_id"])
|
|
||||||
) || WORLDLABS_EXTERNAL_TASK_MODEL,
|
|
||||||
caption: getStringValue(
|
|
||||||
worldAssets?.caption,
|
|
||||||
findNestedString(resultRoot, ["caption"]),
|
|
||||||
findNestedString(root, ["caption"])
|
|
||||||
),
|
|
||||||
thumbnailUrl: getStringValue(
|
|
||||||
worldAssets?.thumbnail_url,
|
|
||||||
findNestedString(resultRoot, [
|
|
||||||
"thumbnailUrl",
|
|
||||||
"thumbnail_url",
|
|
||||||
"previewImageUrl",
|
|
||||||
"preview_image_url",
|
|
||||||
]),
|
|
||||||
findNestedString(root, [
|
|
||||||
"thumbnailUrl",
|
|
||||||
"thumbnail_url",
|
|
||||||
"previewImageUrl",
|
|
||||||
"preview_image_url",
|
|
||||||
])
|
|
||||||
),
|
|
||||||
panoUrl: getStringValue(
|
|
||||||
worldAssets?.imagery?.pano_url,
|
|
||||||
findNestedString(resultRoot, ["panoUrl", "pano_url", "panoramaUrl", "panorama_url"]),
|
|
||||||
findNestedString(root, ["panoUrl", "pano_url", "panoramaUrl", "panorama_url"])
|
|
||||||
),
|
|
||||||
meshUrl: getStringValue(
|
|
||||||
worldAssets?.mesh?.collider_mesh_url,
|
|
||||||
findNestedString(resultRoot, [
|
|
||||||
"meshUrl",
|
|
||||||
"mesh_url",
|
|
||||||
"modelUrl",
|
|
||||||
"model_url",
|
|
||||||
"glbUrl",
|
|
||||||
"glb_url",
|
|
||||||
"colliderMeshUrl",
|
|
||||||
"collider_mesh_url",
|
|
||||||
]),
|
|
||||||
findNestedString(root, [
|
|
||||||
"meshUrl",
|
|
||||||
"mesh_url",
|
|
||||||
"modelUrl",
|
|
||||||
"model_url",
|
|
||||||
"glbUrl",
|
|
||||||
"glb_url",
|
|
||||||
"colliderMeshUrl",
|
|
||||||
"collider_mesh_url",
|
|
||||||
])
|
|
||||||
),
|
|
||||||
spzUrls:
|
|
||||||
readStringRecord(splats?.spz_urls) ||
|
|
||||||
findNestedStringRecord(resultRoot, ["spzUrls", "spz_urls"]) ||
|
|
||||||
findNestedStringRecord(root, ["spzUrls", "spz_urls"]),
|
|
||||||
semanticsMetadata:
|
|
||||||
getObjectValue(splats?.semantics_metadata) ||
|
|
||||||
findNestedObject(resultRoot, ["semanticsMetadata", "semantics_metadata"]) ||
|
|
||||||
findNestedObject(root, ["semanticsMetadata", "semantics_metadata"]),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
interface RouteParams {
|
interface RouteParams {
|
||||||
params: Promise<{ taskId: string }>;
|
params: Promise<{ taskId: string }>;
|
||||||
}
|
}
|
||||||
@ -778,226 +452,6 @@ export async function GET(request: Request, { params }: RouteParams) {
|
|||||||
});
|
});
|
||||||
providerForLog = config.providerId || null;
|
providerForLog = config.providerId || null;
|
||||||
const modelAliasConfig = await getThreeDModelAliasConfigForRuntime();
|
const modelAliasConfig = await getThreeDModelAliasConfigForRuntime();
|
||||||
let persistedAssetPayload =
|
|
||||||
getObjectValue(persistedWorldAsset?.assetPayload) || {};
|
|
||||||
let shouldUseExternalTaskResultApi =
|
|
||||||
isWorldLabsExternalTaskApi(config.baseUrl) &&
|
|
||||||
persistedAssetPayload.externalTaskApi !== false &&
|
|
||||||
persistedAssetPayload.marbleApi !== true;
|
|
||||||
if (shouldUseExternalTaskResultApi) {
|
|
||||||
try {
|
|
||||||
const externalPayload = await worldLabsRequest<
|
|
||||||
WorldLabsExternalTaskResponse<WorldLabsExternalTaskResultData>
|
|
||||||
>(config, {
|
|
||||||
method: "GET",
|
|
||||||
path: `/api/external/task/result?taskId=${encodeURIComponent(normalizedTaskId)}`,
|
|
||||||
timeoutMs: resolveWorldLabsStatusTimeoutMs(),
|
|
||||||
});
|
|
||||||
const externalData =
|
|
||||||
unwrapExternalTaskData<WorldLabsExternalTaskResultData>(externalPayload);
|
|
||||||
const externalStatusText = getStringValue(externalData.status) || "running";
|
|
||||||
const externalStatus = normalizeExternalTaskStatus(
|
|
||||||
externalStatusText,
|
|
||||||
externalData.done === true
|
|
||||||
);
|
|
||||||
const externalAssetPayloadBase = {
|
|
||||||
...persistedAssetPayload,
|
|
||||||
providerId: config.providerId || null,
|
|
||||||
providerName: config.providerName || null,
|
|
||||||
externalTaskApi: true,
|
|
||||||
externalTaskStatus: externalStatusText,
|
|
||||||
requestId: getStringValue(externalPayload.requestId, externalPayload.request_id),
|
|
||||||
};
|
|
||||||
|
|
||||||
if (externalStatus === "running") {
|
|
||||||
return NextResponse.json({
|
|
||||||
success: true,
|
|
||||||
data: {
|
|
||||||
taskId: normalizedTaskId,
|
|
||||||
type: "world",
|
|
||||||
status: "running",
|
|
||||||
progress: null,
|
|
||||||
output: externalData,
|
|
||||||
assetPayload: externalAssetPayloadBase,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (externalStatus === "failed") {
|
|
||||||
const errorMessage = extractExternalTaskErrorMessage(
|
|
||||||
externalData,
|
|
||||||
externalPayload
|
|
||||||
);
|
|
||||||
await updatePersistedWorldAsset({
|
|
||||||
id: persistedWorldAsset?.id,
|
|
||||||
status: "failed",
|
|
||||||
progress: null,
|
|
||||||
error: errorMessage,
|
|
||||||
assetPayload: externalAssetPayloadBase,
|
|
||||||
});
|
|
||||||
logger.warn("external task failed status", {
|
|
||||||
elapsedMs: Date.now() - startedAt,
|
|
||||||
taskIdLength: normalizedTaskId.length,
|
|
||||||
providerId: config.providerId || null,
|
|
||||||
error: errorMessage,
|
|
||||||
});
|
|
||||||
return NextResponse.json({
|
|
||||||
success: true,
|
|
||||||
data: {
|
|
||||||
taskId: normalizedTaskId,
|
|
||||||
type: "world",
|
|
||||||
status: "failed",
|
|
||||||
progress: null,
|
|
||||||
errorMessage,
|
|
||||||
output: externalData,
|
|
||||||
assetPayload: externalAssetPayloadBase,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
const extractedAssetPayload = extractExternalWorldAssetPayload(externalData);
|
|
||||||
const rawMeshUrl = extractedAssetPayload.meshUrl;
|
|
||||||
const rawThumbnailUrl = extractedAssetPayload.thumbnailUrl;
|
|
||||||
const rawPanoUrl = extractedAssetPayload.panoUrl;
|
|
||||||
const stableWorldUrl: string | null = null;
|
|
||||||
const stableModelUrl = await archiveRemoteAssetIfNeeded({
|
|
||||||
cacheKey: `worldlabs:${normalizedTaskId}:mesh`,
|
|
||||||
sourceUrl: rawMeshUrl,
|
|
||||||
filenamePrefix: `worldlabs-mesh-${normalizedTaskId}`,
|
|
||||||
fallbackExtension: "glb",
|
|
||||||
validateAs: "glb",
|
|
||||||
});
|
|
||||||
const stableThumbnailUrl = await archiveRemoteAssetIfNeeded({
|
|
||||||
cacheKey: `worldlabs:${normalizedTaskId}:thumb`,
|
|
||||||
sourceUrl: rawThumbnailUrl,
|
|
||||||
filenamePrefix: `worldlabs-thumb-${normalizedTaskId}`,
|
|
||||||
fallbackExtension: "jpg",
|
|
||||||
});
|
|
||||||
const stablePanoUrl = await archiveRemoteAssetIfNeeded({
|
|
||||||
cacheKey: `worldlabs:${normalizedTaskId}:pano`,
|
|
||||||
sourceUrl: rawPanoUrl,
|
|
||||||
filenamePrefix: `worldlabs-pano-${normalizedTaskId}`,
|
|
||||||
fallbackExtension: "jpg",
|
|
||||||
});
|
|
||||||
const stableSpzUrls = await archiveRemoteAssetMapIfNeeded({
|
|
||||||
cacheKeyPrefix: `worldlabs:${normalizedTaskId}:spz`,
|
|
||||||
sourceMap: extractedAssetPayload.spzUrls,
|
|
||||||
filenamePrefix: `worldlabs-splat-${normalizedTaskId}`,
|
|
||||||
fallbackExtension: "spz",
|
|
||||||
validateAs: "spz",
|
|
||||||
});
|
|
||||||
const sourceSpzEntryCount = Object.keys(
|
|
||||||
extractedAssetPayload.spzUrls || {}
|
|
||||||
).length;
|
|
||||||
const stableSpzEntryCount = Object.keys(stableSpzUrls || {}).length;
|
|
||||||
const assetsArchivingPending = Boolean(
|
|
||||||
(rawMeshUrl && !stableModelUrl) ||
|
|
||||||
(rawThumbnailUrl && !stableThumbnailUrl) ||
|
|
||||||
(rawPanoUrl && !stablePanoUrl) ||
|
|
||||||
(sourceSpzEntryCount > 0 && stableSpzEntryCount !== sourceSpzEntryCount)
|
|
||||||
);
|
|
||||||
const storageArchivedAt = assetsArchivingPending
|
|
||||||
? null
|
|
||||||
: new Date().toISOString();
|
|
||||||
const assetPayload = {
|
|
||||||
...externalAssetPayloadBase,
|
|
||||||
worldId: extractedAssetPayload.worldId,
|
|
||||||
worldUrl: stableWorldUrl,
|
|
||||||
displayName: extractedAssetPayload.displayName,
|
|
||||||
model: resolveThreeDModelAliasId(
|
|
||||||
extractedAssetPayload.model,
|
|
||||||
modelAliasConfig.worldModels
|
|
||||||
),
|
|
||||||
caption: extractedAssetPayload.caption,
|
|
||||||
thumbnailUrl: stableThumbnailUrl || extractedAssetPayload.thumbnailUrl,
|
|
||||||
panoUrl: stablePanoUrl || extractedAssetPayload.panoUrl,
|
|
||||||
meshUrl: stableModelUrl,
|
|
||||||
spzUrls: stableSpzUrls,
|
|
||||||
semanticsMetadata: extractedAssetPayload.semanticsMetadata,
|
|
||||||
spzArchiveValidatedAt: stableSpzUrls ? storageArchivedAt : null,
|
|
||||||
storageArchivedAt,
|
|
||||||
assetsArchivingPending,
|
|
||||||
sourceWorldUrl: extractedAssetPayload.worldUrl || null,
|
|
||||||
sourceMeshUrl:
|
|
||||||
rawMeshUrl && stableModelUrl && rawMeshUrl !== stableModelUrl
|
|
||||||
? rawMeshUrl
|
|
||||||
: null,
|
|
||||||
sourceThumbnailUrl:
|
|
||||||
rawThumbnailUrl && stableThumbnailUrl && rawThumbnailUrl !== stableThumbnailUrl
|
|
||||||
? rawThumbnailUrl
|
|
||||||
: null,
|
|
||||||
sourcePanoUrl:
|
|
||||||
rawPanoUrl && stablePanoUrl && rawPanoUrl !== stablePanoUrl
|
|
||||||
? rawPanoUrl
|
|
||||||
: null,
|
|
||||||
sourceSpzUrls:
|
|
||||||
extractedAssetPayload.spzUrls &&
|
|
||||||
stableSpzUrls !== extractedAssetPayload.spzUrls
|
|
||||||
? extractedAssetPayload.spzUrls
|
|
||||||
: null,
|
|
||||||
};
|
|
||||||
|
|
||||||
await updatePersistedWorldAsset({
|
|
||||||
id: persistedWorldAsset?.id,
|
|
||||||
status: "success",
|
|
||||||
progress: 100,
|
|
||||||
modelUrl: stableModelUrl,
|
|
||||||
previewImageUrl: stableThumbnailUrl || stablePanoUrl || null,
|
|
||||||
error: null,
|
|
||||||
assetPayload,
|
|
||||||
});
|
|
||||||
|
|
||||||
logger.info("external task success status", {
|
|
||||||
elapsedMs: Date.now() - startedAt,
|
|
||||||
taskIdLength: normalizedTaskId.length,
|
|
||||||
providerId: config.providerId || null,
|
|
||||||
hasMesh: Boolean(stableModelUrl),
|
|
||||||
hasPano: Boolean(stablePanoUrl),
|
|
||||||
hasThumbnail: Boolean(stableThumbnailUrl),
|
|
||||||
hasSplat: Boolean(stableSpzUrls && Object.keys(stableSpzUrls).length > 0),
|
|
||||||
assetsArchivingPending,
|
|
||||||
});
|
|
||||||
|
|
||||||
return NextResponse.json({
|
|
||||||
success: true,
|
|
||||||
data: {
|
|
||||||
taskId: normalizedTaskId,
|
|
||||||
type: "world",
|
|
||||||
status: "success",
|
|
||||||
progress: 100,
|
|
||||||
output: externalData,
|
|
||||||
modelUrl: stableModelUrl,
|
|
||||||
previewImageUrl: stableThumbnailUrl || stablePanoUrl || null,
|
|
||||||
worldId: assetPayload.worldId,
|
|
||||||
worldUrl: stableWorldUrl,
|
|
||||||
panoUrl: stablePanoUrl,
|
|
||||||
caption: assetPayload.caption,
|
|
||||||
assetPayload,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
const fallbackAssetPayload = {
|
|
||||||
...persistedAssetPayload,
|
|
||||||
providerId: config.providerId || null,
|
|
||||||
providerName: config.providerName || null,
|
|
||||||
marbleApi: true,
|
|
||||||
externalTaskApi: false,
|
|
||||||
};
|
|
||||||
shouldUseExternalTaskResultApi = false;
|
|
||||||
persistedAssetPayload = fallbackAssetPayload;
|
|
||||||
await updatePersistedWorldAsset({
|
|
||||||
id: persistedWorldAsset?.id,
|
|
||||||
status: "running",
|
|
||||||
assetPayload: fallbackAssetPayload,
|
|
||||||
}).catch(() => {});
|
|
||||||
logger.warn("external task result failed, fallback to marble status", {
|
|
||||||
elapsedMs: Date.now() - startedAt,
|
|
||||||
taskIdLength: normalizedTaskId.length,
|
|
||||||
providerId: config.providerId || null,
|
|
||||||
error: error instanceof Error ? error.message : String(error),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const operation = await worldLabsRequest<WorldLabsOperation>(config, {
|
const operation = await worldLabsRequest<WorldLabsOperation>(config, {
|
||||||
method: "GET",
|
method: "GET",
|
||||||
path: `/marble/v1/operations/${encodeURIComponent(normalizedTaskId)}`,
|
path: `/marble/v1/operations/${encodeURIComponent(normalizedTaskId)}`,
|
||||||
@ -1058,68 +512,22 @@ export async function GET(request: Request, { params }: RouteParams) {
|
|||||||
let world = extractWorldFromOperation(operation);
|
let world = extractWorldFromOperation(operation);
|
||||||
const worldId = extractWorldId(operation);
|
const worldId = extractWorldId(operation);
|
||||||
if ((!world || !hasUsableWorldAssets(world)) && worldId) {
|
if ((!world || !hasUsableWorldAssets(world)) && worldId) {
|
||||||
try {
|
const detailedWorld = await worldLabsRequest<WorldLabsWorld>(config, {
|
||||||
const detailedWorld = await worldLabsRequest<WorldLabsWorld>(config, {
|
method: "GET",
|
||||||
method: "GET",
|
path: `/marble/v1/worlds/${encodeURIComponent(worldId)}`,
|
||||||
path: `/marble/v1/worlds/${encodeURIComponent(worldId)}`,
|
timeoutMs: resolveWorldLabsStatusTimeoutMs(),
|
||||||
timeoutMs: resolveWorldLabsStatusTimeoutMs(),
|
});
|
||||||
});
|
world = world
|
||||||
world = world
|
? {
|
||||||
? {
|
...world,
|
||||||
...world,
|
...detailedWorld,
|
||||||
...detailedWorld,
|
assets: detailedWorld.assets || world.assets || null,
|
||||||
assets: detailedWorld.assets || world.assets || null,
|
}
|
||||||
}
|
: detailedWorld;
|
||||||
: detailedWorld;
|
|
||||||
} catch (error) {
|
|
||||||
logger.warn("world detail fetch failed, retry with running status", {
|
|
||||||
elapsedMs: Date.now() - startedAt,
|
|
||||||
taskIdLength: normalizedTaskId.length,
|
|
||||||
providerId: config.providerId || null,
|
|
||||||
worldId,
|
|
||||||
error: error instanceof Error ? error.message : "unknown",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!world) {
|
if (!world) {
|
||||||
const pendingPayload = {
|
throw new Error("World Labs 任务已完成,但未返回 world 数据");
|
||||||
...(getObjectValue(persistedWorldAsset?.assetPayload) || {}),
|
|
||||||
providerId: config.providerId || null,
|
|
||||||
providerName: config.providerName || null,
|
|
||||||
worldId: worldId || null,
|
|
||||||
worldUrl: null,
|
|
||||||
displayName: null,
|
|
||||||
model: null,
|
|
||||||
assetsPending: true,
|
|
||||||
operationMetadata: operation.metadata ?? null,
|
|
||||||
};
|
|
||||||
await updatePersistedWorldAsset({
|
|
||||||
id: persistedWorldAsset?.id,
|
|
||||||
status: "running",
|
|
||||||
progress: 95,
|
|
||||||
assetPayload: pendingPayload,
|
|
||||||
});
|
|
||||||
logger.warn("task completed but world payload is missing", {
|
|
||||||
elapsedMs: Date.now() - startedAt,
|
|
||||||
taskIdLength: normalizedTaskId.length,
|
|
||||||
providerId: config.providerId || null,
|
|
||||||
worldId: worldId || null,
|
|
||||||
});
|
|
||||||
return NextResponse.json({
|
|
||||||
success: true,
|
|
||||||
data: {
|
|
||||||
taskId: normalizedTaskId,
|
|
||||||
type: "world",
|
|
||||||
status: "running",
|
|
||||||
progress: 95,
|
|
||||||
output: operation.response ?? null,
|
|
||||||
worldId: worldId || null,
|
|
||||||
worldUrl: null,
|
|
||||||
previewImageUrl: null,
|
|
||||||
assetPayload: pendingPayload,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!hasUsableWorldAssets(world)) {
|
if (!hasUsableWorldAssets(world)) {
|
||||||
@ -1235,15 +643,24 @@ export async function GET(request: Request, { params }: RouteParams) {
|
|||||||
: 0;
|
: 0;
|
||||||
const hasMissingSpzArchive =
|
const hasMissingSpzArchive =
|
||||||
rawSpzUrls.length > 0 && stableSpzEntryCount < rawSpzUrls.length;
|
rawSpzUrls.length > 0 && stableSpzEntryCount < rawSpzUrls.length;
|
||||||
const assetsArchivingPending = Boolean(
|
const storageArchivedAt =
|
||||||
(rawMeshUrl && !stableModelUrl) ||
|
[
|
||||||
(rawThumbnailUrl && !stableThumbnailUrl) ||
|
[rawWorldUrl, stableWorldUrl],
|
||||||
(rawPanoUrl && !stablePanoUrl) ||
|
[rawMeshUrl, stableModelUrl],
|
||||||
hasMissingSpzArchive
|
[rawThumbnailUrl, stableThumbnailUrl],
|
||||||
);
|
[rawPanoUrl, stablePanoUrl],
|
||||||
const storageArchivedAt = assetsArchivingPending
|
].some(([rawUrl, stableUrl]) => rawUrl && stableUrl && rawUrl !== stableUrl) ||
|
||||||
? null
|
Boolean(
|
||||||
: new Date().toISOString();
|
stableSpzUrls &&
|
||||||
|
assetPayload.spzUrls &&
|
||||||
|
Object.entries(stableSpzUrls).some(
|
||||||
|
([key, value]) =>
|
||||||
|
typeof assetPayload.spzUrls?.[key] === "string" &&
|
||||||
|
assetPayload.spzUrls[key] !== value
|
||||||
|
)
|
||||||
|
)
|
||||||
|
? new Date().toISOString()
|
||||||
|
: null;
|
||||||
|
|
||||||
logger.info("task success status", {
|
logger.info("task success status", {
|
||||||
elapsedMs: Date.now() - startedAt,
|
elapsedMs: Date.now() - startedAt,
|
||||||
@ -1255,7 +672,10 @@ export async function GET(request: Request, { params }: RouteParams) {
|
|||||||
hasThumbnail: Boolean(stableThumbnailUrl),
|
hasThumbnail: Boolean(stableThumbnailUrl),
|
||||||
hasSplat: Boolean(stableSpzUrls && Object.keys(stableSpzUrls).length > 0),
|
hasSplat: Boolean(stableSpzUrls && Object.keys(stableSpzUrls).length > 0),
|
||||||
storageArchivedAt,
|
storageArchivedAt,
|
||||||
assetsArchivingPending,
|
assetsArchivingPending: Boolean(
|
||||||
|
(rawMeshUrl && !stableModelUrl) ||
|
||||||
|
hasMissingSpzArchive
|
||||||
|
),
|
||||||
});
|
});
|
||||||
|
|
||||||
const responseAssetPayload = {
|
const responseAssetPayload = {
|
||||||
@ -1265,9 +685,12 @@ export async function GET(request: Request, { params }: RouteParams) {
|
|||||||
panoUrl: stablePanoUrl || assetPayload.panoUrl,
|
panoUrl: stablePanoUrl || assetPayload.panoUrl,
|
||||||
meshUrl: stableModelUrl,
|
meshUrl: stableModelUrl,
|
||||||
spzUrls: stableSpzUrls,
|
spzUrls: stableSpzUrls,
|
||||||
spzArchiveValidatedAt: stableSpzUrls ? storageArchivedAt : null,
|
spzArchiveValidatedAt: stableSpzUrls ? new Date().toISOString() : null,
|
||||||
storageArchivedAt,
|
storageArchivedAt,
|
||||||
assetsArchivingPending,
|
assetsArchivingPending: Boolean(
|
||||||
|
(rawMeshUrl && !stableModelUrl) ||
|
||||||
|
hasMissingSpzArchive
|
||||||
|
),
|
||||||
sourceWorldUrl: rawWorldUrl || null,
|
sourceWorldUrl: rawWorldUrl || null,
|
||||||
sourceMeshUrl:
|
sourceMeshUrl:
|
||||||
rawMeshUrl && stableModelUrl && rawMeshUrl !== stableModelUrl
|
rawMeshUrl && stableModelUrl && rawMeshUrl !== stableModelUrl
|
||||||
|
|||||||
@ -12,23 +12,13 @@ import {
|
|||||||
import { getThreeDModelAliasConfigForRuntime } from "@/lib/model3d/model-alias-config-server";
|
import { getThreeDModelAliasConfigForRuntime } from "@/lib/model3d/model-alias-config-server";
|
||||||
import { createLogger } from "@/lib/observability/logger";
|
import { createLogger } from "@/lib/observability/logger";
|
||||||
import { get3DWorldCreditCost } from "@/lib/points/config";
|
import { get3DWorldCreditCost } from "@/lib/points/config";
|
||||||
import {
|
import { WorldLabsApiError, worldLabsRequest } from "@/lib/worldlabs/client";
|
||||||
isWorldLabsExternalTaskApi,
|
|
||||||
WORLDLABS_EXTERNAL_TASK_MODEL,
|
|
||||||
WorldLabsApiError,
|
|
||||||
worldLabsRequest,
|
|
||||||
} from "@/lib/worldlabs/client";
|
|
||||||
import {
|
import {
|
||||||
getWorldLabsProviderConfig,
|
getWorldLabsProviderConfig,
|
||||||
getWorldLabsProviderConfigs,
|
getWorldLabsProviderConfigs,
|
||||||
reportWorldLabsProviderRuntimeFailure,
|
reportWorldLabsProviderRuntimeFailure,
|
||||||
} from "@/lib/worldlabs/server";
|
} from "@/lib/worldlabs/server";
|
||||||
import type {
|
import type { WorldLabsModel, WorldLabsOperation } from "@/lib/worldlabs/types";
|
||||||
WorldLabsExternalTaskCreateData,
|
|
||||||
WorldLabsExternalTaskResponse,
|
|
||||||
WorldLabsModel,
|
|
||||||
WorldLabsOperation,
|
|
||||||
} from "@/lib/worldlabs/types";
|
|
||||||
|
|
||||||
export const runtime = "nodejs";
|
export const runtime = "nodejs";
|
||||||
export const maxDuration = 180;
|
export const maxDuration = 180;
|
||||||
@ -57,10 +47,8 @@ type CreateWorldTaskRequest = {
|
|||||||
makePublic?: boolean;
|
makePublic?: boolean;
|
||||||
reconstructImages?: boolean;
|
reconstructImages?: boolean;
|
||||||
mediaAssetId?: string;
|
mediaAssetId?: string;
|
||||||
sourceUrl?: string;
|
|
||||||
mediaAssets?: Array<{
|
mediaAssets?: Array<{
|
||||||
mediaAssetId?: string;
|
mediaAssetId?: string;
|
||||||
sourceUrl?: string;
|
|
||||||
azimuth?: number | null;
|
azimuth?: number | null;
|
||||||
}>;
|
}>;
|
||||||
};
|
};
|
||||||
@ -70,152 +58,10 @@ function readOptionalTrimmedText(input: unknown) {
|
|||||||
return value || null;
|
return value || null;
|
||||||
}
|
}
|
||||||
|
|
||||||
function readOptionalHttpUrl(input: unknown) {
|
|
||||||
const value = readOptionalTrimmedText(input);
|
|
||||||
if (!value) return null;
|
|
||||||
try {
|
|
||||||
const parsed = new URL(value);
|
|
||||||
return parsed.protocol === "http:" || parsed.protocol === "https:"
|
|
||||||
? parsed.toString()
|
|
||||||
: null;
|
|
||||||
} catch {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function getObjectValue(value: unknown) {
|
|
||||||
return value && typeof value === "object" ? (value as Record<string, unknown>) : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
function unwrapExternalTaskData<T extends Record<string, unknown>>(payload: unknown) {
|
|
||||||
const root = getObjectValue(payload);
|
|
||||||
const data = getObjectValue(root?.data);
|
|
||||||
return (data || root || {}) as T;
|
|
||||||
}
|
|
||||||
|
|
||||||
function normalizeExternalTaskStatus(status?: string | null, done?: boolean) {
|
|
||||||
const normalized = String(status || "").trim().toLowerCase();
|
|
||||||
if (done === true || ["succeeded", "success", "completed", "done"].includes(normalized)) {
|
|
||||||
return "success" as const;
|
|
||||||
}
|
|
||||||
if (["failed", "failure", "error", "cancelled", "canceled"].includes(normalized)) {
|
|
||||||
return "failed" as const;
|
|
||||||
}
|
|
||||||
return "running" as const;
|
|
||||||
}
|
|
||||||
|
|
||||||
function normalizeExternalTaskCreateOperation(
|
|
||||||
payload: WorldLabsExternalTaskResponse<WorldLabsExternalTaskCreateData>
|
|
||||||
): WorldLabsOperation {
|
|
||||||
const data = unwrapExternalTaskData<WorldLabsExternalTaskCreateData>(payload);
|
|
||||||
const taskId = readOptionalTrimmedText(data.taskId || data.task_id || data.id);
|
|
||||||
const status = readOptionalTrimmedText(data.status) || "accepted";
|
|
||||||
const normalizedStatus = normalizeExternalTaskStatus(status, data.done === true);
|
|
||||||
return {
|
|
||||||
operation_id: taskId || undefined,
|
|
||||||
done: normalizedStatus === "success",
|
|
||||||
error:
|
|
||||||
normalizedStatus === "failed"
|
|
||||||
? {
|
|
||||||
message:
|
|
||||||
readOptionalTrimmedText(
|
|
||||||
(payload as Record<string, unknown>)?.message ||
|
|
||||||
(payload as Record<string, unknown>)?.error
|
|
||||||
) || "World Labs 外部任务创建失败",
|
|
||||||
}
|
|
||||||
: null,
|
|
||||||
metadata: {
|
|
||||||
externalTaskApi: true,
|
|
||||||
status,
|
|
||||||
resultPath: readOptionalTrimmedText(data.resultPath || data.result_path),
|
|
||||||
requestId: readOptionalTrimmedText(payload.requestId || payload.request_id),
|
|
||||||
},
|
|
||||||
response: data,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function shouldCharge3DPoints(mode: WorldTaskMode) {
|
function shouldCharge3DPoints(mode: WorldTaskMode) {
|
||||||
return mode === "text" || mode === "image" || mode === "multi-image" || mode === "video";
|
return mode === "text" || mode === "image" || mode === "multi-image" || mode === "video";
|
||||||
}
|
}
|
||||||
|
|
||||||
function shouldUseExternalTaskCreateApi(
|
|
||||||
config: Awaited<ReturnType<typeof getWorldLabsProviderConfig>>,
|
|
||||||
mode: WorldTaskMode
|
|
||||||
) {
|
|
||||||
return isWorldLabsExternalTaskApi(config.baseUrl) && mode === "image";
|
|
||||||
}
|
|
||||||
|
|
||||||
function isWorldLabsModeUnsupportedByExternalProvider(
|
|
||||||
config: Awaited<ReturnType<typeof getWorldLabsProviderConfig>>,
|
|
||||||
mode: WorldTaskMode
|
|
||||||
) {
|
|
||||||
return isWorldLabsExternalTaskApi(config.baseUrl) && mode !== "image";
|
|
||||||
}
|
|
||||||
|
|
||||||
function assertWorldModeSupportedByCandidate(
|
|
||||||
config: Awaited<ReturnType<typeof getWorldLabsProviderConfig>>,
|
|
||||||
mode: WorldTaskMode,
|
|
||||||
) {
|
|
||||||
if (isWorldLabsModeUnsupportedByExternalProvider(config, mode)) {
|
|
||||||
const normalizedBaseUrl = String(config.baseUrl || "").trim() || "未知服务地址";
|
|
||||||
const normalizedMode = String(mode || "").trim() || "未知模式";
|
|
||||||
throw new Error(
|
|
||||||
`${normalizedBaseUrl} 的 world-generation 当前仅支持图生世界(image)。当前提交的是 ${normalizedMode},请先切换供应商或使用图生世界。`
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function resolveOfficialMarbleModel(model?: string | null) {
|
|
||||||
const normalized = readOptionalTrimmedText(model);
|
|
||||||
if (!normalized || normalized === WORLDLABS_EXTERNAL_TASK_MODEL) {
|
|
||||||
return "marble-1.1";
|
|
||||||
}
|
|
||||||
return normalized;
|
|
||||||
}
|
|
||||||
|
|
||||||
function buildExternalWorldTaskPayload(
|
|
||||||
mode: WorldTaskMode,
|
|
||||||
worldPrompt: Record<string, unknown>,
|
|
||||||
prompt: string | null,
|
|
||||||
displayName: string,
|
|
||||||
) {
|
|
||||||
const normalizedPrompt = readOptionalTrimmedText(prompt);
|
|
||||||
if (!normalizedPrompt && mode === "text") {
|
|
||||||
throw new Error("请输入世界描述");
|
|
||||||
}
|
|
||||||
const payload: Record<string, unknown> = {
|
|
||||||
model: WORLDLABS_EXTERNAL_TASK_MODEL,
|
|
||||||
displayName,
|
|
||||||
};
|
|
||||||
if (normalizedPrompt) {
|
|
||||||
payload.prompt = normalizedPrompt;
|
|
||||||
} else if (mode !== "text") {
|
|
||||||
payload.prompt = "图生世界";
|
|
||||||
}
|
|
||||||
|
|
||||||
if (mode === "image" || mode === "multi-image") {
|
|
||||||
payload.world_prompt = worldPrompt;
|
|
||||||
}
|
|
||||||
|
|
||||||
return payload;
|
|
||||||
}
|
|
||||||
|
|
||||||
function buildOfficialWorldTaskPayload(
|
|
||||||
displayName: string,
|
|
||||||
worldPrompt: Record<string, unknown>,
|
|
||||||
officialMarbleModel: string,
|
|
||||||
body: CreateWorldTaskRequest
|
|
||||||
) {
|
|
||||||
return {
|
|
||||||
display_name: displayName,
|
|
||||||
model: officialMarbleModel,
|
|
||||||
permission: {
|
|
||||||
public: body.makePublic === true,
|
|
||||||
},
|
|
||||||
world_prompt: worldPrompt,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function buildWorldPrompt(body: CreateWorldTaskRequest, mode: WorldTaskMode) {
|
function buildWorldPrompt(body: CreateWorldTaskRequest, mode: WorldTaskMode) {
|
||||||
const textPrompt = readOptionalTrimmedText(body.prompt);
|
const textPrompt = readOptionalTrimmedText(body.prompt);
|
||||||
|
|
||||||
@ -234,21 +80,15 @@ function buildWorldPrompt(body: CreateWorldTaskRequest, mode: WorldTaskMode) {
|
|||||||
|
|
||||||
if (mode === "image") {
|
if (mode === "image") {
|
||||||
const mediaAssetId = readOptionalTrimmedText(body.mediaAssetId);
|
const mediaAssetId = readOptionalTrimmedText(body.mediaAssetId);
|
||||||
const sourceUrl = readOptionalHttpUrl(body.sourceUrl);
|
if (!mediaAssetId) {
|
||||||
if (!mediaAssetId && !sourceUrl) {
|
throw new Error("图生世界缺少 mediaAssetId");
|
||||||
throw new Error("图生世界缺少 mediaAssetId 或 sourceUrl");
|
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
type: "image",
|
type: "image",
|
||||||
image_prompt: mediaAssetId
|
image_prompt: {
|
||||||
? {
|
source: "media_asset",
|
||||||
source: "media_asset",
|
media_asset_id: mediaAssetId,
|
||||||
media_asset_id: mediaAssetId,
|
},
|
||||||
}
|
|
||||||
: {
|
|
||||||
source: "uri",
|
|
||||||
uri: sourceUrl,
|
|
||||||
},
|
|
||||||
...(textPrompt ? { text_prompt: textPrompt } : {}),
|
...(textPrompt ? { text_prompt: textPrompt } : {}),
|
||||||
...(body.disableRecaption !== undefined
|
...(body.disableRecaption !== undefined
|
||||||
? { disable_recaption: body.disableRecaption }
|
? { disable_recaption: body.disableRecaption }
|
||||||
@ -262,8 +102,7 @@ function buildWorldPrompt(body: CreateWorldTaskRequest, mode: WorldTaskMode) {
|
|||||||
const multiImagePrompt = mediaAssets
|
const multiImagePrompt = mediaAssets
|
||||||
.map((item) => {
|
.map((item) => {
|
||||||
const mediaAssetId = readOptionalTrimmedText(item?.mediaAssetId);
|
const mediaAssetId = readOptionalTrimmedText(item?.mediaAssetId);
|
||||||
const sourceUrl = readOptionalHttpUrl(item?.sourceUrl);
|
if (!mediaAssetId) return null;
|
||||||
if (!mediaAssetId && !sourceUrl) return null;
|
|
||||||
const azimuth =
|
const azimuth =
|
||||||
item?.azimuth === null || item?.azimuth === undefined
|
item?.azimuth === null || item?.azimuth === undefined
|
||||||
? null
|
? null
|
||||||
@ -272,15 +111,10 @@ function buildWorldPrompt(body: CreateWorldTaskRequest, mode: WorldTaskMode) {
|
|||||||
: null;
|
: null;
|
||||||
return {
|
return {
|
||||||
...(azimuth === null ? {} : { azimuth }),
|
...(azimuth === null ? {} : { azimuth }),
|
||||||
content: mediaAssetId
|
content: {
|
||||||
? {
|
source: "media_asset",
|
||||||
source: "media_asset",
|
media_asset_id: mediaAssetId,
|
||||||
media_asset_id: mediaAssetId,
|
},
|
||||||
}
|
|
||||||
: {
|
|
||||||
source: "uri",
|
|
||||||
uri: sourceUrl,
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
})
|
})
|
||||||
.filter(Boolean);
|
.filter(Boolean);
|
||||||
@ -303,22 +137,16 @@ function buildWorldPrompt(body: CreateWorldTaskRequest, mode: WorldTaskMode) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const mediaAssetId = readOptionalTrimmedText(body.mediaAssetId);
|
const mediaAssetId = readOptionalTrimmedText(body.mediaAssetId);
|
||||||
const sourceUrl = readOptionalHttpUrl(body.sourceUrl);
|
if (!mediaAssetId) {
|
||||||
if (!mediaAssetId && !sourceUrl) {
|
throw new Error("视频生成世界缺少 mediaAssetId");
|
||||||
throw new Error("视频生成世界缺少 mediaAssetId 或 sourceUrl");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
type: "video",
|
type: "video",
|
||||||
video_prompt: mediaAssetId
|
video_prompt: {
|
||||||
? {
|
source: "media_asset",
|
||||||
source: "media_asset",
|
media_asset_id: mediaAssetId,
|
||||||
media_asset_id: mediaAssetId,
|
},
|
||||||
}
|
|
||||||
: {
|
|
||||||
source: "uri",
|
|
||||||
uri: sourceUrl,
|
|
||||||
},
|
|
||||||
...(textPrompt ? { text_prompt: textPrompt } : {}),
|
...(textPrompt ? { text_prompt: textPrompt } : {}),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -359,29 +187,12 @@ export async function POST(request: Request) {
|
|||||||
);
|
);
|
||||||
modelForLog = resolvedWorldModel || "marble-1.1";
|
modelForLog = resolvedWorldModel || "marble-1.1";
|
||||||
|
|
||||||
const preferredProviderId = readOptionalTrimmedText(body.providerId);
|
|
||||||
const displayName =
|
|
||||||
readOptionalTrimmedText(body.displayName) ||
|
|
||||||
readOptionalTrimmedText(body.prompt) ||
|
|
||||||
`World ${new Date().toLocaleString("zh-CN")}`;
|
|
||||||
const worldPrompt = buildWorldPrompt(body, mode);
|
|
||||||
|
|
||||||
const candidates = preferredProviderId
|
|
||||||
? [await getWorldLabsProviderConfig(preferredProviderId)]
|
|
||||||
: await getWorldLabsProviderConfigs();
|
|
||||||
if (candidates.length === 0) {
|
|
||||||
throw new Error("未找到可用的 World Labs Key");
|
|
||||||
}
|
|
||||||
|
|
||||||
const officialMarbleModel = resolveOfficialMarbleModel(resolvedWorldModel);
|
|
||||||
const predictedBillingModel = candidates.some((candidate) =>
|
|
||||||
shouldUseExternalTaskCreateApi(candidate, mode)
|
|
||||||
)
|
|
||||||
? WORLDLABS_EXTERNAL_TASK_MODEL
|
|
||||||
: officialMarbleModel;
|
|
||||||
const pointsConfig = await getPointsSystemConfig();
|
const pointsConfig = await getPointsSystemConfig();
|
||||||
const shouldChargePoints = shouldCharge3DPoints(mode);
|
const shouldChargePoints = shouldCharge3DPoints(mode);
|
||||||
const unitCost = get3DWorldCreditCost(pointsConfig, predictedBillingModel);
|
const unitCost = get3DWorldCreditCost(
|
||||||
|
pointsConfig,
|
||||||
|
resolvedWorldModel || "marble-1.1",
|
||||||
|
);
|
||||||
|
|
||||||
if (
|
if (
|
||||||
shouldChargePoints &&
|
shouldChargePoints &&
|
||||||
@ -409,83 +220,42 @@ export async function POST(request: Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const preferredProviderId = readOptionalTrimmedText(body.providerId);
|
||||||
|
const displayName =
|
||||||
|
readOptionalTrimmedText(body.displayName) ||
|
||||||
|
readOptionalTrimmedText(body.prompt) ||
|
||||||
|
`World ${new Date().toLocaleString("zh-CN")}`;
|
||||||
|
const worldPrompt = buildWorldPrompt(body, mode);
|
||||||
|
|
||||||
|
const candidates = preferredProviderId
|
||||||
|
? [await getWorldLabsProviderConfig(preferredProviderId)]
|
||||||
|
: await getWorldLabsProviderConfigs();
|
||||||
|
if (candidates.length === 0) {
|
||||||
|
throw new Error("未找到可用的 World Labs Key");
|
||||||
|
}
|
||||||
|
|
||||||
let result: WorldLabsOperation | null = null;
|
let result: WorldLabsOperation | null = null;
|
||||||
let usedProvider = null as Awaited<
|
let usedProvider = null as Awaited<
|
||||||
ReturnType<typeof getWorldLabsProviderConfig>
|
ReturnType<typeof getWorldLabsProviderConfig>
|
||||||
> | null;
|
> | null;
|
||||||
let usedExternalTaskApi = false;
|
|
||||||
let lastError: unknown = null;
|
let lastError: unknown = null;
|
||||||
|
|
||||||
for (const candidate of candidates) {
|
for (const candidate of candidates) {
|
||||||
try {
|
try {
|
||||||
const useExternalTaskApi = shouldUseExternalTaskCreateApi(candidate, mode);
|
result = await worldLabsRequest<WorldLabsOperation>(candidate, {
|
||||||
assertWorldModeSupportedByCandidate(candidate, mode);
|
method: "POST",
|
||||||
let requestUsedExternalTaskApi = false;
|
path: "/marble/v1/worlds:generate",
|
||||||
if (useExternalTaskApi) {
|
timeoutMs: resolveWorldLabsCreateTimeoutMs(),
|
||||||
const payload = buildExternalWorldTaskPayload(
|
json: {
|
||||||
mode,
|
display_name: displayName,
|
||||||
worldPrompt,
|
model: resolvedWorldModel || "marble-1.1",
|
||||||
readOptionalTrimmedText(body.prompt),
|
permission: {
|
||||||
displayName,
|
public: body.makePublic === true,
|
||||||
);
|
},
|
||||||
try {
|
world_prompt: worldPrompt,
|
||||||
const externalResult = await worldLabsRequest<
|
},
|
||||||
WorldLabsExternalTaskResponse<WorldLabsExternalTaskCreateData>
|
});
|
||||||
>(candidate, {
|
|
||||||
method: "POST",
|
|
||||||
path: "/api/external/task/generate",
|
|
||||||
timeoutMs: resolveWorldLabsCreateTimeoutMs(),
|
|
||||||
json: payload,
|
|
||||||
});
|
|
||||||
result = normalizeExternalTaskCreateOperation(externalResult);
|
|
||||||
requestUsedExternalTaskApi = true;
|
|
||||||
} catch (error) {
|
|
||||||
const externalError = error instanceof Error ? error.message : String(error);
|
|
||||||
logger.warn("external world create failed, fallback to marble endpoint", {
|
|
||||||
providerId: candidate.providerId || null,
|
|
||||||
error: externalError,
|
|
||||||
});
|
|
||||||
if (isCredentialOrBillingProviderError(error)) {
|
|
||||||
await reportWorldLabsProviderRuntimeFailure(
|
|
||||||
candidate.providerId,
|
|
||||||
externalError
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
result = await worldLabsRequest<WorldLabsOperation>(candidate, {
|
|
||||||
method: "POST",
|
|
||||||
path: "/marble/v1/worlds:generate",
|
|
||||||
timeoutMs: resolveWorldLabsCreateTimeoutMs(),
|
|
||||||
json: buildOfficialWorldTaskPayload(
|
|
||||||
displayName,
|
|
||||||
worldPrompt,
|
|
||||||
officialMarbleModel,
|
|
||||||
body
|
|
||||||
),
|
|
||||||
});
|
|
||||||
requestUsedExternalTaskApi = false;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
requestUsedExternalTaskApi = false;
|
|
||||||
result = await worldLabsRequest<WorldLabsOperation>(candidate, {
|
|
||||||
method: "POST",
|
|
||||||
path: "/marble/v1/worlds:generate",
|
|
||||||
timeoutMs: resolveWorldLabsCreateTimeoutMs(),
|
|
||||||
json: buildOfficialWorldTaskPayload(
|
|
||||||
displayName,
|
|
||||||
worldPrompt,
|
|
||||||
officialMarbleModel,
|
|
||||||
body
|
|
||||||
),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!result?.operation_id) {
|
|
||||||
throw new Error("创建 World Labs 世界任务返回缺少 operation_id");
|
|
||||||
}
|
|
||||||
|
|
||||||
usedProvider = candidate;
|
usedProvider = candidate;
|
||||||
usedExternalTaskApi = requestUsedExternalTaskApi;
|
|
||||||
providerForLog = candidate.providerId || null;
|
providerForLog = candidate.providerId || null;
|
||||||
break;
|
break;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@ -522,18 +292,12 @@ export async function POST(request: Request) {
|
|||||||
|
|
||||||
if (shouldChargePoints && pointsConfig.enabled && unitCost > 0) {
|
if (shouldChargePoints && pointsConfig.enabled && unitCost > 0) {
|
||||||
try {
|
try {
|
||||||
const usedBillingModel = usedExternalTaskApi
|
|
||||||
? WORLDLABS_EXTERNAL_TASK_MODEL
|
|
||||||
: officialMarbleModel;
|
|
||||||
const usedBillingDisplayName = usedBillingModel === WORLDLABS_EXTERNAL_TASK_MODEL
|
|
||||||
? WORLDLABS_EXTERNAL_TASK_MODEL
|
|
||||||
: worldModelDisplayName;
|
|
||||||
await consumeUserPoints({
|
await consumeUserPoints({
|
||||||
userId: session.user.id,
|
userId: session.user.id,
|
||||||
amount: unitCost,
|
amount: unitCost,
|
||||||
reason: "world3d_generation",
|
reason: "world3d_generation",
|
||||||
modelId: `worldlabs-3d:${usedBillingModel}`,
|
modelId: `worldlabs-3d:${resolvedWorldModel || "marble-1.1"}`,
|
||||||
note: `3D世界生成消耗积分(${usedBillingDisplayName})`,
|
note: `3D世界生成消耗积分(${worldModelDisplayName})`,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error("points consume failed", error, {
|
logger.error("points consume failed", error, {
|
||||||
@ -549,13 +313,10 @@ export async function POST(request: Request) {
|
|||||||
elapsedMs: Date.now() - startedAt,
|
elapsedMs: Date.now() - startedAt,
|
||||||
taskIdLength: taskId.length,
|
taskIdLength: taskId.length,
|
||||||
mode,
|
mode,
|
||||||
model: usedExternalTaskApi
|
model: resolvedWorldModel || "marble-1.1",
|
||||||
? WORLDLABS_EXTERNAL_TASK_MODEL
|
|
||||||
: officialMarbleModel,
|
|
||||||
providerId: usedProvider.providerId || null,
|
providerId: usedProvider.providerId || null,
|
||||||
status: result.done ? "success" : "running",
|
status: result.done ? "success" : "running",
|
||||||
unitCost,
|
unitCost,
|
||||||
externalTaskApi: usedExternalTaskApi,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return NextResponse.json({
|
return NextResponse.json({
|
||||||
@ -566,8 +327,6 @@ export async function POST(request: Request) {
|
|||||||
assetPayload: {
|
assetPayload: {
|
||||||
providerId: usedProvider.providerId || null,
|
providerId: usedProvider.providerId || null,
|
||||||
providerName: usedProvider.providerName || null,
|
providerName: usedProvider.providerName || null,
|
||||||
externalTaskApi: usedExternalTaskApi,
|
|
||||||
marbleApi: !usedExternalTaskApi,
|
|
||||||
},
|
},
|
||||||
raw: result,
|
raw: result,
|
||||||
},
|
},
|
||||||
|
|||||||
@ -1,11 +1,7 @@
|
|||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
import { auth } from "@/app/(auth)/auth";
|
import { auth } from "@/app/(auth)/auth";
|
||||||
import { createLogger } from "@/lib/observability/logger";
|
import { createLogger } from "@/lib/observability/logger";
|
||||||
import { uploadFile } from "@/lib/storage/service";
|
import { WorldLabsApiError, worldLabsRequest } from "@/lib/worldlabs/client";
|
||||||
import {
|
|
||||||
WorldLabsApiError,
|
|
||||||
worldLabsRequest,
|
|
||||||
} from "@/lib/worldlabs/client";
|
|
||||||
import {
|
import {
|
||||||
getWorldLabsProviderConfig,
|
getWorldLabsProviderConfig,
|
||||||
getWorldLabsProviderConfigs,
|
getWorldLabsProviderConfigs,
|
||||||
@ -116,81 +112,6 @@ function normalizeMediaAssetId(data: WorldLabsPrepareUploadResponse) {
|
|||||||
return String(data.media_asset?.media_asset_id || data.media_asset?.id || "").trim();
|
return String(data.media_asset?.media_asset_id || data.media_asset?.id || "").trim();
|
||||||
}
|
}
|
||||||
|
|
||||||
function readOptionalString(value: unknown) {
|
|
||||||
const text = String(value || "").trim();
|
|
||||||
return text || null;
|
|
||||||
}
|
|
||||||
|
|
||||||
function extractUploadInfoFromCurlExample(curlExample: unknown) {
|
|
||||||
const curl = String(curlExample || "");
|
|
||||||
const uploadUrl =
|
|
||||||
[...curl.matchAll(/https?:\/\/[^\s'"\\]+/g)]
|
|
||||||
.map((match) => match[0])
|
|
||||||
.find(Boolean) || null;
|
|
||||||
const requiredHeaders: Record<string, string> = {};
|
|
||||||
const headerPattern = /(?:-H|--header)\s+(?:"([^"]+)"|'([^']+)'|([^\s]+))/g;
|
|
||||||
let headerMatch: RegExpExecArray | null;
|
|
||||||
|
|
||||||
while ((headerMatch = headerPattern.exec(curl))) {
|
|
||||||
const header = String(
|
|
||||||
headerMatch[1] || headerMatch[2] || headerMatch[3] || ""
|
|
||||||
).trim();
|
|
||||||
const colonIndex = header.indexOf(":");
|
|
||||||
if (colonIndex <= 0) continue;
|
|
||||||
const name = header.slice(0, colonIndex).trim();
|
|
||||||
const value = header.slice(colonIndex + 1).trim();
|
|
||||||
if (name && value) {
|
|
||||||
requiredHeaders[name] = value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return { uploadUrl, requiredHeaders };
|
|
||||||
}
|
|
||||||
|
|
||||||
function resolvePrepareUploadDetails(data: WorldLabsPrepareUploadResponse) {
|
|
||||||
const uploadInfo = data.upload_info || {};
|
|
||||||
const uploadInfoRecord = uploadInfo as Record<string, unknown>;
|
|
||||||
const curlDetails = extractUploadInfoFromCurlExample(uploadInfo.curl_example);
|
|
||||||
const explicitHeaders =
|
|
||||||
uploadInfo.required_headers && typeof uploadInfo.required_headers === "object"
|
|
||||||
? uploadInfo.required_headers
|
|
||||||
: {};
|
|
||||||
|
|
||||||
return {
|
|
||||||
uploadUrl:
|
|
||||||
readOptionalString(uploadInfo.upload_url) ||
|
|
||||||
readOptionalString(uploadInfoRecord.uploadUrl) ||
|
|
||||||
readOptionalString(uploadInfoRecord.url) ||
|
|
||||||
curlDetails.uploadUrl,
|
|
||||||
uploadMethod:
|
|
||||||
readOptionalString(uploadInfo.upload_method) ||
|
|
||||||
readOptionalString(uploadInfoRecord.method) ||
|
|
||||||
"PUT",
|
|
||||||
requiredHeaders: {
|
|
||||||
...curlDetails.requiredHeaders,
|
|
||||||
...explicitHeaders,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function isSignedUploadFailure(error: unknown) {
|
|
||||||
const message = error instanceof Error ? error.message : String(error || "");
|
|
||||||
return (
|
|
||||||
message.includes("SignatureDoesNotMatch") ||
|
|
||||||
message.includes("Access denied") ||
|
|
||||||
message.includes("Google secret key") ||
|
|
||||||
message.includes("签名")
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
async function uploadWorldLabsInputToStorage(file: File, extension: string) {
|
|
||||||
const buffer = await file.arrayBuffer();
|
|
||||||
const safeExtension = extension && extension !== "bin" ? extension : "bin";
|
|
||||||
const filename = `worldlabs-input-${Date.now()}-${crypto.randomUUID()}.${safeExtension}`;
|
|
||||||
const uploaded = await uploadFile(filename, buffer);
|
|
||||||
return uploaded.url;
|
|
||||||
}
|
|
||||||
|
|
||||||
function isCredentialOrBillingProviderError(error: unknown) {
|
function isCredentialOrBillingProviderError(error: unknown) {
|
||||||
return (
|
return (
|
||||||
error instanceof WorldLabsApiError &&
|
error instanceof WorldLabsApiError &&
|
||||||
@ -281,24 +202,24 @@ export async function POST(request: Request) {
|
|||||||
);
|
);
|
||||||
|
|
||||||
const mediaAssetId = normalizeMediaAssetId(prepareUpload);
|
const mediaAssetId = normalizeMediaAssetId(prepareUpload);
|
||||||
const { uploadUrl, uploadMethod, requiredHeaders } =
|
const uploadUrl = String(prepareUpload.upload_info?.upload_url || "").trim();
|
||||||
resolvePrepareUploadDetails(prepareUpload);
|
|
||||||
if (!mediaAssetId || !uploadUrl) {
|
if (!mediaAssetId || !uploadUrl) {
|
||||||
throw new Error("World Labs 返回的上传信息不完整");
|
throw new Error("World Labs 返回的上传信息不完整");
|
||||||
}
|
}
|
||||||
|
|
||||||
const uploadHeaders = new Headers();
|
const uploadHeaders = new Headers();
|
||||||
|
uploadHeaders.set("Content-Type", file.type || "application/octet-stream");
|
||||||
|
const requiredHeaders = prepareUpload.upload_info?.required_headers || {};
|
||||||
for (const [headerName, headerValue] of Object.entries(requiredHeaders)) {
|
for (const [headerName, headerValue] of Object.entries(requiredHeaders)) {
|
||||||
if (!headerName || headerValue === undefined || headerValue === null) continue;
|
if (!headerName || headerValue === undefined || headerValue === null) continue;
|
||||||
uploadHeaders.set(headerName, String(headerValue));
|
uploadHeaders.set(headerName, String(headerValue));
|
||||||
}
|
}
|
||||||
if (!uploadHeaders.has("Content-Type") && !prepareUpload.upload_info?.curl_example) {
|
|
||||||
uploadHeaders.set("Content-Type", file.type || "application/octet-stream");
|
|
||||||
}
|
|
||||||
|
|
||||||
const normalizedUploadMethod = uploadMethod.toUpperCase();
|
const uploadMethod = String(
|
||||||
|
prepareUpload.upload_info?.upload_method || "PUT"
|
||||||
|
).toUpperCase();
|
||||||
const uploadResponse = await fetch(uploadUrl, {
|
const uploadResponse = await fetch(uploadUrl, {
|
||||||
method: normalizedUploadMethod === "PUT" ? "PUT" : normalizedUploadMethod,
|
method: uploadMethod === "PUT" ? "PUT" : uploadMethod,
|
||||||
headers: uploadHeaders,
|
headers: uploadHeaders,
|
||||||
body: file,
|
body: file,
|
||||||
cache: "no-store",
|
cache: "no-store",
|
||||||
@ -331,28 +252,6 @@ export async function POST(request: Request) {
|
|||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
lastError = error;
|
lastError = error;
|
||||||
if (isSignedUploadFailure(error)) {
|
|
||||||
try {
|
|
||||||
const sourceUrl = await uploadWorldLabsInputToStorage(file, extension);
|
|
||||||
logger.warn("worldlabs signed upload failed; using storage source url", {
|
|
||||||
elapsedMs: Date.now() - startedAt,
|
|
||||||
providerId: config.providerId || null,
|
|
||||||
kind: isImage ? "image" : "video",
|
|
||||||
size: file.size,
|
|
||||||
});
|
|
||||||
return NextResponse.json({
|
|
||||||
success: true,
|
|
||||||
data: {
|
|
||||||
sourceUrl,
|
|
||||||
providerId: config.providerId || null,
|
|
||||||
providerName: config.providerName || null,
|
|
||||||
uploadFallback: "storage-source-url",
|
|
||||||
},
|
|
||||||
});
|
|
||||||
} catch (fallbackError) {
|
|
||||||
lastError = fallbackError;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (isCredentialOrBillingProviderError(error)) {
|
if (isCredentialOrBillingProviderError(error)) {
|
||||||
await reportWorldLabsProviderRuntimeFailure(
|
await reportWorldLabsProviderRuntimeFailure(
|
||||||
config.providerId,
|
config.providerId,
|
||||||
|
|||||||
@ -2,7 +2,6 @@ import { sql } from "drizzle-orm";
|
|||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import { auth } from "@/app/(auth)/auth";
|
import { auth } from "@/app/(auth)/auth";
|
||||||
import { isAgentModeScopedChatId } from "@/lib/chat/messages-route-access";
|
|
||||||
import { db } from "@/lib/db";
|
import { db } from "@/lib/db";
|
||||||
import { clientPerformanceEvent } from "@/lib/db/schema";
|
import { clientPerformanceEvent } from "@/lib/db/schema";
|
||||||
|
|
||||||
@ -168,12 +167,9 @@ export async function POST(request: Request) {
|
|||||||
console.warn("[ClientPerformance] prune failed", error);
|
console.warn("[ClientPerformance] prune failed", error);
|
||||||
});
|
});
|
||||||
|
|
||||||
const persistedChatId =
|
|
||||||
event.chatId && !isAgentModeScopedChatId(event.chatId) ? event.chatId : null;
|
|
||||||
|
|
||||||
await db.insert(clientPerformanceEvent).values({
|
await db.insert(clientPerformanceEvent).values({
|
||||||
userId: session?.user?.id || null,
|
userId: session?.user?.id || null,
|
||||||
chatId: persistedChatId,
|
chatId: event.chatId || null,
|
||||||
projectId: clampText(event.projectId, 64),
|
projectId: clampText(event.projectId, 64),
|
||||||
eventType: clampText(event.eventType, 64) || "unknown",
|
eventType: clampText(event.eventType, 64) || "unknown",
|
||||||
section: event.section || "unknown",
|
section: event.section || "unknown",
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@ -1,10 +1,234 @@
|
|||||||
import { NextResponse } from "next/server";
|
import { type NextRequest, NextResponse } from "next/server";
|
||||||
import { auth } from "@/app/(auth)/auth";
|
import { auth } from "@/app/(auth)/auth";
|
||||||
import { executeExtendPOST } from "@/lib/canvas/operations/extend";
|
import { ExtendServiceError, executeExtendJob } from "@/lib/ai/extend/service";
|
||||||
export const runtime = "nodejs";
|
import { getPointsSystemConfig } from "@/lib/db/queries";
|
||||||
|
import { prepareFeaturePointsCharge } from "@/lib/points/feature-charge";
|
||||||
|
import { getImageToolCreditCost } from "@/lib/points/config";
|
||||||
|
import {
|
||||||
|
getFriendlyRateLimitMessage,
|
||||||
|
getUserFacingRequestErrorMessage,
|
||||||
|
isRateLimitError,
|
||||||
|
} from "@/lib/request-error";
|
||||||
|
|
||||||
export const maxDuration = 180;
|
export const maxDuration = 180;
|
||||||
export async function POST(request: Request) {
|
|
||||||
const session = await auth();
|
function parseIncludeBase64(value: unknown): boolean {
|
||||||
if (!session?.user?.id) return NextResponse.json({ error: "未登录" }, { status: 401 });
|
if (typeof value === "boolean") return value;
|
||||||
return executeExtendPOST(request, session.user.id);
|
const normalized = String(value ?? "").trim().toLowerCase();
|
||||||
|
return normalized === "true" || normalized === "1" || normalized === "yes";
|
||||||
|
}
|
||||||
|
|
||||||
|
function buildExtendSuccessPayload(result: Awaited<ReturnType<typeof executeExtendJob>>) {
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
original_url: result.original_url,
|
||||||
|
thumbnail_url: result.thumbnail_url,
|
||||||
|
...(result.image_base64 ? { base64: result.image_base64 } : {}),
|
||||||
|
processing_time: result.processing_time,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function POST(request: NextRequest) {
|
||||||
|
try {
|
||||||
|
const session = await auth();
|
||||||
|
if (!session?.user?.id) {
|
||||||
|
return NextResponse.json({ error: "未授权,请先登录" }, { status: 401 });
|
||||||
|
}
|
||||||
|
|
||||||
|
const contentType = request.headers.get("content-type") || "";
|
||||||
|
|
||||||
|
let imageUrl: string | null = null;
|
||||||
|
let imageBase64: string | null = null;
|
||||||
|
let prompt = "图像拓展处理";
|
||||||
|
let chatId: string | null = null;
|
||||||
|
let messageId: string | null = null;
|
||||||
|
let includeBase64 = false;
|
||||||
|
const requestId = `extend-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
|
||||||
|
|
||||||
|
if (contentType.includes("application/json")) {
|
||||||
|
const body = await request.json();
|
||||||
|
imageUrl = body.image_url ? String(body.image_url).trim() : null;
|
||||||
|
imageBase64 = body.image_base64 ? String(body.image_base64).trim() : null;
|
||||||
|
prompt = body.prompt || prompt;
|
||||||
|
chatId = body.chatId || null;
|
||||||
|
messageId = body.messageId || null;
|
||||||
|
includeBase64 = parseIncludeBase64(body.includeBase64);
|
||||||
|
|
||||||
|
if (!imageUrl && !imageBase64) {
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: "缺少image_base64或image_url参数" },
|
||||||
|
{ status: 400 },
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("[Extend API] Request received:", {
|
||||||
|
requestId,
|
||||||
|
source: imageUrl ? "url" : "base64",
|
||||||
|
includeBase64,
|
||||||
|
chatId,
|
||||||
|
messageId,
|
||||||
|
xScale: body.xScale,
|
||||||
|
yScale: body.yScale,
|
||||||
|
});
|
||||||
|
|
||||||
|
const pointsConfig = await getPointsSystemConfig();
|
||||||
|
const pointsCharge = await prepareFeaturePointsCharge({
|
||||||
|
userId: session.user.id,
|
||||||
|
unitCost: getImageToolCreditCost(pointsConfig, "extend"),
|
||||||
|
reason: "image_extend",
|
||||||
|
modelId: "image-extend",
|
||||||
|
note: "图片扩图消耗积分",
|
||||||
|
pointsConfig,
|
||||||
|
});
|
||||||
|
if (!pointsCharge.ok) {
|
||||||
|
return NextResponse.json(pointsCharge.payload, { status: pointsCharge.status });
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = await executeExtendJob({
|
||||||
|
userId: session.user.id,
|
||||||
|
imageUrl,
|
||||||
|
imageBase64,
|
||||||
|
prompt,
|
||||||
|
chatId,
|
||||||
|
messageId,
|
||||||
|
xScale: body.xScale ? Number(body.xScale) : undefined,
|
||||||
|
yScale: body.yScale ? Number(body.yScale) : undefined,
|
||||||
|
includeBase64,
|
||||||
|
requestId,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (pointsCharge.chargeable) {
|
||||||
|
try {
|
||||||
|
await pointsCharge.consume();
|
||||||
|
} catch (pointsError) {
|
||||||
|
console.error("[Points] Failed to consume extend points:", pointsError);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("[Extend API] Request completed:", {
|
||||||
|
requestId,
|
||||||
|
includeBase64,
|
||||||
|
processingTime: result.processing_time,
|
||||||
|
originalUrl: result.original_url.slice(0, 120),
|
||||||
|
});
|
||||||
|
|
||||||
|
return NextResponse.json(buildExtendSuccessPayload(result));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (contentType.includes("multipart/form-data")) {
|
||||||
|
const formData = await request.formData();
|
||||||
|
const file = formData.get("file") as File | null;
|
||||||
|
if (!file) {
|
||||||
|
return NextResponse.json({ error: "缺少file参数" }, { status: 400 });
|
||||||
|
}
|
||||||
|
|
||||||
|
const arrayBuffer = await file.arrayBuffer();
|
||||||
|
const buffer = Buffer.from(arrayBuffer);
|
||||||
|
const multipartChatId = String(formData.get("chatId") || "") || null;
|
||||||
|
const multipartMessageId = String(formData.get("messageId") || "") || null;
|
||||||
|
includeBase64 = parseIncludeBase64(formData.get("includeBase64"));
|
||||||
|
console.log("[Extend API] Multipart request received:", {
|
||||||
|
requestId,
|
||||||
|
includeBase64,
|
||||||
|
chatId: multipartChatId,
|
||||||
|
messageId: multipartMessageId,
|
||||||
|
bytes: buffer.length,
|
||||||
|
});
|
||||||
|
|
||||||
|
const pointsConfig = await getPointsSystemConfig();
|
||||||
|
const pointsCharge = await prepareFeaturePointsCharge({
|
||||||
|
userId: session.user.id,
|
||||||
|
unitCost: getImageToolCreditCost(pointsConfig, "extend"),
|
||||||
|
reason: "image_extend",
|
||||||
|
modelId: "image-extend",
|
||||||
|
note: "图片扩图消耗积分",
|
||||||
|
pointsConfig,
|
||||||
|
});
|
||||||
|
if (!pointsCharge.ok) {
|
||||||
|
return NextResponse.json(pointsCharge.payload, { status: pointsCharge.status });
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = await executeExtendJob({
|
||||||
|
userId: session.user.id,
|
||||||
|
imageBase64: buffer.toString("base64"),
|
||||||
|
prompt: String(formData.get("prompt") || prompt),
|
||||||
|
chatId: multipartChatId,
|
||||||
|
messageId: multipartMessageId,
|
||||||
|
includeBase64,
|
||||||
|
requestId,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (pointsCharge.chargeable) {
|
||||||
|
try {
|
||||||
|
await pointsCharge.consume();
|
||||||
|
} catch (pointsError) {
|
||||||
|
console.error("[Points] Failed to consume extend points:", pointsError);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("[Extend API] Multipart request completed:", {
|
||||||
|
requestId,
|
||||||
|
includeBase64,
|
||||||
|
processingTime: result.processing_time,
|
||||||
|
originalUrl: result.original_url.slice(0, 120),
|
||||||
|
});
|
||||||
|
|
||||||
|
return NextResponse.json(buildExtendSuccessPayload(result));
|
||||||
|
}
|
||||||
|
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: "不支持的Content-Type,请使用application/json或multipart/form-data" },
|
||||||
|
{ status: 400 },
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof ExtendServiceError) {
|
||||||
|
if (error.status === 429 || isRateLimitError(error)) {
|
||||||
|
return NextResponse.json(
|
||||||
|
{
|
||||||
|
error: getFriendlyRateLimitMessage("当前操作过于频繁,请稍后再试"),
|
||||||
|
detail: error.detail || error.message,
|
||||||
|
retryAfter: 10,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
status: 429,
|
||||||
|
headers: {
|
||||||
|
"Retry-After": "10",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return NextResponse.json(
|
||||||
|
{
|
||||||
|
error: getUserFacingRequestErrorMessage(error, "图像拓展失败"),
|
||||||
|
detail: error.detail || error.message,
|
||||||
|
},
|
||||||
|
{ status: error.status },
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.error("[Extend API] 错误:", error);
|
||||||
|
if (isRateLimitError(error)) {
|
||||||
|
return NextResponse.json(
|
||||||
|
{
|
||||||
|
error: getFriendlyRateLimitMessage("当前操作过于频繁,请稍后再试"),
|
||||||
|
detail: error instanceof Error ? error.message : String(error),
|
||||||
|
retryAfter: 10,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
status: 429,
|
||||||
|
headers: {
|
||||||
|
"Retry-After": "10",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return NextResponse.json(
|
||||||
|
{
|
||||||
|
error: getUserFacingRequestErrorMessage(error, "图像拓展失败"),
|
||||||
|
detail: error instanceof Error ? error.message : String(error),
|
||||||
|
},
|
||||||
|
{ status: 500 },
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
119
app/api/external/image/generate/route.ts
vendored
119
app/api/external/image/generate/route.ts
vendored
@ -21,6 +21,103 @@ import {
|
|||||||
export const runtime = "nodejs";
|
export const runtime = "nodejs";
|
||||||
export const maxDuration = 1200;
|
export const maxDuration = 1200;
|
||||||
|
|
||||||
|
const EXTERNAL_IMAGE_API_LIMITER_KEY = "__external_image_api_limiter__";
|
||||||
|
const DEFAULT_EXTERNAL_IMAGE_API_MAX_CONCURRENT = 2;
|
||||||
|
const DEFAULT_EXTERNAL_IMAGE_API_MAX_QUEUE = 8;
|
||||||
|
const DEFAULT_EXTERNAL_IMAGE_API_QUEUE_TIMEOUT_MS = 15_000;
|
||||||
|
|
||||||
|
class ExternalImageApiBusyError extends Error {
|
||||||
|
readonly retryAfterSeconds: number;
|
||||||
|
|
||||||
|
constructor(message: string, retryAfterSeconds = 15) {
|
||||||
|
super(message);
|
||||||
|
this.name = "ExternalImageApiBusyError";
|
||||||
|
this.retryAfterSeconds = retryAfterSeconds;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type ExternalImageApiWaiter = {
|
||||||
|
resolve: (release: () => void) => void;
|
||||||
|
reject: (error: Error) => void;
|
||||||
|
timer: ReturnType<typeof setTimeout>;
|
||||||
|
};
|
||||||
|
|
||||||
|
type ExternalImageApiLimiterState = {
|
||||||
|
running: number;
|
||||||
|
queue: ExternalImageApiWaiter[];
|
||||||
|
};
|
||||||
|
|
||||||
|
function parsePositiveInteger(value: unknown, fallback: number) {
|
||||||
|
const parsed = Number.parseInt(String(value || ""), 10);
|
||||||
|
return Number.isFinite(parsed) && parsed > 0 ? parsed : fallback;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getExternalImageApiLimiterState(): ExternalImageApiLimiterState {
|
||||||
|
const globalState = globalThis as typeof globalThis & {
|
||||||
|
[EXTERNAL_IMAGE_API_LIMITER_KEY]?: ExternalImageApiLimiterState;
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!globalState[EXTERNAL_IMAGE_API_LIMITER_KEY]) {
|
||||||
|
globalState[EXTERNAL_IMAGE_API_LIMITER_KEY] = {
|
||||||
|
running: 0,
|
||||||
|
queue: [],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return globalState[EXTERNAL_IMAGE_API_LIMITER_KEY]!;
|
||||||
|
}
|
||||||
|
|
||||||
|
function acquireExternalImageApiSlot(): Promise<() => void> {
|
||||||
|
const state = getExternalImageApiLimiterState();
|
||||||
|
const maxConcurrent = parsePositiveInteger(
|
||||||
|
process.env.EXTERNAL_IMAGE_API_MAX_CONCURRENT,
|
||||||
|
DEFAULT_EXTERNAL_IMAGE_API_MAX_CONCURRENT,
|
||||||
|
);
|
||||||
|
const maxQueue = parsePositiveInteger(
|
||||||
|
process.env.EXTERNAL_IMAGE_API_MAX_QUEUE,
|
||||||
|
DEFAULT_EXTERNAL_IMAGE_API_MAX_QUEUE,
|
||||||
|
);
|
||||||
|
const queueTimeoutMs = parsePositiveInteger(
|
||||||
|
process.env.EXTERNAL_IMAGE_API_QUEUE_TIMEOUT_MS,
|
||||||
|
DEFAULT_EXTERNAL_IMAGE_API_QUEUE_TIMEOUT_MS,
|
||||||
|
);
|
||||||
|
|
||||||
|
const release = () => {
|
||||||
|
state.running = Math.max(0, state.running - 1);
|
||||||
|
const next = state.queue.shift();
|
||||||
|
if (!next) return;
|
||||||
|
clearTimeout(next.timer);
|
||||||
|
state.running += 1;
|
||||||
|
next.resolve(release);
|
||||||
|
};
|
||||||
|
|
||||||
|
if (state.running < maxConcurrent) {
|
||||||
|
state.running += 1;
|
||||||
|
return Promise.resolve(release);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (state.queue.length >= maxQueue) {
|
||||||
|
return Promise.reject(
|
||||||
|
new ExternalImageApiBusyError("图片生成服务繁忙,请稍后重试"),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const waiter: ExternalImageApiWaiter = {
|
||||||
|
resolve,
|
||||||
|
reject,
|
||||||
|
timer: setTimeout(() => {
|
||||||
|
const index = state.queue.indexOf(waiter);
|
||||||
|
if (index >= 0) {
|
||||||
|
state.queue.splice(index, 1);
|
||||||
|
}
|
||||||
|
reject(new ExternalImageApiBusyError("图片生成排队超时,请稍后重试"));
|
||||||
|
}, queueTimeoutMs),
|
||||||
|
};
|
||||||
|
state.queue.push(waiter);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const requestSchema = z.object({
|
const requestSchema = z.object({
|
||||||
prompt: z.string().trim().min(1, "prompt 不能为空"),
|
prompt: z.string().trim().min(1, "prompt 不能为空"),
|
||||||
negativePrompt: z.string().trim().optional(),
|
negativePrompt: z.string().trim().optional(),
|
||||||
@ -197,8 +294,10 @@ export async function POST(request: Request) {
|
|||||||
|
|
||||||
const startedAt = Date.now();
|
const startedAt = Date.now();
|
||||||
let logId: string | null = null;
|
let logId: string | null = null;
|
||||||
|
let releaseExternalImageApiSlot: (() => void) | null = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
releaseExternalImageApiSlot = await acquireExternalImageApiSlot();
|
||||||
const payload = await parseRequestPayload(request);
|
const payload = await parseRequestPayload(request);
|
||||||
const imageSize = normalizeExternalImageApiImageSize(payload.imageSize);
|
const imageSize = normalizeExternalImageApiImageSize(payload.imageSize);
|
||||||
const candidateModels = await buildExternalImageApiModelCandidates({
|
const candidateModels = await buildExternalImageApiModelCandidates({
|
||||||
@ -245,7 +344,6 @@ export async function POST(request: Request) {
|
|||||||
|
|
||||||
const generationResult = await generateImagesWithFailover({
|
const generationResult = await generateImagesWithFailover({
|
||||||
candidates: candidateModels,
|
candidates: candidateModels,
|
||||||
ownerKey: `external-image-api:${authResult.accessKey.id}`,
|
|
||||||
prompt: payload.prompt,
|
prompt: payload.prompt,
|
||||||
negativePrompt: payload.negativePrompt || null,
|
negativePrompt: payload.negativePrompt || null,
|
||||||
imageCount: payload.imageCount || 1,
|
imageCount: payload.imageCount || 1,
|
||||||
@ -369,6 +467,21 @@ export async function POST(request: Request) {
|
|||||||
.where(eq(externalImageApiRequestLog.id, logId));
|
.where(eq(externalImageApiRequestLog.id, logId));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (error instanceof ExternalImageApiBusyError) {
|
||||||
|
return NextResponse.json(
|
||||||
|
{
|
||||||
|
success: false,
|
||||||
|
error: error.message,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
status: 429,
|
||||||
|
headers: {
|
||||||
|
"Retry-After": String(error.retryAfterSeconds),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{
|
{
|
||||||
success: false,
|
success: false,
|
||||||
@ -376,5 +489,9 @@ export async function POST(request: Request) {
|
|||||||
},
|
},
|
||||||
{ status: 500 },
|
{ status: 500 },
|
||||||
);
|
);
|
||||||
|
} finally {
|
||||||
|
if (releaseExternalImageApiSlot) {
|
||||||
|
releaseExternalImageApiSlot();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,8 +1,4 @@
|
|||||||
import type {
|
import type { FlowBoardDraft, FlowBoardSnapshotData } from "@/lib/db/schema";
|
||||||
FlowBoardDraft,
|
|
||||||
FlowBoardSheetSnapshotData,
|
|
||||||
FlowBoardSnapshotData,
|
|
||||||
} from "@/lib/db/schema";
|
|
||||||
|
|
||||||
export function isRecord(value: unknown): value is Record<string, unknown> {
|
export function isRecord(value: unknown): value is Record<string, unknown> {
|
||||||
return typeof value === "object" && value !== null && !Array.isArray(value);
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
||||||
@ -12,73 +8,6 @@ function normalizeRecord(value: unknown): Record<string, unknown> | null {
|
|||||||
return isRecord(value) ? value : null;
|
return isRecord(value) ? value : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const DEFAULT_FLOW_BOARD_CANVAS = {
|
|
||||||
backgroundColor: "#f7f8fb",
|
|
||||||
gridColor: "#d8dde8",
|
|
||||||
gridEnabled: false,
|
|
||||||
gridSize: 24,
|
|
||||||
};
|
|
||||||
|
|
||||||
function normalizeCanvasColor(value: unknown, fallback: string) {
|
|
||||||
return typeof value === "string" && /^#[0-9a-f]{6}$/i.test(value)
|
|
||||||
? value.toLowerCase()
|
|
||||||
: fallback;
|
|
||||||
}
|
|
||||||
|
|
||||||
function normalizeFlowBoardCanvas(value: unknown): Record<string, unknown> {
|
|
||||||
const canvas = isRecord(value) ? value : {};
|
|
||||||
const requestedGridSize =
|
|
||||||
typeof canvas.gridSize === "number" && Number.isFinite(canvas.gridSize)
|
|
||||||
? Math.round(canvas.gridSize)
|
|
||||||
: DEFAULT_FLOW_BOARD_CANVAS.gridSize;
|
|
||||||
|
|
||||||
return {
|
|
||||||
backgroundColor: normalizeCanvasColor(
|
|
||||||
canvas.backgroundColor,
|
|
||||||
DEFAULT_FLOW_BOARD_CANVAS.backgroundColor
|
|
||||||
),
|
|
||||||
gridColor: normalizeCanvasColor(
|
|
||||||
canvas.gridColor,
|
|
||||||
DEFAULT_FLOW_BOARD_CANVAS.gridColor
|
|
||||||
),
|
|
||||||
gridEnabled:
|
|
||||||
typeof canvas.gridEnabled === "boolean"
|
|
||||||
? canvas.gridEnabled
|
|
||||||
: DEFAULT_FLOW_BOARD_CANVAS.gridEnabled,
|
|
||||||
gridSize: Math.min(64, Math.max(8, requestedGridSize)),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function normalizeSheetId(value: unknown, index: number) {
|
|
||||||
const id = typeof value === "string" ? value.trim().slice(0, 128) : "";
|
|
||||||
return id || `sheet-${index + 1}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
function normalizeSheetTitle(value: unknown, index: number) {
|
|
||||||
const title = typeof value === "string" ? value.trim().slice(0, 128) : "";
|
|
||||||
return title || `画布 ${index + 1}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
function normalizeFlowBoardSheet(
|
|
||||||
value: unknown,
|
|
||||||
index: number
|
|
||||||
): FlowBoardSheetSnapshotData | null {
|
|
||||||
if (!isRecord(value)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
id: normalizeSheetId(value.id, index),
|
|
||||||
title: normalizeSheetTitle(value.title, index),
|
|
||||||
children: Array.isArray(value.children)
|
|
||||||
? value.children.filter(isRecord)
|
|
||||||
: [],
|
|
||||||
viewport: normalizeRecord(value.viewport),
|
|
||||||
theme: normalizeRecord(value.theme),
|
|
||||||
canvas: normalizeFlowBoardCanvas(value.canvas),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export function normalizeFlowBoardTitle(value: unknown) {
|
export function normalizeFlowBoardTitle(value: unknown) {
|
||||||
const title = String(value || "").trim();
|
const title = String(value || "").trim();
|
||||||
return title.slice(0, 256) || "未命名流程白板";
|
return title.slice(0, 256) || "未命名流程白板";
|
||||||
@ -88,67 +17,15 @@ export function normalizeFlowBoardSnapshot(
|
|||||||
value: unknown
|
value: unknown
|
||||||
): FlowBoardSnapshotData {
|
): FlowBoardSnapshotData {
|
||||||
const raw = isRecord(value) ? value : {};
|
const raw = isRecord(value) ? value : {};
|
||||||
const legacyChildren = Array.isArray(raw.children)
|
|
||||||
? raw.children.filter(isRecord)
|
|
||||||
: [];
|
|
||||||
const normalizedSheets = Array.isArray(raw.sheets)
|
|
||||||
? raw.sheets
|
|
||||||
.map(normalizeFlowBoardSheet)
|
|
||||||
.filter(
|
|
||||||
(sheet): sheet is FlowBoardSheetSnapshotData => Boolean(sheet)
|
|
||||||
)
|
|
||||||
: [];
|
|
||||||
const sheets =
|
|
||||||
normalizedSheets.length > 0
|
|
||||||
? normalizedSheets
|
|
||||||
: [
|
|
||||||
{
|
|
||||||
id: "sheet-1",
|
|
||||||
title: "画布 1",
|
|
||||||
children: legacyChildren,
|
|
||||||
viewport: normalizeRecord(raw.viewport),
|
|
||||||
theme: normalizeRecord(raw.theme),
|
|
||||||
canvas: normalizeFlowBoardCanvas(raw.canvas),
|
|
||||||
},
|
|
||||||
];
|
|
||||||
const requestedActiveSheetId =
|
|
||||||
typeof raw.activeSheetId === "string" ? raw.activeSheetId.trim() : "";
|
|
||||||
const activeSheet =
|
|
||||||
sheets.find((sheet) => sheet.id === requestedActiveSheetId) || sheets[0];
|
|
||||||
const activeChildren = Array.isArray(raw.children)
|
|
||||||
? legacyChildren
|
|
||||||
: activeSheet.children;
|
|
||||||
const activeViewport = isRecord(raw.viewport)
|
|
||||||
? raw.viewport
|
|
||||||
: activeSheet.viewport;
|
|
||||||
const activeTheme = isRecord(raw.theme) ? raw.theme : activeSheet.theme;
|
|
||||||
const activeCanvas = isRecord(raw.canvas)
|
|
||||||
? normalizeFlowBoardCanvas(raw.canvas)
|
|
||||||
: normalizeFlowBoardCanvas(activeSheet.canvas);
|
|
||||||
const nextSheets = sheets.map((sheet) =>
|
|
||||||
sheet.id === activeSheet.id
|
|
||||||
? {
|
|
||||||
...sheet,
|
|
||||||
children: activeChildren,
|
|
||||||
viewport: activeViewport,
|
|
||||||
theme: activeTheme,
|
|
||||||
canvas: activeCanvas,
|
|
||||||
}
|
|
||||||
: sheet
|
|
||||||
);
|
|
||||||
const updatedAt =
|
const updatedAt =
|
||||||
typeof raw.updatedAt === "string" && raw.updatedAt.trim()
|
typeof raw.updatedAt === "string" && raw.updatedAt.trim()
|
||||||
? raw.updatedAt
|
? raw.updatedAt
|
||||||
: new Date().toISOString();
|
: new Date().toISOString();
|
||||||
|
|
||||||
return {
|
return {
|
||||||
version: 2,
|
children: Array.isArray(raw.children) ? raw.children.filter(isRecord) : [],
|
||||||
activeSheetId: activeSheet.id,
|
viewport: normalizeRecord(raw.viewport),
|
||||||
sheets: nextSheets,
|
theme: normalizeRecord(raw.theme),
|
||||||
children: activeChildren,
|
|
||||||
viewport: activeViewport,
|
|
||||||
theme: activeTheme,
|
|
||||||
canvas: activeCanvas,
|
|
||||||
updatedAt,
|
updatedAt,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
import { NextRequest, NextResponse } from "next/server";
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
import { db } from "@/lib/db";
|
import { db } from "@/lib/db";
|
||||||
import { generatedImage, imageArchiveTask } from "@/lib/db/schema";
|
import { generatedImage } from "@/lib/db/schema";
|
||||||
import { and, desc, eq, isNull, sql } from "drizzle-orm";
|
import { eq, desc, sql } from "drizzle-orm";
|
||||||
import { auth } from "@/app/(auth)/auth";
|
import { auth } from "@/app/(auth)/auth";
|
||||||
|
|
||||||
export async function GET(request: NextRequest) {
|
export async function GET(request: NextRequest) {
|
||||||
@ -19,23 +19,12 @@ export async function GET(request: NextRequest) {
|
|||||||
const page = Number.parseInt(searchParams.get("page") || "1");
|
const page = Number.parseInt(searchParams.get("page") || "1");
|
||||||
const pageSize = Number.parseInt(searchParams.get("pageSize") || "24");
|
const pageSize = Number.parseInt(searchParams.get("pageSize") || "24");
|
||||||
const offset = (page - 1) * pageSize;
|
const offset = (page - 1) * pageSize;
|
||||||
const visibleImageFilter = and(
|
|
||||||
eq(generatedImage.userId, userId),
|
|
||||||
isNull(imageArchiveTask.id)
|
|
||||||
);
|
|
||||||
|
|
||||||
// 查询总数
|
// 查询总数
|
||||||
const countResult = await db
|
const countResult = await db
|
||||||
.select({ count: sql<number>`count(*)::int` })
|
.select({ count: sql<number>`count(*)::int` })
|
||||||
.from(generatedImage)
|
.from(generatedImage)
|
||||||
.leftJoin(
|
.where(eq(generatedImage.userId, userId));
|
||||||
imageArchiveTask,
|
|
||||||
and(
|
|
||||||
eq(imageArchiveTask.sourceUrl, generatedImage.originalUrl),
|
|
||||||
eq(imageArchiveTask.status, "abandoned")
|
|
||||||
)
|
|
||||||
)
|
|
||||||
.where(visibleImageFilter);
|
|
||||||
|
|
||||||
const total = countResult[0]?.count || 0;
|
const total = countResult[0]?.count || 0;
|
||||||
|
|
||||||
@ -53,14 +42,7 @@ export async function GET(request: NextRequest) {
|
|||||||
modelId: generatedImage.modelId, // 添加 modelId 字段
|
modelId: generatedImage.modelId, // 添加 modelId 字段
|
||||||
})
|
})
|
||||||
.from(generatedImage)
|
.from(generatedImage)
|
||||||
.leftJoin(
|
.where(eq(generatedImage.userId, userId))
|
||||||
imageArchiveTask,
|
|
||||||
and(
|
|
||||||
eq(imageArchiveTask.sourceUrl, generatedImage.originalUrl),
|
|
||||||
eq(imageArchiveTask.status, "abandoned")
|
|
||||||
)
|
|
||||||
)
|
|
||||||
.where(visibleImageFilter)
|
|
||||||
.orderBy(desc(generatedImage.createdAt))
|
.orderBy(desc(generatedImage.createdAt))
|
||||||
.limit(pageSize)
|
.limit(pageSize)
|
||||||
.offset(offset);
|
.offset(offset);
|
||||||
|
|||||||
@ -1,34 +0,0 @@
|
|||||||
import { NextRequest, NextResponse } from "next/server";
|
|
||||||
import { eq } from "drizzle-orm";
|
|
||||||
import { db } from "@/lib/db";
|
|
||||||
import { imageArchiveTask } from "@/lib/db/schema";
|
|
||||||
|
|
||||||
// The SHA-256 URL is a capability, like the provider's signed image URL. It must be
|
|
||||||
// readable by image nodes and reference-image providers without a session cookie.
|
|
||||||
export async function GET(request: NextRequest, { params }: { params: Promise<{ key: string }> }) {
|
|
||||||
const { key } = await params;
|
|
||||||
if (!/^[a-f0-9]{64}$/.test(key)) return new NextResponse(null, { status: 404 });
|
|
||||||
const configured =
|
|
||||||
process.env.NEXT_PUBLIC_APP_URL || process.env.AUTH_URL || process.env.NEXTAUTH_URL;
|
|
||||||
const origin = configured
|
|
||||||
? new URL(configured).origin
|
|
||||||
: `http://localhost:${process.env.PORT || "3000"}`;
|
|
||||||
const sourceUrl = `${origin}/api/generated-image-preview/${key}`;
|
|
||||||
const [task] = await db
|
|
||||||
.select({ targetUrl: imageArchiveTask.targetUrl, metadata: imageArchiveTask.metadata })
|
|
||||||
.from(imageArchiveTask)
|
|
||||||
.where(eq(imageArchiveTask.sourceUrl, sourceUrl))
|
|
||||||
.limit(1);
|
|
||||||
if (task?.targetUrl) return NextResponse.redirect(task.targetUrl, 307);
|
|
||||||
const inline = (task?.metadata as { inlineSource?: string } | null)?.inlineSource;
|
|
||||||
const match = inline?.match(/^data:(image\/[a-z0-9.+-]+);base64,([a-z0-9+/=]+)$/i);
|
|
||||||
if (!match) return new NextResponse(null, { status: 404 });
|
|
||||||
return new NextResponse(Buffer.from(match[2], "base64"), {
|
|
||||||
headers: {
|
|
||||||
"Content-Type": match[1],
|
|
||||||
"Cache-Control": "private, no-store",
|
|
||||||
"X-Content-Type-Options": "nosniff",
|
|
||||||
"Content-Security-Policy": "default-src 'none'; sandbox",
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
@ -1,38 +0,0 @@
|
|||||||
import { WebStandardStreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/webStandardStreamableHttp.js";
|
|
||||||
import { NextResponse } from "next/server";
|
|
||||||
|
|
||||||
import { auth } from "@/app/(auth)/auth";
|
|
||||||
import { createCanvasCapabilityMcpServer } from "@/lib/canvas/capabilities/mcp-server";
|
|
||||||
|
|
||||||
export const dynamic = "force-dynamic";
|
|
||||||
export const maxDuration = 600;
|
|
||||||
|
|
||||||
async function handleMcpRequest(
|
|
||||||
request: Request,
|
|
||||||
{ params }: { params: Promise<{ id: string }> }
|
|
||||||
) {
|
|
||||||
const session = await auth();
|
|
||||||
if (!session?.user?.id) {
|
|
||||||
return NextResponse.json(
|
|
||||||
{ error: "未登录,无法访问画布 MCP 服务" },
|
|
||||||
{ status: 401 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const { id: projectId } = await params;
|
|
||||||
const transport = new WebStandardStreamableHTTPServerTransport({
|
|
||||||
enableJsonResponse: true,
|
|
||||||
});
|
|
||||||
const server = await createCanvasCapabilityMcpServer({
|
|
||||||
projectId,
|
|
||||||
requestHeaders: request.headers,
|
|
||||||
requestUrl: request.url,
|
|
||||||
userId: session.user.id,
|
|
||||||
});
|
|
||||||
await server.connect(transport);
|
|
||||||
return transport.handleRequest(request);
|
|
||||||
}
|
|
||||||
|
|
||||||
export const POST = handleMcpRequest;
|
|
||||||
export const GET = handleMcpRequest;
|
|
||||||
export const DELETE = handleMcpRequest;
|
|
||||||
@ -14,10 +14,6 @@ import {
|
|||||||
updateMessageById,
|
updateMessageById,
|
||||||
} from "@/lib/db/queries";
|
} from "@/lib/db/queries";
|
||||||
import type { DBMessage } from "@/lib/db/schema";
|
import type { DBMessage } from "@/lib/db/schema";
|
||||||
import {
|
|
||||||
buildOpenClawArtifactProxyUrl,
|
|
||||||
normalizeOpenClawArtifacts,
|
|
||||||
} from "@/lib/openclaw-chat/artifacts";
|
|
||||||
import {
|
import {
|
||||||
getFalImageTaskStatus,
|
getFalImageTaskStatus,
|
||||||
resolveFalImageTaskToMarkdown,
|
resolveFalImageTaskToMarkdown,
|
||||||
@ -36,32 +32,6 @@ const FAL_IMAGE_TASK_POLL_INTERVAL_MS = 2_000;
|
|||||||
const falTaskLastPolledAt = new Map<string, number>();
|
const falTaskLastPolledAt = new Map<string, number>();
|
||||||
const falTaskSyncInFlight = new Set<string>();
|
const falTaskSyncInFlight = new Set<string>();
|
||||||
|
|
||||||
function toPublicVideoMetadata(params: {
|
|
||||||
message: DBMessage;
|
|
||||||
requestOrigin: string;
|
|
||||||
}) {
|
|
||||||
const value = params.message.videoMetadata;
|
|
||||||
if (!value || typeof value !== "object" || Array.isArray(value)) return value;
|
|
||||||
const metadata = value as Record<string, unknown>;
|
|
||||||
if (metadata.taskKind !== "clawd_chat") return value;
|
|
||||||
const publicMetadata = { ...metadata };
|
|
||||||
delete publicMetadata.upstreamTaskKey;
|
|
||||||
publicMetadata.artifacts = normalizeOpenClawArtifacts(metadata.artifacts).map(
|
|
||||||
(artifact, artifactIndex) => ({
|
|
||||||
...artifact,
|
|
||||||
downloadUrl: new URL(
|
|
||||||
buildOpenClawArtifactProxyUrl({
|
|
||||||
chatId: params.message.chatId,
|
|
||||||
messageId: params.message.id,
|
|
||||||
artifactIndex,
|
|
||||||
}),
|
|
||||||
params.requestOrigin
|
|
||||||
).toString(),
|
|
||||||
})
|
|
||||||
);
|
|
||||||
return publicMetadata;
|
|
||||||
}
|
|
||||||
|
|
||||||
function createEmptyMessagesResponse() {
|
function createEmptyMessagesResponse() {
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ messages: [] },
|
{ messages: [] },
|
||||||
@ -307,11 +277,7 @@ export async function GET(request: NextRequest) {
|
|||||||
createdAt: msg.createdAt,
|
createdAt: msg.createdAt,
|
||||||
parts: parts,
|
parts: parts,
|
||||||
attachments: msg.attachments || [],
|
attachments: msg.attachments || [],
|
||||||
videoMetadata:
|
videoMetadata: msg.videoMetadata || null,
|
||||||
toPublicVideoMetadata({
|
|
||||||
message: msg,
|
|
||||||
requestOrigin: request.nextUrl.origin,
|
|
||||||
}) || null,
|
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -1,148 +0,0 @@
|
|||||||
import { and, eq, isNotNull } from "drizzle-orm";
|
|
||||||
import { z } from "zod";
|
|
||||||
import {
|
|
||||||
createUserAccessKey,
|
|
||||||
listUserAccessKeys,
|
|
||||||
} from "@/lib/access-keys/service";
|
|
||||||
import {
|
|
||||||
MIN_PROVISIONING_SECRET_LENGTH,
|
|
||||||
parseProvisioningBearerSecret,
|
|
||||||
verifyProvisioningSecret,
|
|
||||||
} from "@/lib/access-keys/provisioning-secret";
|
|
||||||
import { db } from "@/lib/db";
|
|
||||||
import { user } from "@/lib/db/schema";
|
|
||||||
|
|
||||||
const provisionAccessKeySchema = z.object({
|
|
||||||
dingTalkUsername: z.string().trim().min(1).max(100),
|
|
||||||
dingTalkUserId: z.string().trim().min(1).max(128).optional(),
|
|
||||||
keyName: z.string().trim().min(1).max(100).optional(),
|
|
||||||
scopes: z
|
|
||||||
.array(z.enum(["canvas:read", "canvas:write"]))
|
|
||||||
.min(1)
|
|
||||||
.optional(),
|
|
||||||
expiresInDays: z.number().int().min(1).max(365).optional(),
|
|
||||||
});
|
|
||||||
|
|
||||||
function jsonError(error: string, code: string, status: number) {
|
|
||||||
return Response.json(
|
|
||||||
{ success: false, error, code },
|
|
||||||
{ status, headers: { "Cache-Control": "no-store" } }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function POST(request: Request) {
|
|
||||||
const expectedSecret = String(
|
|
||||||
process.env.MCP_KEY_PROVISIONING_SECRET || ""
|
|
||||||
).trim();
|
|
||||||
if (expectedSecret.length < MIN_PROVISIONING_SECRET_LENGTH) {
|
|
||||||
return jsonError(
|
|
||||||
"Access Key provisioning is not configured",
|
|
||||||
"PROVISIONING_NOT_CONFIGURED",
|
|
||||||
503
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const providedSecret = parseProvisioningBearerSecret(
|
|
||||||
request.headers.get("authorization")
|
|
||||||
);
|
|
||||||
if (!verifyProvisioningSecret(providedSecret, expectedSecret)) {
|
|
||||||
return jsonError(
|
|
||||||
"Missing or invalid provisioning credential",
|
|
||||||
"PROVISIONING_UNAUTHORIZED",
|
|
||||||
401
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const parsedBody = provisionAccessKeySchema.safeParse(
|
|
||||||
await request.json().catch(() => null)
|
|
||||||
);
|
|
||||||
if (!parsedBody.success) {
|
|
||||||
return jsonError(
|
|
||||||
"Invalid provisioning request",
|
|
||||||
"PROVISIONING_REQUEST_INVALID",
|
|
||||||
400
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const body = parsedBody.data;
|
|
||||||
const conditions = [
|
|
||||||
eq(user.name, body.dingTalkUsername),
|
|
||||||
eq(user.isActive, true),
|
|
||||||
isNotNull(user.dingtalkUserId),
|
|
||||||
];
|
|
||||||
if (body.dingTalkUserId) {
|
|
||||||
conditions.push(eq(user.dingtalkUserId, body.dingTalkUserId));
|
|
||||||
}
|
|
||||||
|
|
||||||
const matches = await db
|
|
||||||
.select({
|
|
||||||
id: user.id,
|
|
||||||
name: user.name,
|
|
||||||
dingtalkUserId: user.dingtalkUserId,
|
|
||||||
})
|
|
||||||
.from(user)
|
|
||||||
.where(and(...conditions))
|
|
||||||
.limit(2);
|
|
||||||
|
|
||||||
if (matches.length === 0) {
|
|
||||||
return jsonError(
|
|
||||||
"No active DingTalk user matched the supplied identity",
|
|
||||||
"DINGTALK_USER_NOT_FOUND",
|
|
||||||
404
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if (matches.length > 1) {
|
|
||||||
return jsonError(
|
|
||||||
"DingTalk username is not unique; provide dingTalkUserId",
|
|
||||||
"DINGTALK_USERNAME_AMBIGUOUS",
|
|
||||||
409
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const matchedUser = matches[0];
|
|
||||||
const existingKeys = await listUserAccessKeys(matchedUser.id);
|
|
||||||
const activeKeyCount = existingKeys.filter(
|
|
||||||
(item) =>
|
|
||||||
!item.revokedAt &&
|
|
||||||
(!item.expiresAt || new Date(item.expiresAt).getTime() > Date.now())
|
|
||||||
).length;
|
|
||||||
if (activeKeyCount >= 10) {
|
|
||||||
return jsonError(
|
|
||||||
"The user already has 10 active Access Keys",
|
|
||||||
"ACCESS_KEY_LIMIT_REACHED",
|
|
||||||
409
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
const accessKey = await createUserAccessKey({
|
|
||||||
userId: matchedUser.id,
|
|
||||||
name: body.keyName || "第三方 OpenClaw",
|
|
||||||
scopes: body.scopes,
|
|
||||||
expiresInDays: body.expiresInDays,
|
|
||||||
});
|
|
||||||
|
|
||||||
return Response.json(
|
|
||||||
{
|
|
||||||
success: true,
|
|
||||||
data: {
|
|
||||||
user: matchedUser,
|
|
||||||
accessKey,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
status: 201,
|
|
||||||
headers: {
|
|
||||||
"Cache-Control": "no-store",
|
|
||||||
Pragma: "no-cache",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
);
|
|
||||||
} catch {
|
|
||||||
return jsonError(
|
|
||||||
"Failed to provision Access Key",
|
|
||||||
"ACCESS_KEY_PROVISION_FAILED",
|
|
||||||
500
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,48 +0,0 @@
|
|||||||
import { getCanvasAgentFeatureFlagsFromSystemSettings } from "@/lib/canvas/agent-feature-settings";
|
|
||||||
import { applyCanvasOperationPlan } from "@/lib/canvas/agent-mutations";
|
|
||||||
import { handleCanvasAgentOperationRouteRequest } from "@/lib/canvas/agent-operation-route";
|
|
||||||
import { z } from "zod";
|
|
||||||
import {
|
|
||||||
openApiErrorResponse,
|
|
||||||
requireOpenApiPrincipal,
|
|
||||||
} from "@/lib/access-keys/openapi-auth";
|
|
||||||
|
|
||||||
const canvasIdSchema = z.string().uuid();
|
|
||||||
|
|
||||||
export async function POST(
|
|
||||||
request: Request,
|
|
||||||
{ params }: { params: Promise<{ id: string }> }
|
|
||||||
) {
|
|
||||||
try {
|
|
||||||
const principal = await requireOpenApiPrincipal(request, "canvas:write");
|
|
||||||
const { id: rawId } = await params;
|
|
||||||
const parsedId = canvasIdSchema.safeParse(rawId);
|
|
||||||
if (!parsedId.success) {
|
|
||||||
return Response.json(
|
|
||||||
{
|
|
||||||
success: false,
|
|
||||||
error: "Invalid canvas ID",
|
|
||||||
code: "INVALID_CANVAS_ID",
|
|
||||||
},
|
|
||||||
{ status: 400 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const id = parsedId.data;
|
|
||||||
const response = await handleCanvasAgentOperationRouteRequest({
|
|
||||||
userId: principal.userId,
|
|
||||||
projectId: id,
|
|
||||||
readBody: () => request.json(),
|
|
||||||
features: await getCanvasAgentFeatureFlagsFromSystemSettings(),
|
|
||||||
applyOperation: applyCanvasOperationPlan,
|
|
||||||
logger: console,
|
|
||||||
});
|
|
||||||
|
|
||||||
return Response.json(response.body, {
|
|
||||||
status: response.status,
|
|
||||||
headers: { "Cache-Control": "no-store" },
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
return openApiErrorResponse(error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,57 +0,0 @@
|
|||||||
import { and, eq } from "drizzle-orm";
|
|
||||||
import { z } from "zod";
|
|
||||||
import { db } from "@/lib/db";
|
|
||||||
import { canvasProject } from "@/lib/db/schema";
|
|
||||||
import {
|
|
||||||
openApiErrorResponse,
|
|
||||||
requireOpenApiPrincipal,
|
|
||||||
} from "@/lib/access-keys/openapi-auth";
|
|
||||||
|
|
||||||
const canvasIdSchema = z.string().uuid();
|
|
||||||
|
|
||||||
export async function GET(
|
|
||||||
request: Request,
|
|
||||||
{ params }: { params: Promise<{ id: string }> }
|
|
||||||
) {
|
|
||||||
try {
|
|
||||||
const principal = await requireOpenApiPrincipal(request, "canvas:read");
|
|
||||||
const { id: rawId } = await params;
|
|
||||||
const parsedId = canvasIdSchema.safeParse(rawId);
|
|
||||||
if (!parsedId.success) {
|
|
||||||
return Response.json(
|
|
||||||
{
|
|
||||||
success: false,
|
|
||||||
error: "Invalid canvas ID",
|
|
||||||
code: "INVALID_CANVAS_ID",
|
|
||||||
},
|
|
||||||
{ status: 400 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const id = parsedId.data;
|
|
||||||
const [project] = await db
|
|
||||||
.select()
|
|
||||||
.from(canvasProject)
|
|
||||||
.where(
|
|
||||||
and(
|
|
||||||
eq(canvasProject.id, id),
|
|
||||||
eq(canvasProject.userId, principal.userId)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
.limit(1);
|
|
||||||
|
|
||||||
if (!project) {
|
|
||||||
return Response.json(
|
|
||||||
{ success: false, error: "Canvas not found", code: "CANVAS_NOT_FOUND" },
|
|
||||||
{ status: 404 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return Response.json(
|
|
||||||
{ success: true, data: project },
|
|
||||||
{ headers: { "Cache-Control": "no-store" } }
|
|
||||||
);
|
|
||||||
} catch (error) {
|
|
||||||
return openApiErrorResponse(error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,52 +0,0 @@
|
|||||||
import { and, desc, eq } from "drizzle-orm";
|
|
||||||
import { db } from "@/lib/db";
|
|
||||||
import { canvasProject } from "@/lib/db/schema";
|
|
||||||
import {
|
|
||||||
openApiErrorResponse,
|
|
||||||
requireOpenApiPrincipal,
|
|
||||||
} from "@/lib/access-keys/openapi-auth";
|
|
||||||
|
|
||||||
function normalizeWorkspaceType(value: string | null) {
|
|
||||||
return value === "agent" || value === "video" ? value : "canvas";
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function GET(request: Request) {
|
|
||||||
try {
|
|
||||||
const principal = await requireOpenApiPrincipal(request, "canvas:read");
|
|
||||||
const url = new URL(request.url);
|
|
||||||
const workspaceType = normalizeWorkspaceType(
|
|
||||||
url.searchParams.get("workspaceType")
|
|
||||||
);
|
|
||||||
const requestedLimit = Number(url.searchParams.get("limit") || 24);
|
|
||||||
const limit = Number.isFinite(requestedLimit)
|
|
||||||
? Math.min(50, Math.max(1, Math.round(requestedLimit)))
|
|
||||||
: 24;
|
|
||||||
|
|
||||||
const projects = await db
|
|
||||||
.select({
|
|
||||||
id: canvasProject.id,
|
|
||||||
title: canvasProject.title,
|
|
||||||
description: canvasProject.description,
|
|
||||||
thumbnail: canvasProject.thumbnail,
|
|
||||||
workspaceType: canvasProject.workspaceType,
|
|
||||||
createdAt: canvasProject.createdAt,
|
|
||||||
updatedAt: canvasProject.updatedAt,
|
|
||||||
})
|
|
||||||
.from(canvasProject)
|
|
||||||
.where(
|
|
||||||
and(
|
|
||||||
eq(canvasProject.userId, principal.userId),
|
|
||||||
eq(canvasProject.workspaceType, workspaceType)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
.orderBy(desc(canvasProject.updatedAt))
|
|
||||||
.limit(limit);
|
|
||||||
|
|
||||||
return Response.json(
|
|
||||||
{ success: true, data: projects },
|
|
||||||
{ headers: { "Cache-Control": "no-store" } }
|
|
||||||
);
|
|
||||||
} catch (error) {
|
|
||||||
return openApiErrorResponse(error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,41 +0,0 @@
|
|||||||
import { eq } from "drizzle-orm";
|
|
||||||
import { db } from "@/lib/db";
|
|
||||||
import { user } from "@/lib/db/schema";
|
|
||||||
import {
|
|
||||||
openApiErrorResponse,
|
|
||||||
requireOpenApiPrincipal,
|
|
||||||
} from "@/lib/access-keys/openapi-auth";
|
|
||||||
|
|
||||||
export async function GET(request: Request) {
|
|
||||||
try {
|
|
||||||
const principal = await requireOpenApiPrincipal(request, "canvas:read");
|
|
||||||
const [currentUser] = await db
|
|
||||||
.select({
|
|
||||||
id: user.id,
|
|
||||||
email: user.email,
|
|
||||||
name: user.name,
|
|
||||||
department: user.department,
|
|
||||||
role: user.role,
|
|
||||||
})
|
|
||||||
.from(user)
|
|
||||||
.where(eq(user.id, principal.userId))
|
|
||||||
.limit(1);
|
|
||||||
|
|
||||||
return Response.json(
|
|
||||||
{
|
|
||||||
success: true,
|
|
||||||
data: {
|
|
||||||
user: currentUser,
|
|
||||||
accessKey: {
|
|
||||||
id: principal.accessKeyId,
|
|
||||||
name: principal.accessKeyName,
|
|
||||||
scopes: principal.scopes,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{ headers: { "Cache-Control": "no-store" } }
|
|
||||||
);
|
|
||||||
} catch (error) {
|
|
||||||
return openApiErrorResponse(error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,10 +1,138 @@
|
|||||||
import { NextResponse } from "next/server";
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
|
|
||||||
import { auth } from "@/app/(auth)/auth";
|
import { auth } from "@/app/(auth)/auth";
|
||||||
import { executeRemoveBackgroundPOST } from "@/lib/canvas/operations/rmbg";
|
import {
|
||||||
|
executeRmbgJob,
|
||||||
|
fetchImageBufferFromUrl,
|
||||||
|
RmbgServiceError,
|
||||||
|
} from "@/lib/ai/rmbg/service";
|
||||||
|
import { getPointsSystemConfig } from "@/lib/db/queries";
|
||||||
|
import { prepareFeaturePointsCharge } from "@/lib/points/feature-charge";
|
||||||
|
import { getImageToolCreditCost } from "@/lib/points/config";
|
||||||
|
|
||||||
export const runtime = "nodejs";
|
export const runtime = "nodejs";
|
||||||
export const maxDuration = 60;
|
export const maxDuration = 60;
|
||||||
export async function POST(request: Request) {
|
|
||||||
const session = await auth();
|
export async function POST(request: NextRequest) {
|
||||||
if (!session?.user?.id) return NextResponse.json({ error: "未登录" }, { status: 401 });
|
try {
|
||||||
return executeRemoveBackgroundPOST(request, session.user.id);
|
const session = await auth();
|
||||||
|
if (!session?.user?.id) {
|
||||||
|
return NextResponse.json({ error: "未授权,请先登录" }, { status: 401 });
|
||||||
|
}
|
||||||
|
|
||||||
|
const contentType = request.headers.get("content-type") || "";
|
||||||
|
|
||||||
|
let imageBuffer: Buffer;
|
||||||
|
let imageBase64: string | undefined;
|
||||||
|
let prompt = "抠图处理";
|
||||||
|
let chatId: string | null = null;
|
||||||
|
let messageId: string | null = null;
|
||||||
|
let skipGallerySave = false;
|
||||||
|
let sourceImageUrl: string | null = null;
|
||||||
|
|
||||||
|
if (contentType.includes("application/json")) {
|
||||||
|
const body = await request.json();
|
||||||
|
prompt = body.prompt || "抠图处理";
|
||||||
|
chatId = body.chatId || null;
|
||||||
|
messageId = body.messageId || null;
|
||||||
|
skipGallerySave = body.skipGallerySave === true || body.skip_gallery_save === 1;
|
||||||
|
|
||||||
|
const imageUrl = body.imageUrl || body.image_url;
|
||||||
|
const explicitSourceImageUrl = body.sourceImageUrl || body.source_image_url;
|
||||||
|
|
||||||
|
if (imageUrl) {
|
||||||
|
sourceImageUrl = String(imageUrl);
|
||||||
|
imageBuffer = await fetchImageBufferFromUrl(sourceImageUrl);
|
||||||
|
} else if (body.image_base64) {
|
||||||
|
imageBase64 = String(body.image_base64);
|
||||||
|
imageBuffer = Buffer.from(imageBase64, "base64");
|
||||||
|
if (explicitSourceImageUrl) {
|
||||||
|
sourceImageUrl = String(explicitSourceImageUrl);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: "缺少 imageUrl 或 image_base64 参数" },
|
||||||
|
{ status: 400 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else if (contentType.includes("multipart/form-data")) {
|
||||||
|
const formData = await request.formData();
|
||||||
|
const file = formData.get("file") as File | null;
|
||||||
|
|
||||||
|
if (!file) {
|
||||||
|
return NextResponse.json({ error: "缺少file参数" }, { status: 400 });
|
||||||
|
}
|
||||||
|
|
||||||
|
const arrayBuffer = await file.arrayBuffer();
|
||||||
|
imageBuffer = Buffer.from(arrayBuffer);
|
||||||
|
imageBase64 = imageBuffer.toString("base64");
|
||||||
|
prompt = (formData.get("prompt") as string) || "抠图处理";
|
||||||
|
chatId = (formData.get("chatId") as string) || null;
|
||||||
|
messageId = (formData.get("messageId") as string) || null;
|
||||||
|
skipGallerySave =
|
||||||
|
formData.get("skipGallerySave") === "true" || formData.get("skip_gallery_save") === "1";
|
||||||
|
sourceImageUrl =
|
||||||
|
(formData.get("sourceImageUrl") as string) ||
|
||||||
|
(formData.get("source_image_url") as string) ||
|
||||||
|
null;
|
||||||
|
} else {
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: "不支持的Content-Type,请使用application/json或multipart/form-data" },
|
||||||
|
{ status: 400 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const pointsConfig = await getPointsSystemConfig();
|
||||||
|
const pointsCharge = await prepareFeaturePointsCharge({
|
||||||
|
userId: session.user.id,
|
||||||
|
unitCost: getImageToolCreditCost(pointsConfig, "remove-background"),
|
||||||
|
reason: "image_remove_background",
|
||||||
|
modelId: "rmbg",
|
||||||
|
note: "图片抠图消耗积分",
|
||||||
|
pointsConfig,
|
||||||
|
});
|
||||||
|
if (!pointsCharge.ok) {
|
||||||
|
return NextResponse.json(pointsCharge.payload, { status: pointsCharge.status });
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = await executeRmbgJob({
|
||||||
|
userId: session.user.id,
|
||||||
|
imageBuffer,
|
||||||
|
imageBase64,
|
||||||
|
prompt,
|
||||||
|
chatId,
|
||||||
|
messageId,
|
||||||
|
skipGallerySave,
|
||||||
|
sourceImageUrl,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (pointsCharge.chargeable) {
|
||||||
|
try {
|
||||||
|
await pointsCharge.consume();
|
||||||
|
} catch (pointsError) {
|
||||||
|
console.error("[Points] Failed to consume RMBG points:", pointsError);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return NextResponse.json({
|
||||||
|
success: true,
|
||||||
|
...result,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof RmbgServiceError) {
|
||||||
|
return NextResponse.json(
|
||||||
|
{
|
||||||
|
error: error.message,
|
||||||
|
detail: error.detail || error.message,
|
||||||
|
},
|
||||||
|
{ status: error.status }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.error("[RMBG API] 处理错误:", error);
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: "服务器内部错误", detail: error instanceof Error ? error.message : "未知错误" },
|
||||||
|
{ status: 500 }
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -26,9 +26,6 @@ import { createFalImage } from "@/lib/ai/providers/fal-image";
|
|||||||
import { createSnapgenImage } from "@/lib/ai/providers/snapgen-image";
|
import { createSnapgenImage } from "@/lib/ai/providers/snapgen-image";
|
||||||
import { createOpenAIImage } from "@/lib/ai/providers/openai-image";
|
import { createOpenAIImage } from "@/lib/ai/providers/openai-image";
|
||||||
import { createBananaImage } from "@/lib/ai/providers/bananaimage";
|
import { createBananaImage } from "@/lib/ai/providers/bananaimage";
|
||||||
import { createVisionary } from "@/lib/ai/providers/visionary";
|
|
||||||
import { createAtlasCloud } from "@/lib/ai/providers/atlascloud";
|
|
||||||
import { createVip98k } from "@/lib/ai/providers/vip98k";
|
|
||||||
import { callQwen3Vl } from "@/lib/ai/qwen3-vl";
|
import { callQwen3Vl } from "@/lib/ai/qwen3-vl";
|
||||||
import {
|
import {
|
||||||
canProcessSceneFusionProviderType,
|
canProcessSceneFusionProviderType,
|
||||||
@ -1479,8 +1476,7 @@ async function processSceneWithProvider(
|
|||||||
modelId: string,
|
modelId: string,
|
||||||
runtimeModelId: string,
|
runtimeModelId: string,
|
||||||
aspectRatio?: string,
|
aspectRatio?: string,
|
||||||
imageSize?: string,
|
imageSize?: string
|
||||||
imageGenConfig?: Record<string, any> | null
|
|
||||||
): Promise<{ thumbnail: string; original: string; prompt: string }> {
|
): Promise<{ thumbnail: string; original: string; prompt: string }> {
|
||||||
// 构建智能提示词
|
// 构建智能提示词
|
||||||
const prompt = buildPrompt(
|
const prompt = buildPrompt(
|
||||||
@ -1543,41 +1539,6 @@ async function processSceneWithProvider(
|
|||||||
imageSize,
|
imageSize,
|
||||||
);
|
);
|
||||||
|
|
||||||
case "visionary":
|
|
||||||
return processWithVisionary(
|
|
||||||
productImageUrl,
|
|
||||||
sceneImageUrl,
|
|
||||||
prompt,
|
|
||||||
provider,
|
|
||||||
runtimeModelId || modelId,
|
|
||||||
aspectRatio,
|
|
||||||
imageSize,
|
|
||||||
);
|
|
||||||
|
|
||||||
case "atlascloud":
|
|
||||||
return processWithAtlasCloud(
|
|
||||||
productImageUrl,
|
|
||||||
sceneImageUrl,
|
|
||||||
prompt,
|
|
||||||
provider,
|
|
||||||
runtimeModelId || modelId,
|
|
||||||
aspectRatio,
|
|
||||||
imageSize,
|
|
||||||
imageGenConfig,
|
|
||||||
);
|
|
||||||
|
|
||||||
case "vip98k":
|
|
||||||
return processWithVip98k(
|
|
||||||
productImageUrl,
|
|
||||||
sceneImageUrl,
|
|
||||||
prompt,
|
|
||||||
provider,
|
|
||||||
runtimeModelId || modelId,
|
|
||||||
aspectRatio,
|
|
||||||
imageSize,
|
|
||||||
imageGenConfig,
|
|
||||||
);
|
|
||||||
|
|
||||||
case "gemini":
|
case "gemini":
|
||||||
case "gemini-image":
|
case "gemini-image":
|
||||||
return processWithGemini(productImageUrl, sceneImageUrl, prompt, provider, modelId, aspectRatio, imageSize);
|
return processWithGemini(productImageUrl, sceneImageUrl, prompt, provider, modelId, aspectRatio, imageSize);
|
||||||
@ -1870,121 +1831,6 @@ async function processWithBananaImage(
|
|||||||
return { thumbnail, original, prompt };
|
return { thumbnail, original, prompt };
|
||||||
}
|
}
|
||||||
|
|
||||||
async function processWithVisionary(
|
|
||||||
productImageUrl: string,
|
|
||||||
sceneImageUrl: string,
|
|
||||||
prompt: string,
|
|
||||||
provider: { baseUrl: string; apiKey: string },
|
|
||||||
modelId: string,
|
|
||||||
aspectRatio?: string,
|
|
||||||
imageSize?: string
|
|
||||||
): Promise<{ thumbnail: string; original: string; prompt: string }> {
|
|
||||||
logger.info("provider_processing", { providerType: "visionary", modelId });
|
|
||||||
|
|
||||||
const visionaryProvider = createVisionary({
|
|
||||||
baseURL: provider.baseUrl,
|
|
||||||
apiKey: provider.apiKey,
|
|
||||||
});
|
|
||||||
const result = await visionaryProvider.chat(modelId).doGenerate({
|
|
||||||
messages: [
|
|
||||||
{
|
|
||||||
role: "user",
|
|
||||||
content: [
|
|
||||||
{ type: "image_url", image_url: { url: productImageUrl } },
|
|
||||||
{ type: "image_url", image_url: { url: sceneImageUrl } },
|
|
||||||
{ type: "text", text: prompt },
|
|
||||||
],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
imageParams: {
|
|
||||||
aspectRatio: aspectRatio || "auto",
|
|
||||||
imageSize: imageSize || "2K",
|
|
||||||
imageCount: 1,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const { thumbnail, original } = parseImageFromMarkdown(result.text);
|
|
||||||
return { thumbnail, original, prompt };
|
|
||||||
}
|
|
||||||
|
|
||||||
async function processWithAtlasCloud(
|
|
||||||
productImageUrl: string,
|
|
||||||
sceneImageUrl: string,
|
|
||||||
prompt: string,
|
|
||||||
provider: { baseUrl: string; apiKey: string },
|
|
||||||
modelId: string,
|
|
||||||
aspectRatio?: string,
|
|
||||||
imageSize?: string,
|
|
||||||
imageGenConfig?: Record<string, any> | null
|
|
||||||
): Promise<{ thumbnail: string; original: string; prompt: string }> {
|
|
||||||
logger.info("provider_processing", { providerType: "atlascloud", modelId });
|
|
||||||
|
|
||||||
const atlasCloudProvider = createAtlasCloud({
|
|
||||||
baseURL: provider.baseUrl,
|
|
||||||
apiKey: provider.apiKey,
|
|
||||||
});
|
|
||||||
const result = await atlasCloudProvider.chat(modelId).doGenerate({
|
|
||||||
messages: [
|
|
||||||
{
|
|
||||||
role: "user",
|
|
||||||
content: [
|
|
||||||
{ type: "image_url", image_url: { url: productImageUrl } },
|
|
||||||
{ type: "image_url", image_url: { url: sceneImageUrl } },
|
|
||||||
{ type: "text", text: prompt },
|
|
||||||
],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
imageParams: {
|
|
||||||
aspectRatio: aspectRatio || "auto",
|
|
||||||
imageSize: imageSize || "2K",
|
|
||||||
imageCount: 1,
|
|
||||||
},
|
|
||||||
imageGenConfig,
|
|
||||||
});
|
|
||||||
|
|
||||||
const { thumbnail, original } = parseImageFromMarkdown(result.text);
|
|
||||||
return { thumbnail, original, prompt };
|
|
||||||
}
|
|
||||||
|
|
||||||
async function processWithVip98k(
|
|
||||||
productImageUrl: string,
|
|
||||||
sceneImageUrl: string,
|
|
||||||
prompt: string,
|
|
||||||
provider: { baseUrl: string; apiKey: string },
|
|
||||||
modelId: string,
|
|
||||||
aspectRatio?: string,
|
|
||||||
imageSize?: string,
|
|
||||||
imageGenConfig?: Record<string, any> | null
|
|
||||||
): Promise<{ thumbnail: string; original: string; prompt: string }> {
|
|
||||||
logger.info("provider_processing", { providerType: "vip98k", modelId });
|
|
||||||
|
|
||||||
const vip98kProvider = createVip98k({
|
|
||||||
baseURL: provider.baseUrl,
|
|
||||||
apiKey: provider.apiKey,
|
|
||||||
});
|
|
||||||
const result = await vip98kProvider.chat(modelId, imageGenConfig || null).doGenerate({
|
|
||||||
messages: [
|
|
||||||
{
|
|
||||||
role: "user",
|
|
||||||
content: [
|
|
||||||
{ type: "image_url", image_url: { url: productImageUrl } },
|
|
||||||
{ type: "image_url", image_url: { url: sceneImageUrl } },
|
|
||||||
{ type: "text", text: prompt },
|
|
||||||
],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
imageParams: {
|
|
||||||
aspectRatio: aspectRatio || "auto",
|
|
||||||
imageSize: imageSize || "2K",
|
|
||||||
imageCount: 1,
|
|
||||||
},
|
|
||||||
imageGenConfig,
|
|
||||||
});
|
|
||||||
|
|
||||||
const { thumbnail, original } = parseImageFromMarkdown(result.text);
|
|
||||||
return { thumbnail, original, prompt };
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 使用既梦/火山模型处理
|
* 使用既梦/火山模型处理
|
||||||
*/
|
*/
|
||||||
@ -2240,8 +2086,7 @@ async function processSceneWithDiversity(
|
|||||||
candidate.model.modelId,
|
candidate.model.modelId,
|
||||||
runtimeModelId,
|
runtimeModelId,
|
||||||
normalizedOutputParams.aspectRatio,
|
normalizedOutputParams.aspectRatio,
|
||||||
normalizedOutputParams.imageSize,
|
normalizedOutputParams.imageSize
|
||||||
(candidate.model.imageGenConfig as Record<string, any> | null) || null
|
|
||||||
);
|
);
|
||||||
} finally {
|
} finally {
|
||||||
await modelSlot.release();
|
await modelSlot.release();
|
||||||
|
|||||||
@ -399,7 +399,7 @@ function normalizeLovartComposerPreviewTexts(value: unknown): string[] {
|
|||||||
function normalizeLovartComposerPrefix(value: unknown): string {
|
function normalizeLovartComposerPrefix(value: unknown): string {
|
||||||
const normalized = String(value || "").trim();
|
const normalized = String(value || "").trim();
|
||||||
if (!normalized) {
|
if (!normalized) {
|
||||||
return "让「𝗕𝘂𝗬𝗶」";
|
return "让「由壹点AI」";
|
||||||
}
|
}
|
||||||
return normalized.slice(0, 40);
|
return normalized.slice(0, 40);
|
||||||
}
|
}
|
||||||
@ -573,21 +573,17 @@ const PUBLIC_SITE_SETTINGS_SCOPES = new Set<string>([
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
const HOME_SITE_SETTINGS_FALLBACK: Record<string, unknown> = {
|
const HOME_SITE_SETTINGS_FALLBACK: Record<string, unknown> = {
|
||||||
siteName: "𝗕𝘂𝗬𝗶",
|
siteName: "AI Chatbot",
|
||||||
siteLogo: null,
|
siteLogo: null,
|
||||||
siteLogoDark: null,
|
siteLogoDark: null,
|
||||||
favicon: null,
|
favicon: null,
|
||||||
lovartPromoText: "现在升级,立享 Kling 3.0 与 Nano Banana Pro,最高 5 折优惠",
|
lovartPromoText: "现在升级,立享 Kling 3.0 与 Nano Banana Pro,最高 5 折优惠",
|
||||||
lovartPromoActionText: "立即升级",
|
lovartPromoActionText: "立即升级",
|
||||||
bannerEnabled: true,
|
lovartHeroTitle: "由壹点AI 让设计更简单",
|
||||||
bannerText: "限时特惠:升级会员最高立享 45% OFF!",
|
lovartHeroSubtitle: "懂你的设计代理,帮你搞定一切",
|
||||||
bannerLink: null,
|
|
||||||
bannerActionText: "立即查看",
|
|
||||||
lovartHeroTitle: "𝗕𝘂𝗬𝗶 让设计触手可及",
|
|
||||||
lovartHeroSubtitle: "下达指令,即刻呈现",
|
|
||||||
surpriseCategories: ["全部", "精选"],
|
surpriseCategories: ["全部", "精选"],
|
||||||
canvasDefaultModelId: null,
|
canvasDefaultModelId: null,
|
||||||
lovartComposerPrefix: "让「𝗕𝘂𝗬𝗶」",
|
lovartComposerPrefix: "让「由壹点AI」",
|
||||||
lovartComposerPreviewTexts: [],
|
lovartComposerPreviewTexts: [],
|
||||||
lovartPromptTagVisibility: {},
|
lovartPromptTagVisibility: {},
|
||||||
contactConfig: normalizeContactConfig(undefined),
|
contactConfig: normalizeContactConfig(undefined),
|
||||||
@ -664,7 +660,7 @@ async function readScopedSiteSettings(keys: string[]) {
|
|||||||
|
|
||||||
function createBrandingSettings(settings: Record<string, unknown> | undefined) {
|
function createBrandingSettings(settings: Record<string, unknown> | undefined) {
|
||||||
return {
|
return {
|
||||||
siteName: String(settings?.siteName || "𝗕𝘂𝗬𝗶"),
|
siteName: String(settings?.siteName || "AI Chatbot"),
|
||||||
siteLogo: settings?.siteLogo || null,
|
siteLogo: settings?.siteLogo || null,
|
||||||
siteLogoDark: settings?.siteLogoDark || null,
|
siteLogoDark: settings?.siteLogoDark || null,
|
||||||
favicon: settings?.favicon || null,
|
favicon: settings?.favicon || null,
|
||||||
@ -822,7 +818,7 @@ async function getScopedPublicSiteSettings(
|
|||||||
usageTimeStart: typedSettings?.usageTimeStart || "08:00",
|
usageTimeStart: typedSettings?.usageTimeStart || "08:00",
|
||||||
usageTimeEnd: typedSettings?.usageTimeEnd || "20:00",
|
usageTimeEnd: typedSettings?.usageTimeEnd || "20:00",
|
||||||
usageTimeMessage:
|
usageTimeMessage:
|
||||||
typedSettings?.usageTimeMessage || "BuYi 累了也是需要休息的",
|
typedSettings?.usageTimeMessage || "由壹点累了也是需要休息的",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -901,7 +897,6 @@ async function getScopedPublicSiteSettings(
|
|||||||
bannerText:
|
bannerText:
|
||||||
typedSettings?.bannerText || "限时特惠:升级会员最高立享 45% OFF!",
|
typedSettings?.bannerText || "限时特惠:升级会员最高立享 45% OFF!",
|
||||||
bannerLink: typedSettings?.bannerLink || null,
|
bannerLink: typedSettings?.bannerLink || null,
|
||||||
bannerActionText: typedSettings?.bannerActionText || "立即查看",
|
|
||||||
lovartComposerPrefix: normalizeLovartComposerPrefix(
|
lovartComposerPrefix: normalizeLovartComposerPrefix(
|
||||||
valueByKey.get(LOVART_COMPOSER_PREFIX_KEY)
|
valueByKey.get(LOVART_COMPOSER_PREFIX_KEY)
|
||||||
),
|
),
|
||||||
@ -944,18 +939,13 @@ async function getScopedPublicSiteSettings(
|
|||||||
|
|
||||||
return createScopedSiteSettingsResponse(scope, {
|
return createScopedSiteSettingsResponse(scope, {
|
||||||
...createBrandingSettings(typedSettings),
|
...createBrandingSettings(typedSettings),
|
||||||
bannerEnabled: typedSettings?.bannerEnabled ?? true,
|
|
||||||
bannerText:
|
|
||||||
typedSettings?.bannerText || "限时特惠:升级会员最高立享 45% OFF!",
|
|
||||||
bannerLink: typedSettings?.bannerLink || null,
|
|
||||||
bannerActionText: typedSettings?.bannerActionText || "立即查看",
|
|
||||||
lovartPromoText:
|
lovartPromoText:
|
||||||
typedSettings?.lovartPromoText ||
|
typedSettings?.lovartPromoText ||
|
||||||
"现在升级,立享 Kling 3.0 与 Nano Banana Pro,最高 5 折优惠",
|
"现在升级,立享 Kling 3.0 与 Nano Banana Pro,最高 5 折优惠",
|
||||||
lovartPromoActionText: typedSettings?.lovartPromoActionText || "立即升级",
|
lovartPromoActionText: typedSettings?.lovartPromoActionText || "立即升级",
|
||||||
lovartHeroTitle: typedSettings?.lovartHeroTitle || "𝗕𝘂𝗬𝗶 让设计触手可及",
|
lovartHeroTitle: typedSettings?.lovartHeroTitle || "由壹点AI 让设计更简单",
|
||||||
lovartHeroSubtitle:
|
lovartHeroSubtitle:
|
||||||
typedSettings?.lovartHeroSubtitle || "下达指令,即刻呈现",
|
typedSettings?.lovartHeroSubtitle || "懂你的设计代理,帮你搞定一切",
|
||||||
surpriseCategories: normalizeSurpriseCategories(
|
surpriseCategories: normalizeSurpriseCategories(
|
||||||
valueByKey.get(HOME_SURPRISE_CATEGORIES_KEY)
|
valueByKey.get(HOME_SURPRISE_CATEGORIES_KEY)
|
||||||
),
|
),
|
||||||
@ -1476,11 +1466,10 @@ export async function GET(request: NextRequest) {
|
|||||||
usageTimeEnabled: false,
|
usageTimeEnabled: false,
|
||||||
usageTimeStart: "08:00",
|
usageTimeStart: "08:00",
|
||||||
usageTimeEnd: "20:00",
|
usageTimeEnd: "20:00",
|
||||||
usageTimeMessage: "BuYi 累了也是需要休息的",
|
usageTimeMessage: "由壹点累了也是需要休息的",
|
||||||
bannerEnabled: true,
|
bannerEnabled: true,
|
||||||
bannerText: "限时特惠:升级会员最高立享 45% OFF!",
|
bannerText: "限时特惠:升级会员最高立享 45% OFF!",
|
||||||
bannerLink: null,
|
bannerLink: null,
|
||||||
bannerActionText: "立即查看",
|
|
||||||
toolbox1Name: "工具箱",
|
toolbox1Name: "工具箱",
|
||||||
toolbox1Enabled: true,
|
toolbox1Enabled: true,
|
||||||
toolbox2Name: "工具箱2",
|
toolbox2Name: "工具箱2",
|
||||||
@ -1525,8 +1514,8 @@ export async function GET(request: NextRequest) {
|
|||||||
canvasDetailAgentModelId: null,
|
canvasDetailAgentModelId: null,
|
||||||
lovartPromoText: "现在升级,立享 Kling 3.0 与 Nano Banana Pro,最高 5 折优惠",
|
lovartPromoText: "现在升级,立享 Kling 3.0 与 Nano Banana Pro,最高 5 折优惠",
|
||||||
lovartPromoActionText: "立即升级",
|
lovartPromoActionText: "立即升级",
|
||||||
lovartHeroTitle: "𝗕𝘂𝗬𝗶 让设计触手可及",
|
lovartHeroTitle: "由壹点AI 让设计更简单",
|
||||||
lovartHeroSubtitle: "下达指令,即刻呈现",
|
lovartHeroSubtitle: "懂你的设计代理,帮你搞定一切",
|
||||||
surpriseCategories,
|
surpriseCategories,
|
||||||
aiDetailGenerationEnabled,
|
aiDetailGenerationEnabled,
|
||||||
lovartComposerPrefix,
|
lovartComposerPrefix,
|
||||||
@ -1546,8 +1535,8 @@ export async function GET(request: NextRequest) {
|
|||||||
baseSettings.lovartPromoText ||
|
baseSettings.lovartPromoText ||
|
||||||
"现在升级,立享 Kling 3.0 与 Nano Banana Pro,最高 5 折优惠",
|
"现在升级,立享 Kling 3.0 与 Nano Banana Pro,最高 5 折优惠",
|
||||||
lovartPromoActionText: baseSettings.lovartPromoActionText || "立即升级",
|
lovartPromoActionText: baseSettings.lovartPromoActionText || "立即升级",
|
||||||
lovartHeroTitle: baseSettings.lovartHeroTitle || "𝗕𝘂𝗬𝗶 让设计触手可及",
|
lovartHeroTitle: baseSettings.lovartHeroTitle || "由壹点AI 让设计更简单",
|
||||||
lovartHeroSubtitle: baseSettings.lovartHeroSubtitle || "下达指令,即刻呈现",
|
lovartHeroSubtitle: baseSettings.lovartHeroSubtitle || "懂你的设计代理,帮你搞定一切",
|
||||||
notification,
|
notification,
|
||||||
surpriseCategories,
|
surpriseCategories,
|
||||||
aiDetailGenerationEnabled,
|
aiDetailGenerationEnabled,
|
||||||
@ -1662,7 +1651,6 @@ const updateSchema = z.object({
|
|||||||
bannerEnabled: z.boolean().optional(),
|
bannerEnabled: z.boolean().optional(),
|
||||||
bannerText: z.string().optional(),
|
bannerText: z.string().optional(),
|
||||||
bannerLink: z.string().nullable().optional(),
|
bannerLink: z.string().nullable().optional(),
|
||||||
bannerActionText: z.string().max(32).optional(),
|
|
||||||
toolbox1Name: z.string().max(64).optional(),
|
toolbox1Name: z.string().max(64).optional(),
|
||||||
toolbox1Enabled: z.boolean().optional(),
|
toolbox1Enabled: z.boolean().optional(),
|
||||||
toolbox2Name: z.string().max(64).optional(),
|
toolbox2Name: z.string().max(64).optional(),
|
||||||
@ -3119,11 +3107,10 @@ export async function PUT(request: NextRequest) {
|
|||||||
usageTimeEnabled: siteSettingsPayload.usageTimeEnabled ?? false,
|
usageTimeEnabled: siteSettingsPayload.usageTimeEnabled ?? false,
|
||||||
usageTimeStart: siteSettingsPayload.usageTimeStart ?? "08:00",
|
usageTimeStart: siteSettingsPayload.usageTimeStart ?? "08:00",
|
||||||
usageTimeEnd: siteSettingsPayload.usageTimeEnd ?? "20:00",
|
usageTimeEnd: siteSettingsPayload.usageTimeEnd ?? "20:00",
|
||||||
usageTimeMessage: siteSettingsPayload.usageTimeMessage ?? "BuYi 累了也是需要休息的",
|
usageTimeMessage: siteSettingsPayload.usageTimeMessage ?? "由壹点累了也是需要休息的",
|
||||||
bannerEnabled: siteSettingsPayload.bannerEnabled ?? true,
|
bannerEnabled: siteSettingsPayload.bannerEnabled ?? true,
|
||||||
bannerText: siteSettingsPayload.bannerText ?? "限时特惠:升级会员最高立享 45% OFF!",
|
bannerText: siteSettingsPayload.bannerText ?? "限时特惠:升级会员最高立享 45% OFF!",
|
||||||
bannerLink: siteSettingsPayload.bannerLink,
|
bannerLink: siteSettingsPayload.bannerLink,
|
||||||
bannerActionText: siteSettingsPayload.bannerActionText ?? "立即查看",
|
|
||||||
toolbox1Name: siteSettingsPayload.toolbox1Name ?? "工具箱",
|
toolbox1Name: siteSettingsPayload.toolbox1Name ?? "工具箱",
|
||||||
toolbox1Enabled: siteSettingsPayload.toolbox1Enabled ?? true,
|
toolbox1Enabled: siteSettingsPayload.toolbox1Enabled ?? true,
|
||||||
toolbox2Name: siteSettingsPayload.toolbox2Name ?? "工具箱2",
|
toolbox2Name: siteSettingsPayload.toolbox2Name ?? "工具箱2",
|
||||||
@ -3144,8 +3131,8 @@ export async function PUT(request: NextRequest) {
|
|||||||
siteSettingsPayload.lovartPromoText ??
|
siteSettingsPayload.lovartPromoText ??
|
||||||
"现在升级,立享 Kling 3.0 与 Nano Banana Pro,最高 5 折优惠",
|
"现在升级,立享 Kling 3.0 与 Nano Banana Pro,最高 5 折优惠",
|
||||||
lovartPromoActionText: siteSettingsPayload.lovartPromoActionText ?? "立即升级",
|
lovartPromoActionText: siteSettingsPayload.lovartPromoActionText ?? "立即升级",
|
||||||
lovartHeroTitle: siteSettingsPayload.lovartHeroTitle ?? "𝗕𝘂𝗬𝗶 让设计触手可及",
|
lovartHeroTitle: siteSettingsPayload.lovartHeroTitle ?? "由壹点AI 让设计更简单",
|
||||||
lovartHeroSubtitle: siteSettingsPayload.lovartHeroSubtitle ?? "下达指令,即刻呈现",
|
lovartHeroSubtitle: siteSettingsPayload.lovartHeroSubtitle ?? "懂你的设计代理,帮你搞定一切",
|
||||||
updatedBy: session.user.id,
|
updatedBy: session.user.id,
|
||||||
})
|
})
|
||||||
.returning();
|
.returning();
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user