chatbot-template/lib/ai/tools/create-document.ts

90 lines
2.3 KiB
TypeScript
Raw Normal View History

import { tool, type UIMessageStreamWriter } from "ai";
import type { Session } from "next-auth";
import { z } from "zod";
import {
artifactKinds,
documentHandlersByArtifactKind,
} from "@/lib/artifacts/server";
import type { ChatMessage } from "@/lib/types";
import { generateUUID } from "@/lib/utils";
type CreateDocumentProps = {
session: Session;
dataStream: UIMessageStreamWriter<ChatMessage>;
modelId: string;
};
export const createDocument = ({
session,
dataStream,
modelId,
}: CreateDocumentProps) =>
tool({
description:
"Create an artifact. You MUST specify kind: use 'code' for any programming/algorithm request (creates a script), 'text' for essays/writing (creates a document), 'sheet' for spreadsheets/data.",
inputSchema: z.object({
title: z.string().describe("The title of the artifact"),
kind: z
.enum(artifactKinds)
.describe(
"REQUIRED. 'code' for programming/algorithms, 'text' for essays/writing, 'sheet' for spreadsheets"
),
}),
execute: async ({ title, kind }) => {
const id = generateUUID();
dataStream.write({
type: "data-kind",
data: kind,
transient: true,
});
dataStream.write({
type: "data-id",
data: id,
transient: true,
});
dataStream.write({
type: "data-title",
data: title,
transient: true,
});
dataStream.write({
type: "data-clear",
data: null,
transient: true,
});
const documentHandler = documentHandlersByArtifactKind.find(
(documentHandlerByArtifactKind) =>
documentHandlerByArtifactKind.kind === kind
);
if (!documentHandler) {
throw new Error(`No document handler found for kind: ${kind}`);
}
await documentHandler.onCreateDocument({
id,
title,
dataStream,
session,
modelId,
});
dataStream.write({ type: "data-finish", data: null, transient: true });
return {
id,
title,
kind,
content:
kind === "code"
? "A script was created and is now visible to the user."
: "A document was created and is now visible to the user.",
};
},
});