Compare commits
7 Commits
codex/batc
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| f670158409 | |||
| 0d16c27178 | |||
| d79b0b8e49 | |||
| 171e5d6a49 | |||
| c0054cfa7c | |||
| 4fb829a417 | |||
| 6ff8a0c43d |
@ -1,92 +1,12 @@
|
||||
import { after, NextResponse } from "next/server";
|
||||
import { z, ZodError } from "zod";
|
||||
import { auth } from "@/app/(auth)/auth";
|
||||
import { CanvasFeedbackError, collectCanvasFeedbackContext } from "@/lib/canvas/feedback-context";
|
||||
import { feedbackCommandSchema } from "@/lib/canvas/feedback-contract";
|
||||
import {
|
||||
deliverCanvasFeedback,
|
||||
feedbackReceipt,
|
||||
findCanvasFeedback,
|
||||
refreshCanvasFeedback,
|
||||
saveCanvasFeedback,
|
||||
} from "@/lib/canvas/feedback-store";
|
||||
import { feedbackGet, feedbackPost } from "@/lib/canvas/feedback-route";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
export const dynamic = "force-dynamic";
|
||||
const response = (data: unknown, status = 200) =>
|
||||
NextResponse.json(data, { status, headers: { "Cache-Control": "no-store" } });
|
||||
function failure(error: unknown) {
|
||||
if (error instanceof CanvasFeedbackError) return response({ error: error.message }, error.status);
|
||||
if (error instanceof ZodError || error instanceof SyntaxError)
|
||||
return response({ error: "反馈资料格式不正确,请重新打开问题反馈后重试。" }, 400);
|
||||
console.error("[Canvas feedback] request failed", {
|
||||
errorType: error instanceof Error ? error.name : "unknown",
|
||||
});
|
||||
return response({ error: "反馈暂未保存成功,请保留问题说明,稍后重试。" }, 503);
|
||||
}
|
||||
|
||||
export async function POST(request: Request, { params }: { params: Promise<{ id: string }> }) {
|
||||
try {
|
||||
const session = await auth();
|
||||
if (!session?.user?.id) return response({ error: "登录已过期,请登录后发送反馈。" }, 401);
|
||||
const projectId = z
|
||||
.string()
|
||||
.uuid()
|
||||
.parse((await params).id);
|
||||
const raw = await request.text();
|
||||
if (new TextEncoder().encode(raw).byteLength > 262144)
|
||||
return response({ error: "反馈资料过大,请减少补充说明后重试。" }, 413);
|
||||
const command = feedbackCommandSchema.parse(JSON.parse(raw));
|
||||
if (command.action === "submit") {
|
||||
const existing = await findCanvasFeedback(command.id);
|
||||
if (existing) {
|
||||
if (existing.userId !== session.user.id || existing.projectId !== projectId)
|
||||
throw new CanvasFeedbackError("反馈编号不可用", 409);
|
||||
return response({ feedback: feedbackReceipt(await refreshCanvasFeedback(existing)) });
|
||||
}
|
||||
}
|
||||
const context = await collectCanvasFeedbackContext(
|
||||
session.user.id,
|
||||
projectId,
|
||||
command.client,
|
||||
command.action === "submit" ? command.selection : null,
|
||||
);
|
||||
if (command.action === "context") return response({ context });
|
||||
const selected = context.requests.find((item) => item.key === command.selection) || null;
|
||||
const row = await saveCanvasFeedback({
|
||||
id: command.id,
|
||||
userId: session.user.id,
|
||||
description: command.description,
|
||||
appUrl: process.env.NEXT_PUBLIC_APP_URL || new URL(request.url).origin,
|
||||
snapshot: {
|
||||
reporter: context.reporter,
|
||||
project: context.project,
|
||||
request: selected,
|
||||
client: command.client,
|
||||
userAgent: (request.headers.get("user-agent") || "").slice(0, 512),
|
||||
},
|
||||
});
|
||||
after(() => deliverCanvasFeedback(row.id));
|
||||
return response({ feedback: feedbackReceipt(row) }, 202);
|
||||
} catch (error) {
|
||||
return failure(error);
|
||||
}
|
||||
return feedbackPost(request, (await params).id);
|
||||
}
|
||||
|
||||
export async function GET(request: Request, { params }: { params: Promise<{ id: string }> }) {
|
||||
try {
|
||||
const session = await auth();
|
||||
if (!session?.user?.id) return response({ error: "登录已过期,请登录后查看反馈状态。" }, 401);
|
||||
const projectId = z
|
||||
.string()
|
||||
.uuid()
|
||||
.parse((await params).id);
|
||||
const id = z.string().uuid().parse(new URL(request.url).searchParams.get("feedbackId"));
|
||||
const row = await findCanvasFeedback(id);
|
||||
if (!row || row.userId !== session.user.id || row.projectId !== projectId)
|
||||
throw new CanvasFeedbackError("反馈记录不存在", 404);
|
||||
return response({ feedback: feedbackReceipt(await refreshCanvasFeedback(row)) });
|
||||
} catch (error) {
|
||||
return failure(error);
|
||||
}
|
||||
return feedbackGet(request, (await params).id);
|
||||
}
|
||||
|
||||
@ -54,6 +54,7 @@ export async function POST(request: Request) {
|
||||
agentImageMode: "auto",
|
||||
promptExtend: false,
|
||||
padBatchInstructionToSquare: square || undefined,
|
||||
batchInstructionWithReference: Boolean(reference) || undefined,
|
||||
debugLabel: "batch-instruction-edit",
|
||||
};
|
||||
const task = await createCanvasImageTask({
|
||||
|
||||
7
app/api/feedback/route.ts
Normal file
7
app/api/feedback/route.ts
Normal file
@ -0,0 +1,7 @@
|
||||
import { feedbackGet, feedbackPost } from "@/lib/canvas/feedback-route";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
export const POST = (request: Request) => feedbackPost(request, null);
|
||||
export const GET = (request: Request) => feedbackGet(request, null);
|
||||
@ -23,6 +23,7 @@ export default async function CanvasFeedbackPage({ params }: { params: Promise<{
|
||||
["反馈人", `${reporter.name} · ${reporter.email} · ${reporter.department || "未登记部门"}`],
|
||||
["用户编号", reporter.id],
|
||||
["反馈时间", formatFeedbackTime(feedback.createdAt.toISOString())],
|
||||
...(project.id ? [
|
||||
["画布", `${project.title}(${project.id})`],
|
||||
["对话编号", client.chatId || "未打开对话"],
|
||||
["请求号", request?.requestId || "尚未获取"],
|
||||
@ -41,6 +42,7 @@ export default async function CanvasFeedbackPage({ params }: { params: Promise<{
|
||||
? "浏览器当前对话,服务端尚无对应消息记录"
|
||||
: "服务端记录及浏览器当前页面",
|
||||
],
|
||||
] : [["页面", project.title]]),
|
||||
["页面版本", client.pageVersion],
|
||||
["浏览器", userAgent],
|
||||
[
|
||||
@ -50,7 +52,7 @@ export default async function CanvasFeedbackPage({ params }: { params: Promise<{
|
||||
];
|
||||
return (
|
||||
<main className="mx-auto max-w-3xl space-y-6 px-6 py-8 text-foreground">
|
||||
<h1 className="text-2xl font-semibold">画布问题反馈</h1>
|
||||
<h1 className="text-2xl font-semibold">问题反馈</h1>
|
||||
<p className="text-sm text-muted-foreground">{feedbackReceipt(feedback).message}</p>
|
||||
<dl className="space-y-3">
|
||||
{fields.map(([label, value]) => (
|
||||
@ -78,7 +80,7 @@ export default async function CanvasFeedbackPage({ params }: { params: Promise<{
|
||||
</pre>
|
||||
</section>
|
||||
)}
|
||||
{client.nodes.length > 0 && (
|
||||
{project.id && client.nodes.length > 0 && (
|
||||
<details className="text-sm">
|
||||
<summary className="cursor-pointer">相关画布节点</summary>
|
||||
<pre className="mt-3 overflow-auto whitespace-pre-wrap break-all">
|
||||
@ -86,6 +88,8 @@ export default async function CanvasFeedbackPage({ params }: { params: Promise<{
|
||||
</pre>
|
||||
</details>
|
||||
)}
|
||||
{project.id && (
|
||||
<>
|
||||
<details className="text-sm">
|
||||
<summary className="cursor-pointer">当前对话上下文</summary>
|
||||
<div className="mt-4 space-y-4">
|
||||
@ -105,6 +109,8 @@ export default async function CanvasFeedbackPage({ params }: { params: Promise<{
|
||||
>
|
||||
打开相关画布
|
||||
</a>
|
||||
</>
|
||||
)}
|
||||
</main>
|
||||
);
|
||||
}
|
||||
|
||||
@ -3,6 +3,7 @@ import { Toaster } from "sonner";
|
||||
import { ThemeProvider } from "@/components/theme-provider";
|
||||
import { FaviconUpdater } from "@/components/favicon-updater";
|
||||
import { ImageDeliveryOptimizer } from "@/components/image-delivery-optimizer";
|
||||
import { PageFeedback } from "@/components/page-feedback";
|
||||
import { AppUpdateNotice } from "@/components/app-update-notice";
|
||||
import { PAGE_DOWNLOAD_ACTIVE_ATTRIBUTE } from "@/lib/client/page-refresh";
|
||||
import { getSiteSettings } from "@/lib/db/queries";
|
||||
@ -222,7 +223,10 @@ export default function RootLayout({
|
||||
>
|
||||
<Toaster position="top-center" />
|
||||
<AppUpdateNotice />
|
||||
<SessionProvider>{children}</SessionProvider>
|
||||
<SessionProvider>
|
||||
{children}
|
||||
<PageFeedback />
|
||||
</SessionProvider>
|
||||
</ThemeProvider>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@ -12,6 +12,7 @@ import {
|
||||
DialogTitle,
|
||||
DialogTrigger,
|
||||
} from "@/components/ui/dialog";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { captureFeedbackChat } from "@/lib/canvas/feedback-client";
|
||||
import { useCanvasStore } from "@/store/canvas";
|
||||
import {
|
||||
@ -27,7 +28,15 @@ const terminal = (value: string) => ["sent", "unread", "read", "failed"].include
|
||||
const fieldClass =
|
||||
"w-full rounded-lg border border-input bg-background px-3 py-2 text-sm text-foreground focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring";
|
||||
|
||||
export function CanvasFeedbackDialog({ projectId }: { projectId: string }) {
|
||||
export function CanvasFeedbackDialog({
|
||||
projectId,
|
||||
pagePath,
|
||||
floating = false,
|
||||
}: {
|
||||
projectId?: string;
|
||||
pagePath?: string;
|
||||
floating?: boolean;
|
||||
}) {
|
||||
const [open, setOpen] = useState(false);
|
||||
const [context, setContext] = useState<FeedbackContext | null>(null);
|
||||
const [selection, setSelection] = useState<string | null>(null);
|
||||
@ -41,8 +50,8 @@ export function CanvasFeedbackDialog({ projectId }: { projectId: string }) {
|
||||
const submission = useRef<Submission | null>(null);
|
||||
const errors = useRef<string[]>([]);
|
||||
const submitLock = useRef(false);
|
||||
const endpoint = `/api/canvas/${projectId}/feedback`;
|
||||
const storageKey = `canvas-feedback:${projectId}`;
|
||||
const endpoint = projectId ? "/api/canvas/" + projectId + "/feedback" : "/api/feedback";
|
||||
const storageKey = projectId ? "canvas-feedback:" + projectId : "page-feedback:" + pagePath;
|
||||
|
||||
useEffect(() => {
|
||||
const rememberError = (event: ErrorEvent) => {
|
||||
@ -91,11 +100,12 @@ export function CanvasFeedbackDialog({ projectId }: { projectId: string }) {
|
||||
}
|
||||
const canvas = useCanvasStore.getState();
|
||||
const selectedIds = new Set(canvas.selectedNodeIds);
|
||||
const nodes = [...canvas.nodes]
|
||||
const nodes = projectId ? [...canvas.nodes]
|
||||
.sort((a, b) => Number(selectedIds.has(b.id)) - Number(selectedIds.has(a.id)))
|
||||
.slice(0, 20);
|
||||
.slice(0, 20) : [];
|
||||
const client: FeedbackClient = {
|
||||
...captureFeedbackChat(projectId),
|
||||
...(projectId ? captureFeedbackChat(projectId) : { chatId: null, messages: [] }),
|
||||
pagePath,
|
||||
capturedAt: new Date().toISOString(),
|
||||
pageVersion: process.env.NEXT_PUBLIC_APP_RELEASE || "development",
|
||||
viewport: { width: window.innerWidth, height: window.innerHeight },
|
||||
@ -138,7 +148,7 @@ export function CanvasFeedbackDialog({ projectId }: { projectId: string }) {
|
||||
if (!controller.signal.aborted) setLoading(false);
|
||||
});
|
||||
return () => controller.abort();
|
||||
}, [open, endpoint, projectId, receipt, storageKey]);
|
||||
}, [open, endpoint, projectId, pagePath, receipt, storageKey]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!open || !receipt || terminal(receipt.status)) return;
|
||||
@ -172,7 +182,7 @@ export function CanvasFeedbackDialog({ projectId }: { projectId: string }) {
|
||||
const send = async () => {
|
||||
if (submitLock.current || receipt) return;
|
||||
if (!submission.current) {
|
||||
if (!captured.current || !context) return;
|
||||
if (!captured.current || !context || (!projectId && !description.trim())) return;
|
||||
submission.current = {
|
||||
action: "submit",
|
||||
id: crypto.randomUUID(),
|
||||
@ -237,10 +247,13 @@ export function CanvasFeedbackDialog({ projectId }: { projectId: string }) {
|
||||
<DialogTrigger asChild>
|
||||
<button
|
||||
type="button"
|
||||
className="ml-2 inline-flex shrink-0 items-center gap-2 rounded-lg px-3 py-2 text-sm text-muted-foreground transition-colors hover:bg-muted hover:text-foreground focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring"
|
||||
className={cn(
|
||||
"inline-flex shrink-0 items-center gap-2 rounded-lg px-3 py-2 text-sm text-muted-foreground transition-colors hover:bg-muted hover:text-foreground focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring",
|
||||
floating ? "fixed bottom-20 right-4 z-40 min-h-10 border bg-background shadow-sm" : "ml-2",
|
||||
)}
|
||||
aria-label="问题反馈"
|
||||
>
|
||||
<Bug className="size-4" aria-hidden="true" />
|
||||
<Bug className="size-4 text-red-600 dark:text-red-400" aria-hidden="true" />
|
||||
<span className="hidden sm:inline">问题反馈</span>
|
||||
</button>
|
||||
</DialogTrigger>
|
||||
@ -251,7 +264,9 @@ export function CanvasFeedbackDialog({ projectId }: { projectId: string }) {
|
||||
<DialogHeader>
|
||||
<DialogTitle>问题反馈</DialogTitle>
|
||||
<DialogDescription className="text-pretty">
|
||||
自动附上当前画布、用户和相关请求资料。你可以补充问题现象,发送给魔礼红。
|
||||
{projectId
|
||||
? "自动附上当前画布、用户和相关请求资料。你可以补充问题现象,发送给魔礼红。"
|
||||
: "自动附上当前页面和浏览器信息。请说明遇到的问题,发送给魔礼红。"}
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
{receipt ? (
|
||||
@ -278,6 +293,7 @@ export function CanvasFeedbackDialog({ projectId }: { projectId: string }) {
|
||||
{context.reporter.name} · {context.project.title}
|
||||
</p>
|
||||
) : null}
|
||||
{projectId && (
|
||||
<div className="space-y-2">
|
||||
<label htmlFor="canvas-feedback-request" className="text-sm font-medium">
|
||||
关联请求
|
||||
@ -314,9 +330,11 @@ export function CanvasFeedbackDialog({ projectId }: { projectId: string }) {
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
<div className="space-y-2">
|
||||
<label htmlFor="canvas-feedback-description" className="text-sm font-medium">
|
||||
补充说明 <span className="font-normal text-muted-foreground">(选填)</span>
|
||||
{projectId ? "补充说明" : "问题说明"}{" "}
|
||||
{projectId && <span className="font-normal text-muted-foreground">(选填)</span>}
|
||||
</label>
|
||||
<textarea
|
||||
id="canvas-feedback-description"
|
||||
@ -329,9 +347,11 @@ export function CanvasFeedbackDialog({ projectId }: { projectId: string }) {
|
||||
placeholder="例如:点击预览后一直加载,等待约 30 秒仍无法查看。"
|
||||
/>
|
||||
</div>
|
||||
{projectId && (
|
||||
<p className="text-xs text-muted-foreground">
|
||||
将附上相关请求内容、任务状态、页面版本及已记录的错误;本次反馈不会重新执行任务。
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
{error && (
|
||||
@ -341,7 +361,7 @@ export function CanvasFeedbackDialog({ projectId }: { projectId: string }) {
|
||||
)}
|
||||
<DialogFooter className="gap-2 sm:space-x-0">
|
||||
<Button type="button" variant="outline" onClick={() => setOpen(false)}>
|
||||
{receipt ? "返回画布" : "取消"}
|
||||
{receipt ? "关闭" : "取消"}
|
||||
</Button>
|
||||
{receipt ? (
|
||||
<Button type="button" variant="outline" onClick={startAnother}>
|
||||
@ -351,7 +371,7 @@ export function CanvasFeedbackDialog({ projectId }: { projectId: string }) {
|
||||
<Button
|
||||
type="button"
|
||||
onClick={() => void send()}
|
||||
disabled={sending || loading || (!context && !submission.current)}
|
||||
disabled={sending || loading || (!submission.current && (!context || (!projectId && !description.trim())))}
|
||||
>
|
||||
{sending ? "正在发送…" : sealed ? "重试提交同一反馈" : "发送给魔礼红"}
|
||||
</Button>
|
||||
|
||||
21
components/page-feedback.tsx
Normal file
21
components/page-feedback.tsx
Normal file
@ -0,0 +1,21 @@
|
||||
"use client";
|
||||
|
||||
import dynamic from "next/dynamic";
|
||||
import { usePathname } from "next/navigation";
|
||||
import { useSession } from "next-auth/react";
|
||||
|
||||
const FeedbackDialog = dynamic(
|
||||
() => import("@/components/canvas/CanvasFeedbackDialog").then((module) => module.CanvasFeedbackDialog),
|
||||
{ ssr: false },
|
||||
);
|
||||
|
||||
export function PageFeedback() {
|
||||
const pathname = usePathname();
|
||||
const { data: session } = useSession();
|
||||
|
||||
if (!session?.user?.id || session.user.type === "guest" || !pathname) return null;
|
||||
if (/^\/canvas\/[0-9a-f-]{36}\/?$/i.test(pathname) || /^\/(?:feedback|login|register)(?:\/|$)/.test(pathname)) {
|
||||
return null;
|
||||
}
|
||||
return <FeedbackDialog key={pathname} pagePath={pathname} floating />;
|
||||
}
|
||||
139
lib/canvas/batch-instruction-reference.ts
Normal file
139
lib/canvas/batch-instruction-reference.ts
Normal file
@ -0,0 +1,139 @@
|
||||
import "server-only";
|
||||
|
||||
import sharp from "sharp";
|
||||
import { callQwen3Vl } from "@/lib/ai/qwen3-vl";
|
||||
import { fetchImageBufferFromUrl } from "@/lib/ai/rmbg/service";
|
||||
import type { CanvasGenerateImageRequest, CanvasLocalEditCompositeRegion } from "./generate-image";
|
||||
|
||||
export function parseBatchInstructionRegion(value: unknown): CanvasLocalEditCompositeRegion | null {
|
||||
if (!Array.isArray(value) || value.length !== 4 || !value.every(
|
||||
point => typeof point === "number" && Number.isFinite(point) && point >= 0 && point <= 1000,
|
||||
)) return null;
|
||||
|
||||
const [left, top, right, bottom] = value;
|
||||
if (right - left < 10 || bottom - top < 10) return null;
|
||||
return {
|
||||
x: left / 1000, y: top / 1000,
|
||||
width: (right - left) / 1000, height: (bottom - top) / 1000,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
const faceSubject = /人脸|脸部|脸型|五官|面容|发型|face|facial features|hairstyle/i;
|
||||
const faceAction = /替换|换成|换为|置换|改成|改为|swap|replace|transfer|change/i;
|
||||
|
||||
function requestsFaceEdit(instruction: string) {
|
||||
return instruction.split(/[,,。;;!!??\n]+/).some((clause) => {
|
||||
const editable = clause.split(/保持|保留|维持|不要|别|不得|禁止|避免|do not|don't|keep|preserve/i, 1)[0];
|
||||
return /换(?:人?脸|五官|发型)|face[- ]swap/i.test(editable)
|
||||
|| (faceSubject.test(editable) && faceAction.test(editable));
|
||||
});
|
||||
}
|
||||
|
||||
function directFaceEdit(request: CanvasGenerateImageRequest): CanvasGenerateImageRequest {
|
||||
return {
|
||||
...request,
|
||||
batchInstructionFaceEdit: true,
|
||||
requiredReferenceImageCount: 2,
|
||||
prompt: [
|
||||
"第一张是要编辑的完整底图。第二张只提供指令指定的面部或头发特征。",
|
||||
"只局部编辑底图人物头部。仅修改指令明确要求的五官、脸型、发型或发色,保留其余头部特征。匹配底图的头部位置、转头或低头角度、视线、遮挡、肤色及场景光线,脸部和颈部自然衔接。",
|
||||
"保留原始画幅、姿势、手、道具、服饰颜色与印花、背景物件、标牌、文字和非头部区域的原有纹理。避免裁切、全图重绘、锐化或改色。输出一张完整成品图。",
|
||||
"用户指令:" + request.displayPrompt,
|
||||
].join("\n"),
|
||||
};
|
||||
}
|
||||
|
||||
function expandProductRegion(region: CanvasLocalEditCompositeRegion) {
|
||||
const xMargin = region.width * 0.11;
|
||||
const yMargin = region.height * 0.11;
|
||||
const x = Math.max(0, region.x - xMargin);
|
||||
const y = Math.max(0, region.y - yMargin);
|
||||
return {
|
||||
x, y,
|
||||
width: Math.min(1, region.x + region.width + xMargin) - x,
|
||||
height: Math.min(1, region.y + region.height + yMargin) - y,
|
||||
};
|
||||
}
|
||||
|
||||
async function cropReference(url: string, region: CanvasLocalEditCompositeRegion) {
|
||||
const bytes = await fetchImageBufferFromUrl(url);
|
||||
const oriented = await sharp(bytes).autoOrient().png().toBuffer({ resolveWithObject: true });
|
||||
const { width, height } = oriented.info;
|
||||
const left = Math.floor(region.x * width);
|
||||
const top = Math.floor(region.y * height);
|
||||
return sharp(oriented.data).extract({
|
||||
left, top,
|
||||
width: Math.min(width - left, Math.ceil(region.width * width)),
|
||||
height: Math.min(height - top, Math.ceil(region.height * height)),
|
||||
}).resize({ width: 1280, height: 1280, fit: "inside", withoutEnlargement: true }).png().toBuffer();
|
||||
}
|
||||
|
||||
export async function prepareBatchInstructionWithReference(request: CanvasGenerateImageRequest) {
|
||||
const [base, reference] = request.referenceImages || [];
|
||||
if (!base || !reference) throw new Error("局部指令编辑缺少原图或共用参考图");
|
||||
if (requestsFaceEdit(request.displayPrompt || "")) return directFaceEdit(request);
|
||||
|
||||
let text: string;
|
||||
try {
|
||||
({ text } = await callQwen3Vl({
|
||||
imageUrls: [base, reference],
|
||||
temperature: 0,
|
||||
maxTokens: 280,
|
||||
timeoutMs: 25_000,
|
||||
retries: 0,
|
||||
prompt: [
|
||||
"定位图片编辑目标。图1是要编辑的原图,图2只提供参照特征。",
|
||||
"只输出 JSON,格式为 {\"scope\":\"local\",\"base_box\":[x1,y1,x2,y2],\"reference_box\":[x1,y1,x2,y2]}。",
|
||||
"两个框分别使用各自图片的 0 至 999 归一化坐标。",
|
||||
"指令明确要求换脸、换五官、脸型或发型时,直接返回 {\"scope\":\"face\"}。只要求保留这些特征时,按实际编辑目标判断。",
|
||||
"替换商品时,图1框住指定旧商品的完整外轮廓,包括扶手和腿。图2框住替换商品,勿框入其他人或物。",
|
||||
"只有指令明确要求修改整张图的整体风格、色调或场景时,返回 {\"scope\":\"full\"}。",
|
||||
"找不到明确目标,或多张脸、多件商品无法确定哪一个时,返回 {\"scope\":\"unresolved\"}。",
|
||||
"用户指令:" + request.displayPrompt,
|
||||
].join("\n"),
|
||||
}));
|
||||
} catch {
|
||||
throw new Error("暂时无法识别要编辑的区域,生图任务尚未提交,请稍后重试");
|
||||
}
|
||||
|
||||
let result: { scope?: string; base_box?: unknown; reference_box?: unknown } = {};
|
||||
try {
|
||||
const match = text.match(/\{[\s\S]*\}/);
|
||||
if (match) result = JSON.parse(match[0]) || {};
|
||||
} catch { /* Invalid or ambiguous localization is reported below. */ }
|
||||
|
||||
if (result.scope === "face") return directFaceEdit(request);
|
||||
|
||||
const requiresLocalTarget = /换商品|换产品|(?:商品|产品|主体).{0,8}替换|替换.{0,10}(?:商品|产品|主体|模特)|face swap|replace (?:the )?(?:face|product)/i.test(request.displayPrompt || "");
|
||||
if (result.scope === "full" && !requiresLocalTarget) return request;
|
||||
const baseRegion = parseBatchInstructionRegion(result.base_box);
|
||||
const referenceRegion = parseBatchInstructionRegion(result.reference_box);
|
||||
if (result.scope !== "local" || !baseRegion || !referenceRegion) {
|
||||
throw new Error("未能定位唯一替换目标。请在指令里描述位置或特征,再重试这张图片");
|
||||
}
|
||||
|
||||
// Alibaba can use the cropped bytes directly without writing a new reference object.
|
||||
const target = expandProductRegion(baseRegion);
|
||||
const cropped = await cropReference(reference, referenceRegion).catch(() => null);
|
||||
return {
|
||||
...request,
|
||||
prompt: [
|
||||
"这是局部编辑。以第一张图为唯一底图。图2只提供目标特征。",
|
||||
"把修改放回原目标的位置和大小范围内。保持原目标的中心、落地位置或五官定位,保留原图其他内容。",
|
||||
"完整移除原目标的轮廓、腿和残影,保持原物件的落地位置。",
|
||||
"禁止添加图1原本没有的人物、物件、文字或标记。只输出一张与图1原构图对齐的图片。",
|
||||
"指令:" + request.displayPrompt,
|
||||
].join("\n"),
|
||||
referenceImageBuffers: cropped ? new Map([[reference, cropped]]) : undefined,
|
||||
localEditComposite: {
|
||||
sourceImageUrl: base,
|
||||
regions: [target],
|
||||
expandRegions: false,
|
||||
maskMode: "region",
|
||||
featherMinPx: 7,
|
||||
featherMaxPx: 20,
|
||||
featherRatio: 0.012,
|
||||
},
|
||||
} satisfies CanvasGenerateImageRequest;
|
||||
}
|
||||
@ -103,6 +103,27 @@ export function feedbackConversationRequests(
|
||||
return requests.filter((item) => item.prompt).reverse();
|
||||
}
|
||||
|
||||
async function collectFeedbackReporter(userId: string): Promise<FeedbackContext["reporter"]> {
|
||||
const [reporter] = await db
|
||||
.select({ id: user.id, name: user.name, email: user.email, department: user.department })
|
||||
.from(user)
|
||||
.where(eq(user.id, userId))
|
||||
.limit(1);
|
||||
if (!reporter) throw new CanvasFeedbackError("登录状态已失效,请重新登录", 401);
|
||||
return { ...reporter, name: reporter.name || reporter.email };
|
||||
}
|
||||
|
||||
export async function collectPageFeedbackContext(
|
||||
userId: string,
|
||||
pagePath: string,
|
||||
): Promise<FeedbackContext> {
|
||||
return {
|
||||
reporter: await collectFeedbackReporter(userId),
|
||||
project: { id: null, title: pagePath },
|
||||
requests: [],
|
||||
};
|
||||
}
|
||||
|
||||
export async function collectCanvasFeedbackContext(
|
||||
userId: string,
|
||||
projectId: string,
|
||||
@ -115,12 +136,7 @@ export async function collectCanvasFeedbackContext(
|
||||
.where(and(eq(canvasProject.id, projectId), eq(canvasProject.userId, userId)))
|
||||
.limit(1);
|
||||
if (!project) throw new CanvasFeedbackError("画布不存在或无权反馈该画布", 404);
|
||||
const [reporter] = await db
|
||||
.select({ id: user.id, name: user.name, email: user.email, department: user.department })
|
||||
.from(user)
|
||||
.where(eq(user.id, userId))
|
||||
.limit(1);
|
||||
if (!reporter) throw new CanvasFeedbackError("登录状态已失效,请重新登录", 401);
|
||||
const reporter = await collectFeedbackReporter(userId);
|
||||
const [kind, selectedId] = (selection || "").split(":", 2);
|
||||
const validId = /^[0-9a-f-]{36}$/i.test(selectedId || "");
|
||||
const selectedMessage =
|
||||
@ -303,5 +319,5 @@ export async function collectCanvasFeedbackContext(
|
||||
requests.sort((a, b) => (Date.parse(b.createdAt) || 0) - (Date.parse(a.createdAt) || 0));
|
||||
if (selection && !requests.some((item) => item.key === selection))
|
||||
throw new CanvasFeedbackError("关联请求已变化,请重新选择要反馈的请求", 409);
|
||||
return { project, reporter: { ...reporter, name: reporter.name || reporter.email }, requests };
|
||||
return { project, reporter, requests };
|
||||
}
|
||||
|
||||
@ -10,7 +10,10 @@ export const feedbackMessageSchema = z.object({
|
||||
error: z.string().max(2000).optional(),
|
||||
userMessageId: z.string().max(180).optional(),
|
||||
});
|
||||
export const feedbackPagePathSchema = z.string().max(2048).regex(/^\/(?!\/)[^?#\\\s]*$/);
|
||||
|
||||
export const feedbackClientSchema = z.object({
|
||||
pagePath: feedbackPagePathSchema.optional(),
|
||||
chatId: z.string().uuid().nullable(),
|
||||
messages: z.array(feedbackMessageSchema).max(12),
|
||||
pageVersion: z.string().max(100),
|
||||
@ -64,7 +67,7 @@ export type FeedbackRequest = {
|
||||
};
|
||||
export type FeedbackContext = {
|
||||
reporter: { id: string; name: string; email: string; department: string | null };
|
||||
project: { id: string; title: string };
|
||||
project: { id: string | null; title: string };
|
||||
requests: FeedbackRequest[];
|
||||
};
|
||||
export type FeedbackSnapshot = Omit<FeedbackContext, "requests"> & {
|
||||
|
||||
89
lib/canvas/feedback-route.ts
Normal file
89
lib/canvas/feedback-route.ts
Normal file
@ -0,0 +1,89 @@
|
||||
import { after, NextResponse } from "next/server";
|
||||
import { z, ZodError } from "zod";
|
||||
import { auth } from "@/app/(auth)/auth";
|
||||
import { CanvasFeedbackError, collectCanvasFeedbackContext, collectPageFeedbackContext } from "@/lib/canvas/feedback-context";
|
||||
import { feedbackCommandSchema, feedbackPagePathSchema } from "@/lib/canvas/feedback-contract";
|
||||
import {
|
||||
deliverCanvasFeedback,
|
||||
feedbackReceipt,
|
||||
findCanvasFeedback,
|
||||
refreshCanvasFeedback,
|
||||
saveCanvasFeedback,
|
||||
} from "@/lib/canvas/feedback-store";
|
||||
|
||||
const response = (data: unknown, status = 200) =>
|
||||
NextResponse.json(data, { status, headers: { "Cache-Control": "no-store" } });
|
||||
function failure(error: unknown) {
|
||||
if (error instanceof CanvasFeedbackError) return response({ error: error.message }, error.status);
|
||||
if (error instanceof ZodError || error instanceof SyntaxError)
|
||||
return response({ error: "反馈资料格式不正确,请重新打开问题反馈后重试。" }, 400);
|
||||
console.error("[Canvas feedback] request failed", {
|
||||
errorType: error instanceof Error ? error.name : "unknown",
|
||||
});
|
||||
return response({ error: "反馈暂未保存成功,请保留问题说明,稍后重试。" }, 503);
|
||||
}
|
||||
|
||||
export async function feedbackPost(request: Request, projectId: string | null) {
|
||||
try {
|
||||
const session = await auth();
|
||||
if (!session?.user?.id || (projectId === null && session.user.type === "guest")) return response({ error: "登录已过期,请登录后发送反馈。" }, 401);
|
||||
if (projectId !== null) z.string().uuid().parse(projectId);
|
||||
const raw = await request.text();
|
||||
if (new TextEncoder().encode(raw).byteLength > 262144)
|
||||
return response({ error: "反馈资料过大,请减少补充说明后重试。" }, 413);
|
||||
const command = feedbackCommandSchema.parse(JSON.parse(raw));
|
||||
if (command.action === "submit") {
|
||||
const existing = await findCanvasFeedback(command.id);
|
||||
if (existing) {
|
||||
if (existing.userId !== session.user.id || existing.projectId !== projectId)
|
||||
throw new CanvasFeedbackError("反馈编号不可用", 409);
|
||||
return response({ feedback: feedbackReceipt(await refreshCanvasFeedback(existing)) });
|
||||
}
|
||||
}
|
||||
if (!projectId && command.action === "submit" && !command.description) {
|
||||
throw new CanvasFeedbackError("请填写遇到的问题。");
|
||||
}
|
||||
const context = projectId
|
||||
? await collectCanvasFeedbackContext(
|
||||
session.user.id,
|
||||
projectId,
|
||||
command.client,
|
||||
command.action === "submit" ? command.selection : null,
|
||||
)
|
||||
: await collectPageFeedbackContext(session.user.id, feedbackPagePathSchema.parse(command.client.pagePath));
|
||||
if (command.action === "context") return response({ context });
|
||||
const selected = context.requests.find((item) => item.key === command.selection) || null;
|
||||
const row = await saveCanvasFeedback({
|
||||
id: command.id,
|
||||
userId: session.user.id,
|
||||
description: command.description,
|
||||
appUrl: process.env.NEXT_PUBLIC_APP_URL || new URL(request.url).origin,
|
||||
snapshot: {
|
||||
reporter: context.reporter,
|
||||
project: context.project,
|
||||
request: selected,
|
||||
client: projectId ? command.client : { ...command.client, chatId: null, messages: [], nodes: [] },
|
||||
userAgent: (request.headers.get("user-agent") || "").slice(0, 512),
|
||||
},
|
||||
});
|
||||
after(() => deliverCanvasFeedback(row.id));
|
||||
return response({ feedback: feedbackReceipt(row) }, 202);
|
||||
} catch (error) {
|
||||
return failure(error);
|
||||
}
|
||||
}
|
||||
|
||||
export async function feedbackGet(request: Request, projectId: string | null) {
|
||||
try {
|
||||
const session = await auth();
|
||||
if (!session?.user?.id || (projectId === null && session.user.type === "guest")) return response({ error: "登录已过期,请登录后查看反馈状态。" }, 401);
|
||||
if (projectId !== null) z.string().uuid().parse(projectId);
|
||||
const id = z.string().uuid().parse(new URL(request.url).searchParams.get("feedbackId"));
|
||||
const row = await findCanvasFeedback(id);
|
||||
if (!row || row.userId !== session.user.id || row.projectId !== projectId)
|
||||
throw new CanvasFeedbackError("反馈记录不存在", 404);
|
||||
return response({ feedback: feedbackReceipt(await refreshCanvasFeedback(row)) });
|
||||
} catch (error) {
|
||||
return failure(error);
|
||||
}
|
||||
}
|
||||
@ -35,10 +35,11 @@ export function buildFeedbackNotification(input: {
|
||||
appUrl: string;
|
||||
}): NotificationPayload {
|
||||
const { reporter, project, request } = input.snapshot;
|
||||
const location = project.id ? "**画布:** " + project.title : "**页面:** " + project.title;
|
||||
const content = [
|
||||
`**反馈人:** ${reporter.name} · ${reporter.department || "未登记部门"}`,
|
||||
`**反馈时间:** ${formatFeedbackTime(input.createdAt)}`,
|
||||
`**画布:** ${project.title}`,
|
||||
location,
|
||||
`**请求时间:** ${request?.createdAt ? formatFeedbackTime(request.createdAt) : "未关联请求"}`,
|
||||
`**请求号:** ${request?.requestId || "尚未获取;可通过消息或任务编号排查"}`,
|
||||
`**状态 / 模型:** ${request?.status || "页面问题"} / ${request?.model || "未记录"}`,
|
||||
@ -52,7 +53,7 @@ export function buildFeedbackNotification(input: {
|
||||
return {
|
||||
idempotencyKey: `canvas-feedback:${input.id}:v1`,
|
||||
recipientAlias: "魔礼红",
|
||||
title: "画布问题反馈",
|
||||
title: project.id ? "画布问题反馈" : "页面问题反馈",
|
||||
content: clip(content, 4400),
|
||||
actionTitle: "查看完整反馈",
|
||||
actionUrl: `${input.appUrl.replace(/\/$/, "")}/feedback/${input.id}`,
|
||||
|
||||
@ -60,6 +60,7 @@ export interface CanvasLocalEditCompositeConfig {
|
||||
regions: CanvasLocalEditCompositeRegion[];
|
||||
expandRegions?: boolean;
|
||||
maskMode?: "region" | "diff";
|
||||
maskShape?: "ellipse";
|
||||
featherMinPx?: number;
|
||||
featherMaxPx?: number;
|
||||
featherRatio?: number;
|
||||
@ -186,6 +187,8 @@ export interface CanvasGenerateImageRequest {
|
||||
billingFeature?: "multi-view" | "repaint";
|
||||
requiredReferenceImageCount?: number;
|
||||
padBatchInstructionToSquare?: boolean;
|
||||
batchInstructionWithReference?: boolean;
|
||||
batchInstructionFaceEdit?: boolean;
|
||||
ownerKey?: string;
|
||||
debugLabel?: string;
|
||||
debugMeta?: Record<string, unknown>;
|
||||
@ -544,6 +547,7 @@ function normalizeLocalEditCompositeConfig(
|
||||
regions,
|
||||
expandRegions,
|
||||
maskMode,
|
||||
maskShape: value.maskShape === "ellipse" ? value.maskShape : undefined,
|
||||
diffHighThreshold: normalizeOptionalNumber(value.diffHighThreshold, {
|
||||
max: 128,
|
||||
min: 1,
|
||||
@ -2894,6 +2898,7 @@ function buildLocalEditCompositeMaskSvg(params: {
|
||||
height: number;
|
||||
regions: CanvasLocalEditCompositeRegion[];
|
||||
featherPx?: number;
|
||||
maskShape?: "ellipse";
|
||||
}) {
|
||||
const { width, height, regions } = params;
|
||||
const blur =
|
||||
@ -2921,12 +2926,14 @@ function buildLocalEditCompositeMaskSvg(params: {
|
||||
return { left, top, rectHeight, rectWidth };
|
||||
})
|
||||
.filter(({ rectHeight, rectWidth }) => rectWidth > 0 && rectHeight > 0);
|
||||
const shape = (left: number, top: number, w: number, h: number, radius: number) =>
|
||||
params.maskShape === "ellipse"
|
||||
? `<ellipse cx="${left + w / 2}" cy="${top + h / 2}" rx="${w / 2}" ry="${h / 2}" fill="#ffffff" />`
|
||||
: `<rect x="${left}" y="${top}" width="${w}" height="${h}" rx="${radius}" fill="#ffffff" />`;
|
||||
const softRects = rectEntries
|
||||
.map(
|
||||
({ left, top, rectHeight, rectWidth }) =>
|
||||
`<rect x="${left}" y="${top}" width="${rectWidth}" height="${rectHeight}" rx="${Math.round(
|
||||
blur * 0.9
|
||||
)}" fill="#ffffff" />`
|
||||
shape(left, top, rectWidth, rectHeight, Math.round(blur * 0.9))
|
||||
)
|
||||
.join("");
|
||||
const coreRects = rectEntries
|
||||
@ -2942,9 +2949,7 @@ function buildLocalEditCompositeMaskSvg(params: {
|
||||
const coreWidth = rectWidth - inset * 2;
|
||||
const coreHeight = rectHeight - inset * 2;
|
||||
if (coreWidth <= 0 || coreHeight <= 0) return "";
|
||||
return `<rect x="${left + inset}" y="${top + inset}" width="${coreWidth}" height="${coreHeight}" rx="${Math.round(
|
||||
blur * 0.45
|
||||
)}" fill="#ffffff" />`;
|
||||
return shape(left + inset, top + inset, coreWidth, coreHeight, Math.round(blur * 0.45));
|
||||
})
|
||||
.join("");
|
||||
|
||||
@ -3425,6 +3430,7 @@ async function applyLocalEditCompositeToImages(params: {
|
||||
input: buildLocalEditCompositeMaskSvg({
|
||||
featherPx,
|
||||
height: sourceHeight,
|
||||
maskShape: composite.maskShape,
|
||||
regions: composite.regions,
|
||||
width: sourceWidth,
|
||||
}),
|
||||
@ -4270,6 +4276,7 @@ function isCanvasScaleAdjustProviderMaskEditModel(item: CanvasModelWithProvider)
|
||||
|
||||
interface CanvasImageGenConfig {
|
||||
maxImages?: number;
|
||||
maxReferenceImages?: number;
|
||||
supports1K?: boolean;
|
||||
supports2K?: boolean;
|
||||
supports4K?: boolean;
|
||||
@ -4919,6 +4926,14 @@ export async function generateCanvasImage(
|
||||
const allModels = await getEnabledAiModelsWithProvider();
|
||||
const balanceBlockedProviderIds = new Set(request.internalBalanceBlockedProviderIds || []);
|
||||
let imageModels = allModels.filter((m) => m.model.supportsImageGen && !isImageModelBalanceBlocked(m.model) && !balanceBlockedProviderIds.has(m.provider.id));
|
||||
if (request.batchInstructionFaceEdit) {
|
||||
imageModels = imageModels.filter(({ model, provider }) =>
|
||||
SUPPORTED_CANVAS_PROVIDER_TYPES.has(provider.type) &&
|
||||
!model.excludeFromFailover &&
|
||||
!modelLacksReferenceImageInputSupport(model, provider) &&
|
||||
Number((model.imageGenConfig as CanvasImageGenConfig | null)?.maxReferenceImages ?? 0) >= 2
|
||||
);
|
||||
}
|
||||
const requestedModel = allModels.find(entry => entry.model.id === model || entry.model.modelId === model)?.model ||
|
||||
(/^[0-9a-f-]{36}$/i.test(String(model || "")) ? await getAiModelById({ id: model! }) : undefined);
|
||||
let effectiveModel = model;
|
||||
@ -4939,12 +4954,19 @@ export async function generateCanvasImage(
|
||||
}
|
||||
|
||||
if (!imageModels || imageModels.length === 0) {
|
||||
if (request.batchInstructionFaceEdit) {
|
||||
throw new AgentImageGenerationError("当前没有支持两张参考图的生图线路,换脸任务尚未提交。", true);
|
||||
}
|
||||
throw new Error("暂无可用的图片生成模型");
|
||||
}
|
||||
|
||||
const verifiedFaceModel = request.batchInstructionFaceEdit
|
||||
? imageModels.find(({ model, provider }) =>
|
||||
model.modelId === "nano-banana-2.1" && provider.type === "vip98k")
|
||||
: undefined;
|
||||
let selectedModel = effectiveModel
|
||||
? imageModels.find((m) => m.model.modelId === effectiveModel || m.model.id === effectiveModel)
|
||||
: imageModels.find((m) => m.model.isDefault) || imageModels[0];
|
||||
: verifiedFaceModel || imageModels.find((m) => m.model.isDefault) || imageModels[0];
|
||||
|
||||
if (!selectedModel) {
|
||||
if (model && (model !== "auto" || request.agentImageMode === "pinned") && request.allowFailover === false) throw new Error("指定的图片模型不可用,未切换到其他模型");
|
||||
|
||||
@ -403,8 +403,11 @@ async function runSingleTask(entry: PendingTaskEntry) {
|
||||
: (async () => {
|
||||
const imageRequest = request as CanvasGenerateImageRequest;
|
||||
const ownerKey = String(task.userId || entry.userId);
|
||||
const prepared = imageRequest.batchInstructionWithReference
|
||||
? await (await import("./batch-instruction-reference")).prepareBatchInstructionWithReference(imageRequest)
|
||||
: imageRequest;
|
||||
const initialResult = await generateCanvasImage({
|
||||
...imageRequest,
|
||||
...prepared,
|
||||
ownerKey,
|
||||
});
|
||||
if (imageRequest.padBatchInstructionToSquare) {
|
||||
|
||||
1
lib/db/migrations/0107_allow_page_feedback.sql
Normal file
1
lib/db/migrations/0107_allow_page_feedback.sql
Normal file
@ -0,0 +1 @@
|
||||
ALTER TABLE "CanvasFeedback" ALTER COLUMN "projectId" DROP NOT NULL;
|
||||
@ -652,6 +652,13 @@
|
||||
"when": 1794960000001,
|
||||
"tag": "0106_canvas_feedback_utc_defaults",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 91,
|
||||
"version": "7",
|
||||
"when": 1795132800000,
|
||||
"tag": "0107_allow_page_feedback",
|
||||
"breakpoints": true
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
@ -707,7 +707,7 @@ export type ClientPerformanceEvent = InferSelectModel<
|
||||
export const canvasFeedback = pgTable("CanvasFeedback", {
|
||||
id: uuid("id").primaryKey().notNull(),
|
||||
userId: uuid("userId").notNull().references(() => user.id, { onDelete: "cascade" }),
|
||||
projectId: uuid("projectId").notNull(),
|
||||
projectId: uuid("projectId"),
|
||||
description: text("description").notNull(),
|
||||
snapshot: jsonb("snapshot").$type<import("@/lib/canvas/feedback-contract").FeedbackSnapshot>().notNull(),
|
||||
notification: jsonb("notification").$type<import("@/lib/notifications/notification-center").NotificationPayload>().notNull(),
|
||||
|
||||
@ -58,3 +58,52 @@ test("instruction requests preserve base and shared reference roles and task ide
|
||||
expect(request?.prompt).toContain("第二张图只提供指令指定");
|
||||
expect(request?.prompt).toContain("只换五官");
|
||||
});
|
||||
|
||||
test("shared face edits use the two full images and follow requested hair changes", async () => {
|
||||
const reference = "https://media.example/face.png";
|
||||
const base = "https://media.example/person.png";
|
||||
const { prepareBatchInstructionWithReference } = serverLoader({
|
||||
"@/lib/ai/qwen3-vl": { callQwen3Vl: async () => { throw new Error("Explicit face intent should use direct editing"); } },
|
||||
"@/lib/ai/rmbg/service": {},
|
||||
})("lib/canvas/batch-instruction-reference.ts");
|
||||
|
||||
const prepared = await prepareBatchInstructionWithReference({
|
||||
displayPrompt: "将图里的人物的五官脸型发型换成参考图人物的五官脸型发型",
|
||||
referenceImages: [base, reference],
|
||||
});
|
||||
|
||||
expect(prepared).toMatchObject({
|
||||
referenceImages: [base, reference],
|
||||
requiredReferenceImageCount: 2,
|
||||
batchInstructionFaceEdit: true,
|
||||
});
|
||||
expect(prepared.localEditComposite).toBeUndefined();
|
||||
expect(prepared.referenceImageBuffers).toBeUndefined();
|
||||
expect(prepared.prompt).toContain("保留原始画幅");
|
||||
expect(prepared.prompt).toContain("将图里的人物的五官脸型发型换成参考图");
|
||||
});
|
||||
|
||||
test("a product replacement that preserves the face still uses local product editing", async () => {
|
||||
const reference = "https://media.example/product.png";
|
||||
const base = "https://media.example/person.png";
|
||||
const photo = await sharp({ create: { width: 500, height: 500, channels: 3, background: "white" } }).png().toBuffer();
|
||||
const { prepareBatchInstructionWithReference } = serverLoader({
|
||||
"@/lib/ai/qwen3-vl": { callQwen3Vl: async () => ({
|
||||
text: '{"scope":"local","base_box":[300,110,690,490],"reference_box":[350,260,650,620]}',
|
||||
}) },
|
||||
"@/lib/ai/rmbg/service": { fetchImageBufferFromUrl: async (url: string) => {
|
||||
expect(url).toBe(reference);
|
||||
return photo;
|
||||
} },
|
||||
})("lib/canvas/batch-instruction-reference.ts");
|
||||
|
||||
const prepared = await prepareBatchInstructionWithReference({
|
||||
displayPrompt: "只换商品,保留原脸和发型,不要换脸",
|
||||
referenceImages: [base, reference],
|
||||
});
|
||||
|
||||
expect(prepared.batchInstructionFaceEdit).toBeUndefined();
|
||||
expect(prepared.localEditComposite).toMatchObject({ sourceImageUrl: base, expandRegions: false });
|
||||
const cropped = await sharp(prepared.referenceImageBuffers.get(reference)).metadata();
|
||||
expect([cropped.width, cropped.height]).toEqual([150, 180]);
|
||||
});
|
||||
|
||||
@ -192,3 +192,36 @@ test("feedback input bounds and fixed contract exclude recipient and arbitrary b
|
||||
expect(result.client).not.toHaveProperty("cookies");
|
||||
expect(result.client.nodes).toEqual([]);
|
||||
});
|
||||
|
||||
test("site feedback keeps the current path without requiring a canvas", () => {
|
||||
const snapshot: FeedbackSnapshot = {
|
||||
reporter: { id: "reporter", name: "验收用户", email: "test@example.com", department: null },
|
||||
project: { id: null, title: "/video-canvas" },
|
||||
request: null,
|
||||
client: {
|
||||
pagePath: "/video-canvas",
|
||||
chatId: null,
|
||||
messages: [],
|
||||
pageVersion: "qa",
|
||||
capturedAt: "2026-11-20T07:00:00Z",
|
||||
viewport: { width: 1440, height: 900 },
|
||||
online: true,
|
||||
errors: [],
|
||||
nodes: [],
|
||||
},
|
||||
userAgent: "test browser",
|
||||
};
|
||||
const notification = buildFeedbackNotification({
|
||||
id: "feedback",
|
||||
description: "视频工具加载异常",
|
||||
snapshot,
|
||||
createdAt: "2026-11-20T07:00:00Z",
|
||||
appUrl: "https://ai.yydd.ai",
|
||||
});
|
||||
expect(notification.title).toBe("页面问题反馈");
|
||||
expect(notification.content).toContain("**页面:** /video-canvas");
|
||||
expect(notification.actionUrl).toBe("https://ai.yydd.ai/feedback/feedback");
|
||||
expect(feedbackCommandSchema.safeParse({
|
||||
action: "context", client: { ...snapshot.client, pagePath: "/video-canvas?token=private" },
|
||||
}).success).toBe(false);
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user