41 lines
1.2 KiB
TypeScript
41 lines
1.2 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import { z } from "zod";
|
|
import { auth } from "@/app/(auth)/auth";
|
|
import { getChatById } from "@/lib/db/queries";
|
|
import {
|
|
buildClawdCancelKey,
|
|
cancelClawdRequest,
|
|
} from "@/lib/openclaw-chat/cancel-registry";
|
|
|
|
const cancelSchema = z.object({
|
|
id: z.string().uuid(),
|
|
userMessageId: z.string().uuid(),
|
|
});
|
|
|
|
export async function POST(request: Request) {
|
|
const session = await auth().catch(() => null);
|
|
if (!session?.user?.id) {
|
|
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
|
}
|
|
|
|
const parsed = cancelSchema.safeParse(await request.json().catch(() => null));
|
|
if (!parsed.success) {
|
|
return NextResponse.json({ error: "请求参数不正确" }, { status: 400 });
|
|
}
|
|
|
|
const existingChat = await getChatById({ id: parsed.data.id }).catch(() => null);
|
|
if (existingChat && existingChat.userId !== session.user.id) {
|
|
return NextResponse.json({ error: "Forbidden" }, { status: 403 });
|
|
}
|
|
|
|
const active = cancelClawdRequest(
|
|
buildClawdCancelKey({
|
|
userId: session.user.id,
|
|
chatId: parsed.data.id,
|
|
userMessageId: parsed.data.userMessageId,
|
|
})
|
|
);
|
|
|
|
return NextResponse.json({ canceled: true, active });
|
|
}
|