2025-09-21 11:02:31 -07:00
|
|
|
import { tool, type UIMessageStreamWriter } from "ai";
|
2026-03-20 09:37:02 +00:00
|
|
|
import type { Session } from "next-auth";
|
2025-09-21 11:02:31 -07:00
|
|
|
import { z } from "zod";
|
|
|
|
|
import { documentHandlersByArtifactKind } from "@/lib/artifacts/server";
|
|
|
|
|
import { getDocumentById } from "@/lib/db/queries";
|
|
|
|
|
import type { ChatMessage } from "@/lib/types";
|
|
|
|
|
|
|
|
|
|
type UpdateDocumentProps = {
|
2026-03-20 09:37:02 +00:00
|
|
|
session: Session;
|
2025-07-03 02:26:34 -07:00
|
|
|
dataStream: UIMessageStreamWriter<ChatMessage>;
|
2026-03-20 09:37:02 +00:00
|
|
|
modelId: string;
|
2025-09-21 11:02:31 -07:00
|
|
|
};
|
2025-01-23 01:19:48 +05:30
|
|
|
|
2026-03-20 09:37:02 +00:00
|
|
|
export const updateDocument = ({
|
|
|
|
|
session,
|
|
|
|
|
dataStream,
|
|
|
|
|
modelId,
|
|
|
|
|
}: UpdateDocumentProps) =>
|
2025-01-23 01:19:48 +05:30
|
|
|
tool({
|
2026-03-20 09:37:02 +00:00
|
|
|
description:
|
|
|
|
|
"Full rewrite of an existing artifact. Only use for major changes where most content needs replacing. Prefer editDocument for targeted changes.",
|
2025-07-03 02:26:34 -07:00
|
|
|
inputSchema: z.object({
|
2026-03-20 09:37:02 +00:00
|
|
|
id: z.string().describe("The ID of the artifact to rewrite"),
|
2025-01-23 01:19:48 +05:30
|
|
|
description: z
|
|
|
|
|
.string()
|
2026-03-20 09:37:02 +00:00
|
|
|
.default("Improve the content")
|
2025-09-21 11:02:31 -07:00
|
|
|
.describe("The description of changes that need to be made"),
|
2025-01-23 01:19:48 +05:30
|
|
|
}),
|
|
|
|
|
execute: async ({ id, description }) => {
|
|
|
|
|
const document = await getDocumentById({ id });
|
|
|
|
|
|
|
|
|
|
if (!document) {
|
|
|
|
|
return {
|
2025-09-21 11:02:31 -07:00
|
|
|
error: "Document not found",
|
2025-01-23 01:19:48 +05:30
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-20 09:37:02 +00:00
|
|
|
if (document.userId !== session.user?.id) {
|
|
|
|
|
return { error: "Forbidden" };
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-03 02:26:34 -07:00
|
|
|
dataStream.write({
|
2025-09-21 11:02:31 -07:00
|
|
|
type: "data-clear",
|
2025-07-03 02:26:34 -07:00
|
|
|
data: null,
|
|
|
|
|
transient: true,
|
2025-01-23 01:19:48 +05:30
|
|
|
});
|
|
|
|
|
|
2025-02-13 08:25:57 -08:00
|
|
|
const documentHandler = documentHandlersByArtifactKind.find(
|
|
|
|
|
(documentHandlerByArtifactKind) =>
|
2025-09-21 11:02:31 -07:00
|
|
|
documentHandlerByArtifactKind.kind === document.kind
|
2025-02-04 17:15:39 +03:00
|
|
|
);
|
2025-01-23 01:19:48 +05:30
|
|
|
|
2025-02-04 17:15:39 +03:00
|
|
|
if (!documentHandler) {
|
|
|
|
|
throw new Error(`No document handler found for kind: ${document.kind}`);
|
2025-01-23 01:19:48 +05:30
|
|
|
}
|
|
|
|
|
|
2025-02-04 17:15:39 +03:00
|
|
|
await documentHandler.onUpdateDocument({
|
|
|
|
|
document,
|
|
|
|
|
description,
|
|
|
|
|
dataStream,
|
|
|
|
|
session,
|
2026-03-20 09:37:02 +00:00
|
|
|
modelId,
|
2025-02-04 17:15:39 +03:00
|
|
|
});
|
|
|
|
|
|
2025-09-21 11:02:31 -07:00
|
|
|
dataStream.write({ type: "data-finish", data: null, transient: true });
|
2025-01-23 01:19:48 +05:30
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
id,
|
|
|
|
|
title: document.title,
|
|
|
|
|
kind: document.kind,
|
2026-03-20 09:37:02 +00:00
|
|
|
content:
|
|
|
|
|
document.kind === "code"
|
|
|
|
|
? "The script has been updated successfully."
|
|
|
|
|
: "The document has been updated successfully.",
|
2025-01-23 01:19:48 +05:30
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
});
|