feat: v1 — persistent shell, model gateway, artifact improvements (#1462)
This commit is contained in:
parent
3651670fb9
commit
f9652b452a
161 changed files with 5166 additions and 8009 deletions
|
|
@ -1,12 +1,21 @@
|
|||
import type { ArtifactKind } from "@/components/artifact";
|
||||
import { getSession } from "@/lib/auth";
|
||||
import { z } from "zod";
|
||||
import { auth } from "@/app/(auth)/auth";
|
||||
import type { ArtifactKind } from "@/components/chat/artifact";
|
||||
import {
|
||||
deleteDocumentsByIdAfterTimestamp,
|
||||
getDocumentsById,
|
||||
saveDocument,
|
||||
updateDocumentContent,
|
||||
} from "@/lib/db/queries";
|
||||
import { ChatbotError } from "@/lib/errors";
|
||||
|
||||
const documentSchema = z.object({
|
||||
content: z.string(),
|
||||
title: z.string(),
|
||||
kind: z.enum(["text", "code", "image", "sheet"]),
|
||||
isManualEdit: z.boolean().optional(),
|
||||
});
|
||||
|
||||
export async function GET(request: Request) {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const id = searchParams.get("id");
|
||||
|
|
@ -18,7 +27,7 @@ export async function GET(request: Request) {
|
|||
).toResponse();
|
||||
}
|
||||
|
||||
const session = await getSession();
|
||||
const session = await auth();
|
||||
|
||||
if (!session?.user) {
|
||||
return new ChatbotError("unauthorized:document").toResponse();
|
||||
|
|
@ -50,18 +59,29 @@ export async function POST(request: Request) {
|
|||
).toResponse();
|
||||
}
|
||||
|
||||
const session = await getSession();
|
||||
const session = await auth();
|
||||
|
||||
if (!session?.user) {
|
||||
return new ChatbotError("not_found:document").toResponse();
|
||||
}
|
||||
|
||||
const {
|
||||
content,
|
||||
title,
|
||||
kind,
|
||||
}: { content: string; title: string; kind: ArtifactKind } =
|
||||
await request.json();
|
||||
let content: string;
|
||||
let title: string;
|
||||
let kind: ArtifactKind;
|
||||
let isManualEdit: boolean | undefined;
|
||||
|
||||
try {
|
||||
const parsed = documentSchema.parse(await request.json());
|
||||
content = parsed.content;
|
||||
title = parsed.title;
|
||||
kind = parsed.kind;
|
||||
isManualEdit = parsed.isManualEdit;
|
||||
} catch {
|
||||
return new ChatbotError(
|
||||
"bad_request:api",
|
||||
"Invalid request body."
|
||||
).toResponse();
|
||||
}
|
||||
|
||||
const documents = await getDocumentsById({ id });
|
||||
|
||||
|
|
@ -73,6 +93,11 @@ export async function POST(request: Request) {
|
|||
}
|
||||
}
|
||||
|
||||
if (isManualEdit && documents.length > 0) {
|
||||
const result = await updateDocumentContent({ id, content });
|
||||
return Response.json(result, { status: 200 });
|
||||
}
|
||||
|
||||
const document = await saveDocument({
|
||||
id,
|
||||
content,
|
||||
|
|
@ -103,7 +128,7 @@ export async function DELETE(request: Request) {
|
|||
).toResponse();
|
||||
}
|
||||
|
||||
const session = await getSession();
|
||||
const session = await auth();
|
||||
|
||||
if (!session?.user) {
|
||||
return new ChatbotError("unauthorized:document").toResponse();
|
||||
|
|
@ -117,9 +142,18 @@ export async function DELETE(request: Request) {
|
|||
return new ChatbotError("forbidden:document").toResponse();
|
||||
}
|
||||
|
||||
const parsedTimestamp = new Date(timestamp);
|
||||
|
||||
if (Number.isNaN(parsedTimestamp.getTime())) {
|
||||
return new ChatbotError(
|
||||
"bad_request:api",
|
||||
"Invalid timestamp."
|
||||
).toResponse();
|
||||
}
|
||||
|
||||
const documentsDeleted = await deleteDocumentsByIdAfterTimestamp({
|
||||
id,
|
||||
timestamp: new Date(timestamp),
|
||||
timestamp: parsedTimestamp,
|
||||
});
|
||||
|
||||
return Response.json(documentsDeleted, { status: 200 });
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue