Your Name 4b820f8ff4
Some checks failed
Lint / build (20) (push) Has been cancelled
Playwright Tests / test (push) Has been cancelled
Add canvas agent mode and management
2026-03-10 19:56:25 +08:00

155 lines
4.5 KiB
TypeScript

import { NextRequest, NextResponse } from "next/server";
import { z } from "zod";
import { auth } from "@/app/(auth)/auth";
import { ChatSDKError } from "@/lib/errors";
import {
deleteAgentPresetById,
getAccessibleAgentPresetById,
updateAgentPreset,
} from "@/lib/db/queries";
const slotSchema = z.object({
title: z.string().trim().max(128).optional(),
promptTemplate: z.string().trim().min(1).max(8000),
defaultParams: z.record(z.string(), z.any()).nullable().optional(),
requireUpload: z.boolean().optional(),
uploadLabel: z.string().trim().max(128).optional(),
referenceAssetIds: z.array(z.string().uuid()).max(12).optional(),
});
const updateAgentSchema = z
.object({
name: z.string().trim().min(1).max(128).optional(),
type: z.enum(["image", "video"]).optional(),
visibility: z.enum(["public", "private"]).optional(),
categoryId: z.string().uuid().nullable().optional(),
newCategoryName: z.string().trim().min(1).max(64).optional(),
description: z.string().trim().max(2000).nullable().optional(),
defaultModelId: z.string().uuid().optional(),
defaultParams: z.record(z.string(), z.any()).nullable().optional(),
avatarAssetId: z.string().uuid().nullable().optional(),
slots: z.array(slotSchema).min(1).max(20).optional(),
status: z.enum(["active", "disabled", "hidden", "blocked"]).optional(),
})
.superRefine((value, ctx) => {
if (value.categoryId && value.newCategoryName) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "categoryId 和 newCategoryName 不能同时传入",
path: ["newCategoryName"],
});
}
});
export async function GET(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
try {
const session = await auth();
if (!session?.user?.id) {
return NextResponse.json({ success: false, error: "未登录" }, { status: 401 });
}
const { id } = await params;
const preset = await getAccessibleAgentPresetById({
id,
userId: session.user.id,
});
if (!preset) {
return NextResponse.json({ success: false, error: "智能体不存在" }, { status: 404 });
}
return NextResponse.json({ success: true, data: preset });
} catch (error) {
if (error instanceof ChatSDKError) {
return error.toResponse();
}
return NextResponse.json(
{ success: false, error: "获取智能体失败" },
{ status: 500 }
);
}
}
export async function PUT(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
try {
const session = await auth();
if (!session?.user?.id) {
return NextResponse.json({ success: false, error: "未登录" }, { status: 401 });
}
const { id } = await params;
const body = await request.json();
const data = updateAgentSchema.parse(body);
const preset = await updateAgentPreset({
id,
userId: session.user.id,
name: data.name,
type: data.type,
visibility: data.visibility,
categoryId: data.categoryId ?? undefined,
newCategoryName: data.newCategoryName,
description: data.description ?? undefined,
defaultModelId: data.defaultModelId,
defaultParams: data.defaultParams ?? undefined,
avatarAssetId: data.avatarAssetId ?? undefined,
slots: data.slots,
status: data.status,
});
return NextResponse.json({ success: true, data: preset });
} catch (error) {
if (error instanceof ChatSDKError) {
return error.toResponse();
}
if (error instanceof z.ZodError) {
return NextResponse.json(
{ success: false, error: error.message },
{ status: 400 }
);
}
return NextResponse.json(
{ success: false, error: "更新智能体失败" },
{ status: 500 }
);
}
}
export async function DELETE(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
try {
const session = await auth();
if (!session?.user?.id) {
return NextResponse.json({ success: false, error: "未登录" }, { status: 401 });
}
const { id } = await params;
const deleted = await deleteAgentPresetById({
id,
userId: session.user.id,
});
if (!deleted) {
return NextResponse.json({ success: false, error: "智能体不存在" }, { status: 404 });
}
return NextResponse.json({ success: true, data: deleted });
} catch (error) {
if (error instanceof ChatSDKError) {
return error.toResponse();
}
return NextResponse.json(
{ success: false, error: "删除智能体失败" },
{ status: 500 }
);
}
}