Use direct batch face edits and allow site-wide issue feedback
This commit is contained in:
parent
d79b0b8e49
commit
0d16c27178
@ -1,92 +1,12 @@
|
|||||||
import { after, NextResponse } from "next/server";
|
import { feedbackGet, feedbackPost } from "@/lib/canvas/feedback-route";
|
||||||
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";
|
|
||||||
|
|
||||||
export const runtime = "nodejs";
|
export const runtime = "nodejs";
|
||||||
export const dynamic = "force-dynamic";
|
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 }> }) {
|
export async function POST(request: Request, { params }: { params: Promise<{ id: string }> }) {
|
||||||
try {
|
return feedbackPost(request, (await params).id);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function GET(request: Request, { params }: { params: Promise<{ id: string }> }) {
|
export async function GET(request: Request, { params }: { params: Promise<{ id: string }> }) {
|
||||||
try {
|
return feedbackGet(request, (await params).id);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
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,24 +23,26 @@ export default async function CanvasFeedbackPage({ params }: { params: Promise<{
|
|||||||
["反馈人", `${reporter.name} · ${reporter.email} · ${reporter.department || "未登记部门"}`],
|
["反馈人", `${reporter.name} · ${reporter.email} · ${reporter.department || "未登记部门"}`],
|
||||||
["用户编号", reporter.id],
|
["用户编号", reporter.id],
|
||||||
["反馈时间", formatFeedbackTime(feedback.createdAt.toISOString())],
|
["反馈时间", formatFeedbackTime(feedback.createdAt.toISOString())],
|
||||||
["画布", `${project.title}(${project.id})`],
|
...(project.id ? [
|
||||||
["对话编号", client.chatId || "未打开对话"],
|
["画布", `${project.title}(${project.id})`],
|
||||||
["请求号", request?.requestId || "尚未获取"],
|
["对话编号", client.chatId || "未打开对话"],
|
||||||
["任务或消息编号", request?.id || "未关联"],
|
["请求号", request?.requestId || "尚未获取"],
|
||||||
["请求时间", request?.createdAt ? formatFeedbackTime(request.createdAt) : "未记录"],
|
["任务或消息编号", request?.id || "未关联"],
|
||||||
["状态与模型", `${request?.status || "页面问题"} / ${request?.model || "未记录"}`],
|
["请求时间", request?.createdAt ? formatFeedbackTime(request.createdAt) : "未记录"],
|
||||||
[
|
["状态与模型", `${request?.status || "页面问题"} / ${request?.model || "未记录"}`],
|
||||||
"处理耗时",
|
[
|
||||||
request?.durationMs != null
|
"处理耗时",
|
||||||
? `${(request.durationMs / 1000).toFixed(1)} 秒`
|
request?.durationMs != null
|
||||||
: "未记录或尚未结束",
|
? `${(request.durationMs / 1000).toFixed(1)} 秒`
|
||||||
],
|
: "未记录或尚未结束",
|
||||||
[
|
],
|
||||||
"资料来源",
|
[
|
||||||
request?.evidence === "browser"
|
"资料来源",
|
||||||
? "浏览器当前对话,服务端尚无对应消息记录"
|
request?.evidence === "browser"
|
||||||
: "服务端记录及浏览器当前页面",
|
? "浏览器当前对话,服务端尚无对应消息记录"
|
||||||
],
|
: "服务端记录及浏览器当前页面",
|
||||||
|
],
|
||||||
|
] : [["页面", project.title]]),
|
||||||
["页面版本", client.pageVersion],
|
["页面版本", client.pageVersion],
|
||||||
["浏览器", userAgent],
|
["浏览器", userAgent],
|
||||||
[
|
[
|
||||||
@ -50,7 +52,7 @@ export default async function CanvasFeedbackPage({ params }: { params: Promise<{
|
|||||||
];
|
];
|
||||||
return (
|
return (
|
||||||
<main className="mx-auto max-w-3xl space-y-6 px-6 py-8 text-foreground">
|
<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>
|
<p className="text-sm text-muted-foreground">{feedbackReceipt(feedback).message}</p>
|
||||||
<dl className="space-y-3">
|
<dl className="space-y-3">
|
||||||
{fields.map(([label, value]) => (
|
{fields.map(([label, value]) => (
|
||||||
@ -78,7 +80,7 @@ export default async function CanvasFeedbackPage({ params }: { params: Promise<{
|
|||||||
</pre>
|
</pre>
|
||||||
</section>
|
</section>
|
||||||
)}
|
)}
|
||||||
{client.nodes.length > 0 && (
|
{project.id && client.nodes.length > 0 && (
|
||||||
<details className="text-sm">
|
<details className="text-sm">
|
||||||
<summary className="cursor-pointer">相关画布节点</summary>
|
<summary className="cursor-pointer">相关画布节点</summary>
|
||||||
<pre className="mt-3 overflow-auto whitespace-pre-wrap break-all">
|
<pre className="mt-3 overflow-auto whitespace-pre-wrap break-all">
|
||||||
@ -86,25 +88,29 @@ export default async function CanvasFeedbackPage({ params }: { params: Promise<{
|
|||||||
</pre>
|
</pre>
|
||||||
</details>
|
</details>
|
||||||
)}
|
)}
|
||||||
<details className="text-sm">
|
{project.id && (
|
||||||
<summary className="cursor-pointer">当前对话上下文</summary>
|
<>
|
||||||
<div className="mt-4 space-y-4">
|
<details className="text-sm">
|
||||||
{client.messages.map((item) => (
|
<summary className="cursor-pointer">当前对话上下文</summary>
|
||||||
<div key={item.id} className="space-y-1">
|
<div className="mt-4 space-y-4">
|
||||||
<p className="text-muted-foreground">
|
{client.messages.map((item) => (
|
||||||
{item.role === "user" ? "用户" : "助手"} · {item.id}
|
<div key={item.id} className="space-y-1">
|
||||||
</p>
|
<p className="text-muted-foreground">
|
||||||
<p className="whitespace-pre-wrap break-words">{item.text}</p>
|
{item.role === "user" ? "用户" : "助手"} · {item.id}
|
||||||
|
</p>
|
||||||
|
<p className="whitespace-pre-wrap break-words">{item.text}</p>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
</div>
|
</div>
|
||||||
))}
|
</details>
|
||||||
</div>
|
<a
|
||||||
</details>
|
className="inline-block text-sm underline underline-offset-4"
|
||||||
<a
|
href={`/canvas/${project.id}`}
|
||||||
className="inline-block text-sm underline underline-offset-4"
|
>
|
||||||
href={`/canvas/${project.id}`}
|
打开相关画布
|
||||||
>
|
</a>
|
||||||
打开相关画布
|
</>
|
||||||
</a>
|
)}
|
||||||
</main>
|
</main>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,6 +3,7 @@ import { Toaster } from "sonner";
|
|||||||
import { ThemeProvider } from "@/components/theme-provider";
|
import { ThemeProvider } from "@/components/theme-provider";
|
||||||
import { FaviconUpdater } from "@/components/favicon-updater";
|
import { FaviconUpdater } from "@/components/favicon-updater";
|
||||||
import { ImageDeliveryOptimizer } from "@/components/image-delivery-optimizer";
|
import { ImageDeliveryOptimizer } from "@/components/image-delivery-optimizer";
|
||||||
|
import { PageFeedback } from "@/components/page-feedback";
|
||||||
import { AppUpdateNotice } from "@/components/app-update-notice";
|
import { AppUpdateNotice } from "@/components/app-update-notice";
|
||||||
import { PAGE_DOWNLOAD_ACTIVE_ATTRIBUTE } from "@/lib/client/page-refresh";
|
import { PAGE_DOWNLOAD_ACTIVE_ATTRIBUTE } from "@/lib/client/page-refresh";
|
||||||
import { getSiteSettings } from "@/lib/db/queries";
|
import { getSiteSettings } from "@/lib/db/queries";
|
||||||
@ -222,7 +223,10 @@ export default function RootLayout({
|
|||||||
>
|
>
|
||||||
<Toaster position="top-center" />
|
<Toaster position="top-center" />
|
||||||
<AppUpdateNotice />
|
<AppUpdateNotice />
|
||||||
<SessionProvider>{children}</SessionProvider>
|
<SessionProvider>
|
||||||
|
{children}
|
||||||
|
<PageFeedback />
|
||||||
|
</SessionProvider>
|
||||||
</ThemeProvider>
|
</ThemeProvider>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
@ -12,6 +12,7 @@ import {
|
|||||||
DialogTitle,
|
DialogTitle,
|
||||||
DialogTrigger,
|
DialogTrigger,
|
||||||
} from "@/components/ui/dialog";
|
} from "@/components/ui/dialog";
|
||||||
|
import { cn } from "@/lib/utils";
|
||||||
import { captureFeedbackChat } from "@/lib/canvas/feedback-client";
|
import { captureFeedbackChat } from "@/lib/canvas/feedback-client";
|
||||||
import { useCanvasStore } from "@/store/canvas";
|
import { useCanvasStore } from "@/store/canvas";
|
||||||
import {
|
import {
|
||||||
@ -27,7 +28,15 @@ const terminal = (value: string) => ["sent", "unread", "read", "failed"].include
|
|||||||
const fieldClass =
|
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";
|
"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 [open, setOpen] = useState(false);
|
||||||
const [context, setContext] = useState<FeedbackContext | null>(null);
|
const [context, setContext] = useState<FeedbackContext | null>(null);
|
||||||
const [selection, setSelection] = useState<string | 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 submission = useRef<Submission | null>(null);
|
||||||
const errors = useRef<string[]>([]);
|
const errors = useRef<string[]>([]);
|
||||||
const submitLock = useRef(false);
|
const submitLock = useRef(false);
|
||||||
const endpoint = `/api/canvas/${projectId}/feedback`;
|
const endpoint = projectId ? "/api/canvas/" + projectId + "/feedback" : "/api/feedback";
|
||||||
const storageKey = `canvas-feedback:${projectId}`;
|
const storageKey = projectId ? "canvas-feedback:" + projectId : "page-feedback:" + pagePath;
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const rememberError = (event: ErrorEvent) => {
|
const rememberError = (event: ErrorEvent) => {
|
||||||
@ -91,11 +100,12 @@ export function CanvasFeedbackDialog({ projectId }: { projectId: string }) {
|
|||||||
}
|
}
|
||||||
const canvas = useCanvasStore.getState();
|
const canvas = useCanvasStore.getState();
|
||||||
const selectedIds = new Set(canvas.selectedNodeIds);
|
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)))
|
.sort((a, b) => Number(selectedIds.has(b.id)) - Number(selectedIds.has(a.id)))
|
||||||
.slice(0, 20);
|
.slice(0, 20) : [];
|
||||||
const client: FeedbackClient = {
|
const client: FeedbackClient = {
|
||||||
...captureFeedbackChat(projectId),
|
...(projectId ? captureFeedbackChat(projectId) : { chatId: null, messages: [] }),
|
||||||
|
pagePath,
|
||||||
capturedAt: new Date().toISOString(),
|
capturedAt: new Date().toISOString(),
|
||||||
pageVersion: process.env.NEXT_PUBLIC_APP_RELEASE || "development",
|
pageVersion: process.env.NEXT_PUBLIC_APP_RELEASE || "development",
|
||||||
viewport: { width: window.innerWidth, height: window.innerHeight },
|
viewport: { width: window.innerWidth, height: window.innerHeight },
|
||||||
@ -138,7 +148,7 @@ export function CanvasFeedbackDialog({ projectId }: { projectId: string }) {
|
|||||||
if (!controller.signal.aborted) setLoading(false);
|
if (!controller.signal.aborted) setLoading(false);
|
||||||
});
|
});
|
||||||
return () => controller.abort();
|
return () => controller.abort();
|
||||||
}, [open, endpoint, projectId, receipt, storageKey]);
|
}, [open, endpoint, projectId, pagePath, receipt, storageKey]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!open || !receipt || terminal(receipt.status)) return;
|
if (!open || !receipt || terminal(receipt.status)) return;
|
||||||
@ -172,7 +182,7 @@ export function CanvasFeedbackDialog({ projectId }: { projectId: string }) {
|
|||||||
const send = async () => {
|
const send = async () => {
|
||||||
if (submitLock.current || receipt) return;
|
if (submitLock.current || receipt) return;
|
||||||
if (!submission.current) {
|
if (!submission.current) {
|
||||||
if (!captured.current || !context) return;
|
if (!captured.current || !context || (!projectId && !description.trim())) return;
|
||||||
submission.current = {
|
submission.current = {
|
||||||
action: "submit",
|
action: "submit",
|
||||||
id: crypto.randomUUID(),
|
id: crypto.randomUUID(),
|
||||||
@ -237,10 +247,13 @@ export function CanvasFeedbackDialog({ projectId }: { projectId: string }) {
|
|||||||
<DialogTrigger asChild>
|
<DialogTrigger asChild>
|
||||||
<button
|
<button
|
||||||
type="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="问题反馈"
|
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>
|
<span className="hidden sm:inline">问题反馈</span>
|
||||||
</button>
|
</button>
|
||||||
</DialogTrigger>
|
</DialogTrigger>
|
||||||
@ -251,7 +264,9 @@ export function CanvasFeedbackDialog({ projectId }: { projectId: string }) {
|
|||||||
<DialogHeader>
|
<DialogHeader>
|
||||||
<DialogTitle>问题反馈</DialogTitle>
|
<DialogTitle>问题反馈</DialogTitle>
|
||||||
<DialogDescription className="text-pretty">
|
<DialogDescription className="text-pretty">
|
||||||
自动附上当前画布、用户和相关请求资料。你可以补充问题现象,发送给魔礼红。
|
{projectId
|
||||||
|
? "自动附上当前画布、用户和相关请求资料。你可以补充问题现象,发送给魔礼红。"
|
||||||
|
: "自动附上当前页面和浏览器信息。请说明遇到的问题,发送给魔礼红。"}
|
||||||
</DialogDescription>
|
</DialogDescription>
|
||||||
</DialogHeader>
|
</DialogHeader>
|
||||||
{receipt ? (
|
{receipt ? (
|
||||||
@ -278,45 +293,48 @@ export function CanvasFeedbackDialog({ projectId }: { projectId: string }) {
|
|||||||
{context.reporter.name} · {context.project.title}
|
{context.reporter.name} · {context.project.title}
|
||||||
</p>
|
</p>
|
||||||
) : null}
|
) : null}
|
||||||
<div className="space-y-2">
|
{projectId && (
|
||||||
<label htmlFor="canvas-feedback-request" className="text-sm font-medium">
|
<div className="space-y-2">
|
||||||
关联请求
|
<label htmlFor="canvas-feedback-request" className="text-sm font-medium">
|
||||||
</label>
|
关联请求
|
||||||
<select
|
</label>
|
||||||
id="canvas-feedback-request"
|
<select
|
||||||
className={fieldClass}
|
id="canvas-feedback-request"
|
||||||
value={selection || ""}
|
className={fieldClass}
|
||||||
disabled={sealed || loading}
|
value={selection || ""}
|
||||||
onChange={(event) => setSelection(event.target.value || null)}
|
disabled={sealed || loading}
|
||||||
>
|
onChange={(event) => setSelection(event.target.value || null)}
|
||||||
<option value="">页面或画布问题,不关联具体请求</option>
|
>
|
||||||
{context?.requests.map((item) => (
|
<option value="">页面或画布问题,不关联具体请求</option>
|
||||||
<option key={item.key} value={item.key}>
|
{context?.requests.map((item) => (
|
||||||
{item.createdAt ? formatFeedbackTime(item.createdAt) : "时间未记录"} ·{" "}
|
<option key={item.key} value={item.key}>
|
||||||
{item.prompt.slice(0, 45)}
|
{item.createdAt ? formatFeedbackTime(item.createdAt) : "时间未记录"} ·{" "}
|
||||||
</option>
|
{item.prompt.slice(0, 45)}
|
||||||
))}
|
</option>
|
||||||
</select>
|
))}
|
||||||
{selected && (
|
</select>
|
||||||
<div className="space-y-2 rounded-lg bg-muted p-3 text-sm">
|
{selected && (
|
||||||
<p>
|
<div className="space-y-2 rounded-lg bg-muted p-3 text-sm">
|
||||||
请求号:
|
<p>
|
||||||
<span className="break-all">
|
请求号:
|
||||||
{selected.requestId || "尚未获取,将附上消息编号"}
|
<span className="break-all">
|
||||||
</span>
|
{selected.requestId || "尚未获取,将附上消息编号"}
|
||||||
</p>
|
</span>
|
||||||
<p className="max-h-28 overflow-y-auto whitespace-pre-wrap break-words text-muted-foreground">
|
</p>
|
||||||
{selected.prompt}
|
<p className="max-h-28 overflow-y-auto whitespace-pre-wrap break-words text-muted-foreground">
|
||||||
</p>
|
{selected.prompt}
|
||||||
{selected.error && (
|
</p>
|
||||||
<p className="whitespace-pre-wrap break-words">{selected.error}</p>
|
{selected.error && (
|
||||||
)}
|
<p className="whitespace-pre-wrap break-words">{selected.error}</p>
|
||||||
</div>
|
)}
|
||||||
)}
|
</div>
|
||||||
</div>
|
)}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
<div className="space-y-2">
|
<div className="space-y-2">
|
||||||
<label htmlFor="canvas-feedback-description" className="text-sm font-medium">
|
<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>
|
</label>
|
||||||
<textarea
|
<textarea
|
||||||
id="canvas-feedback-description"
|
id="canvas-feedback-description"
|
||||||
@ -329,9 +347,11 @@ export function CanvasFeedbackDialog({ projectId }: { projectId: string }) {
|
|||||||
placeholder="例如:点击预览后一直加载,等待约 30 秒仍无法查看。"
|
placeholder="例如:点击预览后一直加载,等待约 30 秒仍无法查看。"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<p className="text-xs text-muted-foreground">
|
{projectId && (
|
||||||
将附上相关请求内容、任务状态、页面版本及已记录的错误;本次反馈不会重新执行任务。
|
<p className="text-xs text-muted-foreground">
|
||||||
</p>
|
将附上相关请求内容、任务状态、页面版本及已记录的错误;本次反馈不会重新执行任务。
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
{error && (
|
{error && (
|
||||||
@ -341,7 +361,7 @@ export function CanvasFeedbackDialog({ projectId }: { projectId: string }) {
|
|||||||
)}
|
)}
|
||||||
<DialogFooter className="gap-2 sm:space-x-0">
|
<DialogFooter className="gap-2 sm:space-x-0">
|
||||||
<Button type="button" variant="outline" onClick={() => setOpen(false)}>
|
<Button type="button" variant="outline" onClick={() => setOpen(false)}>
|
||||||
{receipt ? "返回画布" : "取消"}
|
{receipt ? "关闭" : "取消"}
|
||||||
</Button>
|
</Button>
|
||||||
{receipt ? (
|
{receipt ? (
|
||||||
<Button type="button" variant="outline" onClick={startAnother}>
|
<Button type="button" variant="outline" onClick={startAnother}>
|
||||||
@ -351,7 +371,7 @@ export function CanvasFeedbackDialog({ projectId }: { projectId: string }) {
|
|||||||
<Button
|
<Button
|
||||||
type="button"
|
type="button"
|
||||||
onClick={() => void send()}
|
onClick={() => void send()}
|
||||||
disabled={sending || loading || (!context && !submission.current)}
|
disabled={sending || loading || (!submission.current && (!context || (!projectId && !description.trim())))}
|
||||||
>
|
>
|
||||||
{sending ? "正在发送…" : sealed ? "重试提交同一反馈" : "发送给魔礼红"}
|
{sending ? "正在发送…" : sealed ? "重试提交同一反馈" : "发送给魔礼红"}
|
||||||
</Button>
|
</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 />;
|
||||||
|
}
|
||||||
@ -19,28 +19,34 @@ export function parseBatchInstructionRegion(value: unknown): CanvasLocalEditComp
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function faceFeaturesRegion(points: unknown) {
|
const faceSubject = /人脸|脸部|脸型|五官|面容|发型|face|facial features|hairstyle/i;
|
||||||
if (!Array.isArray(points) || points.length !== 6 || !points.every(
|
const faceAction = /替换|换成|换为|置换|改成|改为|swap|replace|transfer|change/i;
|
||||||
point => typeof point === "number" && Number.isFinite(point) && point >= 0 && point <= 1000,
|
|
||||||
)) return null;
|
function requestsFaceEdit(instruction: string) {
|
||||||
const [leftX, leftY, rightX, rightY, mouthX, mouthY] = points;
|
return instruction.split(/[,,。;;!!??\n]+/).some((clause) => {
|
||||||
const span = Math.abs(rightX - leftX);
|
const editable = clause.split(/保持|保留|维持|不要|别|不得|禁止|避免|do not|don't|keep|preserve/i, 1)[0];
|
||||||
const eyeY = (leftY + rightY) / 2;
|
return /换(?:人?脸|五官|发型)|face[- ]swap/i.test(editable)
|
||||||
const eyeX = (leftX + rightX) / 2;
|
|| (faceSubject.test(editable) && faceAction.test(editable));
|
||||||
const height = mouthY - eyeY;
|
});
|
||||||
if (span < 10 || height < 10 || Math.abs(mouthX - eyeX) > span) return null;
|
|
||||||
const center = (2 * eyeX + mouthX) / 3;
|
|
||||||
return parseBatchInstructionRegion([
|
|
||||||
Math.max(0, center - span * 0.86),
|
|
||||||
Math.max(0, Math.min(leftY, rightY) - height * 0.53),
|
|
||||||
Math.min(1000, center + span * 0.86),
|
|
||||||
Math.min(1000, mouthY + height * 0.34),
|
|
||||||
]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function resizeEditRegion(region: CanvasLocalEditCompositeRegion, face: boolean) {
|
function directFaceEdit(request: CanvasGenerateImageRequest): CanvasGenerateImageRequest {
|
||||||
const xMargin = region.width * (face ? -0.08 : 0.11);
|
return {
|
||||||
const yMargin = region.height * (face ? -0.11 : 0.11);
|
...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 x = Math.max(0, region.x - xMargin);
|
||||||
const y = Math.max(0, region.y - yMargin);
|
const y = Math.max(0, region.y - yMargin);
|
||||||
return {
|
return {
|
||||||
@ -66,6 +72,7 @@ async function cropReference(url: string, region: CanvasLocalEditCompositeRegion
|
|||||||
export async function prepareBatchInstructionWithReference(request: CanvasGenerateImageRequest) {
|
export async function prepareBatchInstructionWithReference(request: CanvasGenerateImageRequest) {
|
||||||
const [base, reference] = request.referenceImages || [];
|
const [base, reference] = request.referenceImages || [];
|
||||||
if (!base || !reference) throw new Error("局部指令编辑缺少原图或共用参考图");
|
if (!base || !reference) throw new Error("局部指令编辑缺少原图或共用参考图");
|
||||||
|
if (requestsFaceEdit(request.displayPrompt || "")) return directFaceEdit(request);
|
||||||
|
|
||||||
let text: string;
|
let text: string;
|
||||||
try {
|
try {
|
||||||
@ -79,8 +86,7 @@ export async function prepareBatchInstructionWithReference(request: CanvasGenera
|
|||||||
"定位图片编辑目标。图1是要编辑的原图,图2只提供参照特征。",
|
"定位图片编辑目标。图1是要编辑的原图,图2只提供参照特征。",
|
||||||
"只输出 JSON,格式为 {\"scope\":\"local\",\"base_box\":[x1,y1,x2,y2],\"reference_box\":[x1,y1,x2,y2]}。",
|
"只输出 JSON,格式为 {\"scope\":\"local\",\"base_box\":[x1,y1,x2,y2],\"reference_box\":[x1,y1,x2,y2]}。",
|
||||||
"两个框分别使用各自图片的 0 至 999 归一化坐标。",
|
"两个框分别使用各自图片的 0 至 999 归一化坐标。",
|
||||||
"换脸或换五官时,将 scope 设为 face。两个框只框人脸内部的眉、眼、鼻、嘴,不包含头发、颈部、衣服和背景。",
|
"指令明确要求换脸、换五官、脸型或发型时,直接返回 {\"scope\":\"face\"}。只要求保留这些特征时,按实际编辑目标判断。",
|
||||||
"换脸时再返回 base_face 和 reference_face;每个数组分别为 [左眼中心x,左眼中心y,右眼中心x,右眼中心y,嘴中心x,嘴中心y]。",
|
|
||||||
"替换商品时,图1框住指定旧商品的完整外轮廓,包括扶手和腿。图2框住替换商品,勿框入其他人或物。",
|
"替换商品时,图1框住指定旧商品的完整外轮廓,包括扶手和腿。图2框住替换商品,勿框入其他人或物。",
|
||||||
"只有指令明确要求修改整张图的整体风格、色调或场景时,返回 {\"scope\":\"full\"}。",
|
"只有指令明确要求修改整张图的整体风格、色调或场景时,返回 {\"scope\":\"full\"}。",
|
||||||
"找不到明确目标,或多张脸、多件商品无法确定哪一个时,返回 {\"scope\":\"unresolved\"}。",
|
"找不到明确目标,或多张脸、多件商品无法确定哪一个时,返回 {\"scope\":\"unresolved\"}。",
|
||||||
@ -91,33 +97,31 @@ export async function prepareBatchInstructionWithReference(request: CanvasGenera
|
|||||||
throw new Error("暂时无法识别要编辑的区域,生图任务尚未提交,请稍后重试");
|
throw new Error("暂时无法识别要编辑的区域,生图任务尚未提交,请稍后重试");
|
||||||
}
|
}
|
||||||
|
|
||||||
let result: { scope?: string; base_box?: unknown; reference_box?: unknown; base_face?: unknown; reference_face?: unknown } = {};
|
let result: { scope?: string; base_box?: unknown; reference_box?: unknown } = {};
|
||||||
try {
|
try {
|
||||||
const match = text.match(/\{[\s\S]*\}/);
|
const match = text.match(/\{[\s\S]*\}/);
|
||||||
if (match) result = JSON.parse(match[0]) || {};
|
if (match) result = JSON.parse(match[0]) || {};
|
||||||
} catch { /* Invalid or ambiguous localization is reported below. */ }
|
} catch { /* Invalid or ambiguous localization is reported below. */ }
|
||||||
|
|
||||||
const requiresLocalTarget = /换脸|五官|脸部|面部|换商品|换产品|(?:商品|产品|主体).{0,8}替换|替换.{0,10}(?:商品|产品|主体|模特)|face swap|replace (?:the )?(?:face|product)/i.test(request.displayPrompt || "");
|
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;
|
if (result.scope === "full" && !requiresLocalTarget) return request;
|
||||||
const baseRegion = parseBatchInstructionRegion(result.base_box);
|
const baseRegion = parseBatchInstructionRegion(result.base_box);
|
||||||
const referenceRegion = parseBatchInstructionRegion(result.reference_box);
|
const referenceRegion = parseBatchInstructionRegion(result.reference_box);
|
||||||
if ((result.scope !== "local" && result.scope !== "face") || !baseRegion || !referenceRegion) {
|
if (result.scope !== "local" || !baseRegion || !referenceRegion) {
|
||||||
throw new Error("未能定位唯一替换目标。请在指令里描述位置或特征,再重试这张图片");
|
throw new Error("未能定位唯一替换目标。请在指令里描述位置或特征,再重试这张图片");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Alibaba can use the cropped bytes directly without writing a new reference object.
|
// Alibaba can use the cropped bytes directly without writing a new reference object.
|
||||||
const face = result.scope === "face" || /换脸|五官|face swap/i.test(request.displayPrompt || "");
|
const target = expandProductRegion(baseRegion);
|
||||||
const target = (face && faceFeaturesRegion(result.base_face)) || resizeEditRegion(baseRegion, face);
|
const cropped = await cropReference(reference, referenceRegion).catch(() => null);
|
||||||
const referenceCrop = face
|
|
||||||
? faceFeaturesRegion(result.reference_face) || resizeEditRegion(referenceRegion, true)
|
|
||||||
: referenceRegion;
|
|
||||||
const cropped = await cropReference(reference, referenceCrop).catch(() => null);
|
|
||||||
return {
|
return {
|
||||||
...request,
|
...request,
|
||||||
prompt: [
|
prompt: [
|
||||||
"这是局部编辑。以第一张图为唯一底图。图2只提供目标特征。",
|
"这是局部编辑。以第一张图为唯一底图。图2只提供目标特征。",
|
||||||
"把修改放回原目标的位置和大小范围内。保持原目标的中心、落地位置或五官定位,保留原图其他内容。",
|
"把修改放回原目标的位置和大小范围内。保持原目标的中心、落地位置或五官定位,保留原图其他内容。",
|
||||||
face ? "仅对齐图1的眉毛、眼睛、鼻子和嘴。保持两眼及嘴的位置,不改变头发、发际线、肤色和脸的位置。" : "完整移除原目标的轮廓、腿和残影,保持原物件的落地位置。",
|
"完整移除原目标的轮廓、腿和残影,保持原物件的落地位置。",
|
||||||
"禁止添加图1原本没有的人物、物件、文字或标记。只输出一张与图1原构图对齐的图片。",
|
"禁止添加图1原本没有的人物、物件、文字或标记。只输出一张与图1原构图对齐的图片。",
|
||||||
"指令:" + request.displayPrompt,
|
"指令:" + request.displayPrompt,
|
||||||
].join("\n"),
|
].join("\n"),
|
||||||
@ -127,10 +131,9 @@ export async function prepareBatchInstructionWithReference(request: CanvasGenera
|
|||||||
regions: [target],
|
regions: [target],
|
||||||
expandRegions: false,
|
expandRegions: false,
|
||||||
maskMode: "region",
|
maskMode: "region",
|
||||||
maskShape: face ? "ellipse" : undefined,
|
featherMinPx: 7,
|
||||||
featherMinPx: face ? 3 : 7,
|
featherMaxPx: 20,
|
||||||
featherMaxPx: face ? 10 : 20,
|
featherRatio: 0.012,
|
||||||
featherRatio: face ? 0.007 : 0.012,
|
|
||||||
},
|
},
|
||||||
} satisfies CanvasGenerateImageRequest;
|
} satisfies CanvasGenerateImageRequest;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -103,6 +103,27 @@ export function feedbackConversationRequests(
|
|||||||
return requests.filter((item) => item.prompt).reverse();
|
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(
|
export async function collectCanvasFeedbackContext(
|
||||||
userId: string,
|
userId: string,
|
||||||
projectId: string,
|
projectId: string,
|
||||||
@ -115,12 +136,7 @@ export async function collectCanvasFeedbackContext(
|
|||||||
.where(and(eq(canvasProject.id, projectId), eq(canvasProject.userId, userId)))
|
.where(and(eq(canvasProject.id, projectId), eq(canvasProject.userId, userId)))
|
||||||
.limit(1);
|
.limit(1);
|
||||||
if (!project) throw new CanvasFeedbackError("画布不存在或无权反馈该画布", 404);
|
if (!project) throw new CanvasFeedbackError("画布不存在或无权反馈该画布", 404);
|
||||||
const [reporter] = await db
|
const reporter = await collectFeedbackReporter(userId);
|
||||||
.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 [kind, selectedId] = (selection || "").split(":", 2);
|
const [kind, selectedId] = (selection || "").split(":", 2);
|
||||||
const validId = /^[0-9a-f-]{36}$/i.test(selectedId || "");
|
const validId = /^[0-9a-f-]{36}$/i.test(selectedId || "");
|
||||||
const selectedMessage =
|
const selectedMessage =
|
||||||
@ -303,5 +319,5 @@ export async function collectCanvasFeedbackContext(
|
|||||||
requests.sort((a, b) => (Date.parse(b.createdAt) || 0) - (Date.parse(a.createdAt) || 0));
|
requests.sort((a, b) => (Date.parse(b.createdAt) || 0) - (Date.parse(a.createdAt) || 0));
|
||||||
if (selection && !requests.some((item) => item.key === selection))
|
if (selection && !requests.some((item) => item.key === selection))
|
||||||
throw new CanvasFeedbackError("关联请求已变化,请重新选择要反馈的请求", 409);
|
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(),
|
error: z.string().max(2000).optional(),
|
||||||
userMessageId: z.string().max(180).optional(),
|
userMessageId: z.string().max(180).optional(),
|
||||||
});
|
});
|
||||||
|
export const feedbackPagePathSchema = z.string().max(2048).regex(/^\/(?!\/)[^?#\\\s]*$/);
|
||||||
|
|
||||||
export const feedbackClientSchema = z.object({
|
export const feedbackClientSchema = z.object({
|
||||||
|
pagePath: feedbackPagePathSchema.optional(),
|
||||||
chatId: z.string().uuid().nullable(),
|
chatId: z.string().uuid().nullable(),
|
||||||
messages: z.array(feedbackMessageSchema).max(12),
|
messages: z.array(feedbackMessageSchema).max(12),
|
||||||
pageVersion: z.string().max(100),
|
pageVersion: z.string().max(100),
|
||||||
@ -64,7 +67,7 @@ export type FeedbackRequest = {
|
|||||||
};
|
};
|
||||||
export type FeedbackContext = {
|
export type FeedbackContext = {
|
||||||
reporter: { id: string; name: string; email: string; department: string | null };
|
reporter: { id: string; name: string; email: string; department: string | null };
|
||||||
project: { id: string; title: string };
|
project: { id: string | null; title: string };
|
||||||
requests: FeedbackRequest[];
|
requests: FeedbackRequest[];
|
||||||
};
|
};
|
||||||
export type FeedbackSnapshot = Omit<FeedbackContext, "requests"> & {
|
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;
|
appUrl: string;
|
||||||
}): NotificationPayload {
|
}): NotificationPayload {
|
||||||
const { reporter, project, request } = input.snapshot;
|
const { reporter, project, request } = input.snapshot;
|
||||||
|
const location = project.id ? "**画布:** " + project.title : "**页面:** " + project.title;
|
||||||
const content = [
|
const content = [
|
||||||
`**反馈人:** ${reporter.name} · ${reporter.department || "未登记部门"}`,
|
`**反馈人:** ${reporter.name} · ${reporter.department || "未登记部门"}`,
|
||||||
`**反馈时间:** ${formatFeedbackTime(input.createdAt)}`,
|
`**反馈时间:** ${formatFeedbackTime(input.createdAt)}`,
|
||||||
`**画布:** ${project.title}`,
|
location,
|
||||||
`**请求时间:** ${request?.createdAt ? formatFeedbackTime(request.createdAt) : "未关联请求"}`,
|
`**请求时间:** ${request?.createdAt ? formatFeedbackTime(request.createdAt) : "未关联请求"}`,
|
||||||
`**请求号:** ${request?.requestId || "尚未获取;可通过消息或任务编号排查"}`,
|
`**请求号:** ${request?.requestId || "尚未获取;可通过消息或任务编号排查"}`,
|
||||||
`**状态 / 模型:** ${request?.status || "页面问题"} / ${request?.model || "未记录"}`,
|
`**状态 / 模型:** ${request?.status || "页面问题"} / ${request?.model || "未记录"}`,
|
||||||
@ -52,7 +53,7 @@ export function buildFeedbackNotification(input: {
|
|||||||
return {
|
return {
|
||||||
idempotencyKey: `canvas-feedback:${input.id}:v1`,
|
idempotencyKey: `canvas-feedback:${input.id}:v1`,
|
||||||
recipientAlias: "魔礼红",
|
recipientAlias: "魔礼红",
|
||||||
title: "画布问题反馈",
|
title: project.id ? "画布问题反馈" : "页面问题反馈",
|
||||||
content: clip(content, 4400),
|
content: clip(content, 4400),
|
||||||
actionTitle: "查看完整反馈",
|
actionTitle: "查看完整反馈",
|
||||||
actionUrl: `${input.appUrl.replace(/\/$/, "")}/feedback/${input.id}`,
|
actionUrl: `${input.appUrl.replace(/\/$/, "")}/feedback/${input.id}`,
|
||||||
|
|||||||
@ -188,6 +188,7 @@ export interface CanvasGenerateImageRequest {
|
|||||||
requiredReferenceImageCount?: number;
|
requiredReferenceImageCount?: number;
|
||||||
padBatchInstructionToSquare?: boolean;
|
padBatchInstructionToSquare?: boolean;
|
||||||
batchInstructionWithReference?: boolean;
|
batchInstructionWithReference?: boolean;
|
||||||
|
batchInstructionFaceEdit?: boolean;
|
||||||
ownerKey?: string;
|
ownerKey?: string;
|
||||||
debugLabel?: string;
|
debugLabel?: string;
|
||||||
debugMeta?: Record<string, unknown>;
|
debugMeta?: Record<string, unknown>;
|
||||||
@ -4275,6 +4276,7 @@ function isCanvasScaleAdjustProviderMaskEditModel(item: CanvasModelWithProvider)
|
|||||||
|
|
||||||
interface CanvasImageGenConfig {
|
interface CanvasImageGenConfig {
|
||||||
maxImages?: number;
|
maxImages?: number;
|
||||||
|
maxReferenceImages?: number;
|
||||||
supports1K?: boolean;
|
supports1K?: boolean;
|
||||||
supports2K?: boolean;
|
supports2K?: boolean;
|
||||||
supports4K?: boolean;
|
supports4K?: boolean;
|
||||||
@ -4924,6 +4926,14 @@ export async function generateCanvasImage(
|
|||||||
const allModels = await getEnabledAiModelsWithProvider();
|
const allModels = await getEnabledAiModelsWithProvider();
|
||||||
const balanceBlockedProviderIds = new Set(request.internalBalanceBlockedProviderIds || []);
|
const balanceBlockedProviderIds = new Set(request.internalBalanceBlockedProviderIds || []);
|
||||||
let imageModels = allModels.filter((m) => m.model.supportsImageGen && !isImageModelBalanceBlocked(m.model) && !balanceBlockedProviderIds.has(m.provider.id));
|
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 ||
|
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);
|
(/^[0-9a-f-]{36}$/i.test(String(model || "")) ? await getAiModelById({ id: model! }) : undefined);
|
||||||
let effectiveModel = model;
|
let effectiveModel = model;
|
||||||
@ -4944,12 +4954,19 @@ export async function generateCanvasImage(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!imageModels || imageModels.length === 0) {
|
if (!imageModels || imageModels.length === 0) {
|
||||||
|
if (request.batchInstructionFaceEdit) {
|
||||||
|
throw new AgentImageGenerationError("当前没有支持两张参考图的生图线路,换脸任务尚未提交。", true);
|
||||||
|
}
|
||||||
throw new Error("暂无可用的图片生成模型");
|
throw new Error("暂无可用的图片生成模型");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const verifiedFaceModel = request.batchInstructionFaceEdit
|
||||||
|
? imageModels.find(({ model, provider }) =>
|
||||||
|
model.modelId === "nano-banana-2.1" && provider.type === "vip98k")
|
||||||
|
: undefined;
|
||||||
let selectedModel = effectiveModel
|
let selectedModel = effectiveModel
|
||||||
? imageModels.find((m) => m.model.modelId === effectiveModel || m.model.id === 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 (!selectedModel) {
|
||||||
if (model && (model !== "auto" || request.agentImageMode === "pinned") && request.allowFailover === false) throw new Error("指定的图片模型不可用,未切换到其他模型");
|
if (model && (model !== "auto" || request.agentImageMode === "pinned") && request.allowFailover === false) throw new Error("指定的图片模型不可用,未切换到其他模型");
|
||||||
|
|||||||
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,
|
"when": 1794960000001,
|
||||||
"tag": "0106_canvas_feedback_utc_defaults",
|
"tag": "0106_canvas_feedback_utc_defaults",
|
||||||
"breakpoints": true
|
"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", {
|
export const canvasFeedback = pgTable("CanvasFeedback", {
|
||||||
id: uuid("id").primaryKey().notNull(),
|
id: uuid("id").primaryKey().notNull(),
|
||||||
userId: uuid("userId").notNull().references(() => user.id, { onDelete: "cascade" }),
|
userId: uuid("userId").notNull().references(() => user.id, { onDelete: "cascade" }),
|
||||||
projectId: uuid("projectId").notNull(),
|
projectId: uuid("projectId"),
|
||||||
description: text("description").notNull(),
|
description: text("description").notNull(),
|
||||||
snapshot: jsonb("snapshot").$type<import("@/lib/canvas/feedback-contract").FeedbackSnapshot>().notNull(),
|
snapshot: jsonb("snapshot").$type<import("@/lib/canvas/feedback-contract").FeedbackSnapshot>().notNull(),
|
||||||
notification: jsonb("notification").$type<import("@/lib/notifications/notification-center").NotificationPayload>().notNull(),
|
notification: jsonb("notification").$type<import("@/lib/notifications/notification-center").NotificationPayload>().notNull(),
|
||||||
|
|||||||
@ -59,13 +59,37 @@ test("instruction requests preserve base and shared reference roles and task ide
|
|||||||
expect(request?.prompt).toContain("只换五官");
|
expect(request?.prompt).toContain("只换五官");
|
||||||
});
|
});
|
||||||
|
|
||||||
test("shared face edits are confined to the source region and crop the reference", async () => {
|
test("shared face edits use the two full images and follow requested hair changes", async () => {
|
||||||
const reference = "https://media.example/face.png";
|
const reference = "https://media.example/face.png";
|
||||||
const base = "https://media.example/person.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 photo = await sharp({ create: { width: 500, height: 500, channels: 3, background: "white" } }).png().toBuffer();
|
||||||
const { prepareBatchInstructionWithReference } = serverLoader({
|
const { prepareBatchInstructionWithReference } = serverLoader({
|
||||||
"@/lib/ai/qwen3-vl": { callQwen3Vl: async () => ({
|
"@/lib/ai/qwen3-vl": { callQwen3Vl: async () => ({
|
||||||
text: '{"scope":"face","base_box":[300,110,690,490],"reference_box":[350,260,650,620],"base_face":[420,220,580,220,500,340],"reference_face":[430,350,570,350,500,500]}',
|
text: '{"scope":"local","base_box":[300,110,690,490],"reference_box":[350,260,650,620]}',
|
||||||
}) },
|
}) },
|
||||||
"@/lib/ai/rmbg/service": { fetchImageBufferFromUrl: async (url: string) => {
|
"@/lib/ai/rmbg/service": { fetchImageBufferFromUrl: async (url: string) => {
|
||||||
expect(url).toBe(reference);
|
expect(url).toBe(reference);
|
||||||
@ -73,12 +97,13 @@ test("shared face edits are confined to the source region and crop the reference
|
|||||||
} },
|
} },
|
||||||
})("lib/canvas/batch-instruction-reference.ts");
|
})("lib/canvas/batch-instruction-reference.ts");
|
||||||
|
|
||||||
const prepared = await prepareBatchInstructionWithReference({ displayPrompt: "只换五官", referenceImages: [base, reference] });
|
const prepared = await prepareBatchInstructionWithReference({
|
||||||
expect(prepared.localEditComposite).toMatchObject({
|
displayPrompt: "只换商品,保留原脸和发型,不要换脸",
|
||||||
sourceImageUrl: base, expandRegions: false, maskShape: "ellipse",
|
referenceImages: [base, reference],
|
||||||
});
|
});
|
||||||
|
|
||||||
|
expect(prepared.batchInstructionFaceEdit).toBeUndefined();
|
||||||
|
expect(prepared.localEditComposite).toMatchObject({ sourceImageUrl: base, expandRegions: false });
|
||||||
const cropped = await sharp(prepared.referenceImageBuffers.get(reference)).metadata();
|
const cropped = await sharp(prepared.referenceImageBuffers.get(reference)).metadata();
|
||||||
expect([cropped.width, cropped.height]).toEqual([121, 141]);
|
expect([cropped.width, cropped.height]).toEqual([150, 180]);
|
||||||
expect(prepared.localEditComposite.regions[0].x).toBeGreaterThan(0.3);
|
|
||||||
expect(prepared.localEditComposite.regions[0].y).toBeGreaterThan(0.11);
|
|
||||||
});
|
});
|
||||||
|
|||||||
@ -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).not.toHaveProperty("cookies");
|
||||||
expect(result.client.nodes).toEqual([]);
|
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