102 lines
2.9 KiB
TypeScript
102 lines
2.9 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import { z } from "zod";
|
|
import { auth } from "@/app/(auth)/auth";
|
|
import { getChatById } from "@/lib/db/queries";
|
|
import {
|
|
OPENCLAW_AUTH_REQUIRED_CODE,
|
|
OPENCLAW_AUTH_REQUIRED_MESSAGE,
|
|
} from "@/lib/openclaw-chat/errors";
|
|
import { getPersistedClawdTask } from "@/lib/openclaw-chat/task-store";
|
|
|
|
const statusSchema = z.object({
|
|
id: z.string().uuid(),
|
|
userMessageId: z.string().uuid().optional(),
|
|
});
|
|
|
|
export const dynamic = "force-dynamic";
|
|
export const revalidate = 0;
|
|
|
|
export async function GET(request: Request) {
|
|
const session = await auth().catch(() => null);
|
|
if (!session?.user?.id) {
|
|
return NextResponse.json(
|
|
{
|
|
error: OPENCLAW_AUTH_REQUIRED_MESSAGE,
|
|
code: OPENCLAW_AUTH_REQUIRED_CODE,
|
|
},
|
|
{ status: 401 }
|
|
);
|
|
}
|
|
|
|
const url = new URL(request.url);
|
|
const parsed = statusSchema.safeParse({
|
|
id: url.searchParams.get("id"),
|
|
userMessageId: url.searchParams.get("userMessageId") || undefined,
|
|
});
|
|
if (!parsed.success) {
|
|
return NextResponse.json({ error: "请求参数不正确" }, { status: 400 });
|
|
}
|
|
|
|
const chat = await getChatById({ id: parsed.data.id }).catch(() => null);
|
|
if (!chat) {
|
|
return NextResponse.json({ task: null }, { status: 404 });
|
|
}
|
|
if (chat.userId !== session.user.id) {
|
|
return NextResponse.json({ error: "Forbidden" }, { status: 403 });
|
|
}
|
|
|
|
const task = await getPersistedClawdTask({
|
|
chatId: parsed.data.id,
|
|
userMessageId: parsed.data.userMessageId,
|
|
});
|
|
if (!task) {
|
|
return NextResponse.json(
|
|
{ task: null },
|
|
{ headers: { "Cache-Control": "no-store, max-age=0" } }
|
|
);
|
|
}
|
|
|
|
const userTextPart = Array.isArray(task.userMessage?.parts)
|
|
? task.userMessage.parts.find(
|
|
(part) =>
|
|
part &&
|
|
typeof part === "object" &&
|
|
!Array.isArray(part) &&
|
|
(part as { type?: unknown }).type === "text"
|
|
)
|
|
: null;
|
|
const userText =
|
|
userTextPart && typeof userTextPart === "object"
|
|
? String((userTextPart as { text?: unknown }).text || "")
|
|
: "";
|
|
|
|
return NextResponse.json(
|
|
{
|
|
task: {
|
|
chatId: parsed.data.id,
|
|
userMessageId: task.metadata.userMessageId,
|
|
assistantMessageId: task.metadata.assistantMessageId,
|
|
status: task.metadata.status,
|
|
processingHint: task.metadata.processingHint,
|
|
progressEvents: task.metadata.progressEvents,
|
|
skillTagId: task.metadata.skillTagId || null,
|
|
skillTagName: task.metadata.skillTagName || null,
|
|
requestId: task.metadata.requestId || null,
|
|
error: task.metadata.error || null,
|
|
createdAt: task.metadata.createdAt,
|
|
updatedAt: task.metadata.updatedAt,
|
|
userMessage: {
|
|
text: userText,
|
|
attachments: task.userMessage?.attachments || [],
|
|
},
|
|
},
|
|
},
|
|
{
|
|
headers: {
|
|
"Cache-Control": "no-store, max-age=0",
|
|
"Retry-After": "2",
|
|
},
|
|
}
|
|
);
|
|
}
|