Restore Ultracite + fix sidebar (#1233)

This commit is contained in:
Hayden Bleasel 2025-09-21 11:02:31 -07:00 committed by GitHub
parent 8fbfc253fa
commit 947ed094a6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
177 changed files with 6908 additions and 8306 deletions

View file

@ -1,6 +1,6 @@
'use server';
"use server";
import { getSuggestionsByDocumentId } from '@/lib/db/queries';
import { getSuggestionsByDocumentId } from "@/lib/db/queries";
export async function getSuggestions({ documentId }: { documentId: string }) {
const suggestions = await getSuggestionsByDocumentId({ documentId });

View file

@ -1,5 +1,11 @@
import { Artifact } from '@/components/create-artifact';
import { CodeEditor } from '@/components/code-editor';
import { toast } from "sonner";
import { CodeEditor } from "@/components/code-editor";
import {
Console,
type ConsoleOutput,
type ConsoleOutputContent,
} from "@/components/console";
import { Artifact } from "@/components/create-artifact";
import {
CopyIcon,
LogsIcon,
@ -7,14 +13,8 @@ import {
PlayIcon,
RedoIcon,
UndoIcon,
} from '@/components/icons';
import { toast } from 'sonner';
import { generateUUID } from '@/lib/utils';
import {
Console,
type ConsoleOutput,
type ConsoleOutputContent,
} from '@/components/console';
} from "@/components/icons";
import { generateUUID } from "@/lib/utils";
const OUTPUT_HANDLERS = {
matplotlib: `
@ -53,40 +53,40 @@ const OUTPUT_HANDLERS = {
};
function detectRequiredHandlers(code: string): string[] {
const handlers: string[] = ['basic'];
const handlers: string[] = ["basic"];
if (code.includes('matplotlib') || code.includes('plt.')) {
handlers.push('matplotlib');
if (code.includes("matplotlib") || code.includes("plt.")) {
handlers.push("matplotlib");
}
return handlers;
}
interface Metadata {
outputs: Array<ConsoleOutput>;
}
type Metadata = {
outputs: ConsoleOutput[];
};
export const codeArtifact = new Artifact<'code', Metadata>({
kind: 'code',
export const codeArtifact = new Artifact<"code", Metadata>({
kind: "code",
description:
'Useful for code generation; Code execution is only available for python code.',
initialize: async ({ setMetadata }) => {
"Useful for code generation; Code execution is only available for python code.",
initialize: ({ setMetadata }) => {
setMetadata({
outputs: [],
});
},
onStreamPart: ({ streamPart, setArtifact }) => {
if (streamPart.type === 'data-codeDelta') {
if (streamPart.type === "data-codeDelta") {
setArtifact((draftArtifact) => ({
...draftArtifact,
content: streamPart.data,
isVisible:
draftArtifact.status === 'streaming' &&
draftArtifact.status === "streaming" &&
draftArtifact.content.length > 300 &&
draftArtifact.content.length < 310
? true
: draftArtifact.isVisible,
status: 'streaming',
status: "streaming",
}));
}
},
@ -114,11 +114,11 @@ export const codeArtifact = new Artifact<'code', Metadata>({
actions: [
{
icon: <PlayIcon size={18} />,
label: 'Run',
description: 'Execute code',
label: "Run",
description: "Execute code",
onClick: async ({ content, setMetadata }) => {
const runId = generateUUID();
const outputContent: Array<ConsoleOutputContent> = [];
const outputContent: ConsoleOutputContent[] = [];
setMetadata((metadata) => ({
...metadata,
@ -127,7 +127,7 @@ export const codeArtifact = new Artifact<'code', Metadata>({
{
id: runId,
contents: [],
status: 'in_progress',
status: "in_progress",
},
],
}));
@ -135,15 +135,15 @@ export const codeArtifact = new Artifact<'code', Metadata>({
try {
// @ts-expect-error - loadPyodide is not defined
const currentPyodideInstance = await globalThis.loadPyodide({
indexURL: 'https://cdn.jsdelivr.net/pyodide/v0.23.4/full/',
indexURL: "https://cdn.jsdelivr.net/pyodide/v0.23.4/full/",
});
currentPyodideInstance.setStdout({
batched: (output: string) => {
outputContent.push({
type: output.startsWith('data:image/png;base64')
? 'image'
: 'text',
type: output.startsWith("data:image/png;base64")
? "image"
: "text",
value: output,
});
},
@ -157,8 +157,8 @@ export const codeArtifact = new Artifact<'code', Metadata>({
...metadata.outputs.filter((output) => output.id !== runId),
{
id: runId,
contents: [{ type: 'text', value: message }],
status: 'loading_packages',
contents: [{ type: "text", value: message }],
status: "loading_packages",
},
],
}));
@ -169,12 +169,12 @@ export const codeArtifact = new Artifact<'code', Metadata>({
for (const handler of requiredHandlers) {
if (OUTPUT_HANDLERS[handler as keyof typeof OUTPUT_HANDLERS]) {
await currentPyodideInstance.runPythonAsync(
OUTPUT_HANDLERS[handler as keyof typeof OUTPUT_HANDLERS],
OUTPUT_HANDLERS[handler as keyof typeof OUTPUT_HANDLERS]
);
if (handler === 'matplotlib') {
if (handler === "matplotlib") {
await currentPyodideInstance.runPythonAsync(
'setup_matplotlib_output()',
"setup_matplotlib_output()"
);
}
}
@ -189,7 +189,7 @@ export const codeArtifact = new Artifact<'code', Metadata>({
{
id: runId,
contents: outputContent,
status: 'completed',
status: "completed",
},
],
}));
@ -200,8 +200,8 @@ export const codeArtifact = new Artifact<'code', Metadata>({
...metadata.outputs.filter((output) => output.id !== runId),
{
id: runId,
contents: [{ type: 'text', value: error.message }],
status: 'failed',
contents: [{ type: "text", value: error.message }],
status: "failed",
},
],
}));
@ -210,9 +210,9 @@ export const codeArtifact = new Artifact<'code', Metadata>({
},
{
icon: <UndoIcon size={18} />,
description: 'View Previous version',
description: "View Previous version",
onClick: ({ handleVersionChange }) => {
handleVersionChange('prev');
handleVersionChange("prev");
},
isDisabled: ({ currentVersionIndex }) => {
if (currentVersionIndex === 0) {
@ -224,9 +224,9 @@ export const codeArtifact = new Artifact<'code', Metadata>({
},
{
icon: <RedoIcon size={18} />,
description: 'View Next version',
description: "View Next version",
onClick: ({ handleVersionChange }) => {
handleVersionChange('next');
handleVersionChange("next");
},
isDisabled: ({ isCurrentVersion }) => {
if (isCurrentVersion) {
@ -238,24 +238,24 @@ export const codeArtifact = new Artifact<'code', Metadata>({
},
{
icon: <CopyIcon size={18} />,
description: 'Copy code to clipboard',
description: "Copy code to clipboard",
onClick: ({ content }) => {
navigator.clipboard.writeText(content);
toast.success('Copied to clipboard!');
toast.success("Copied to clipboard!");
},
},
],
toolbar: [
{
icon: <MessageIcon />,
description: 'Add comments',
description: "Add comments",
onClick: ({ sendMessage }) => {
sendMessage({
role: 'user',
role: "user",
parts: [
{
type: 'text',
text: 'Add comments to the code snippet for understanding',
type: "text",
text: "Add comments to the code snippet for understanding",
},
],
});
@ -263,14 +263,14 @@ export const codeArtifact = new Artifact<'code', Metadata>({
},
{
icon: <LogsIcon />,
description: 'Add logs',
description: "Add logs",
onClick: ({ sendMessage }) => {
sendMessage({
role: 'user',
role: "user",
parts: [
{
type: 'text',
text: 'Add logs to the code snippet for debugging',
type: "text",
text: "Add logs to the code snippet for debugging",
},
],
});

View file

@ -1,16 +1,16 @@
import { z } from 'zod';
import { streamObject } from 'ai';
import { myProvider } from '@/lib/ai/providers';
import { codePrompt, updateDocumentPrompt } from '@/lib/ai/prompts';
import { createDocumentHandler } from '@/lib/artifacts/server';
import { streamObject } from "ai";
import { z } from "zod";
import { codePrompt, updateDocumentPrompt } from "@/lib/ai/prompts";
import { myProvider } from "@/lib/ai/providers";
import { createDocumentHandler } from "@/lib/artifacts/server";
export const codeDocumentHandler = createDocumentHandler<'code'>({
kind: 'code',
export const codeDocumentHandler = createDocumentHandler<"code">({
kind: "code",
onCreateDocument: async ({ title, dataStream }) => {
let draftContent = '';
let draftContent = "";
const { fullStream } = streamObject({
model: myProvider.languageModel('artifact-model'),
model: myProvider.languageModel("artifact-model"),
system: codePrompt,
prompt: title,
schema: z.object({
@ -21,14 +21,14 @@ export const codeDocumentHandler = createDocumentHandler<'code'>({
for await (const delta of fullStream) {
const { type } = delta;
if (type === 'object') {
if (type === "object") {
const { object } = delta;
const { code } = object;
if (code) {
dataStream.write({
type: 'data-codeDelta',
data: code ?? '',
type: "data-codeDelta",
data: code ?? "",
transient: true,
});
@ -40,11 +40,11 @@ export const codeDocumentHandler = createDocumentHandler<'code'>({
return draftContent;
},
onUpdateDocument: async ({ document, description, dataStream }) => {
let draftContent = '';
let draftContent = "";
const { fullStream } = streamObject({
model: myProvider.languageModel('artifact-model'),
system: updateDocumentPrompt(document.content, 'code'),
model: myProvider.languageModel("artifact-model"),
system: updateDocumentPrompt(document.content, "code"),
prompt: description,
schema: z.object({
code: z.string(),
@ -54,14 +54,14 @@ export const codeDocumentHandler = createDocumentHandler<'code'>({
for await (const delta of fullStream) {
const { type } = delta;
if (type === 'object') {
if (type === "object") {
const { object } = delta;
const { code } = object;
if (code) {
dataStream.write({
type: 'data-codeDelta',
data: code ?? '',
type: "data-codeDelta",
data: code ?? "",
transient: true,
});

View file

@ -1,18 +1,18 @@
import { Artifact } from '@/components/create-artifact';
import { CopyIcon, RedoIcon, UndoIcon } from '@/components/icons';
import { ImageEditor } from '@/components/image-editor';
import { toast } from 'sonner';
import { toast } from "sonner";
import { Artifact } from "@/components/create-artifact";
import { CopyIcon, RedoIcon, UndoIcon } from "@/components/icons";
import { ImageEditor } from "@/components/image-editor";
export const imageArtifact = new Artifact({
kind: 'image',
description: 'Useful for image generation',
kind: "image",
description: "Useful for image generation",
onStreamPart: ({ streamPart, setArtifact }) => {
if (streamPart.type === 'data-imageDelta') {
if (streamPart.type === "data-imageDelta") {
setArtifact((draftArtifact) => ({
...draftArtifact,
content: streamPart.data,
isVisible: true,
status: 'streaming',
status: "streaming",
}));
}
},
@ -20,9 +20,9 @@ export const imageArtifact = new Artifact({
actions: [
{
icon: <UndoIcon size={18} />,
description: 'View Previous version',
description: "View Previous version",
onClick: ({ handleVersionChange }) => {
handleVersionChange('prev');
handleVersionChange("prev");
},
isDisabled: ({ currentVersionIndex }) => {
if (currentVersionIndex === 0) {
@ -34,9 +34,9 @@ export const imageArtifact = new Artifact({
},
{
icon: <RedoIcon size={18} />,
description: 'View Next version',
description: "View Next version",
onClick: ({ handleVersionChange }) => {
handleVersionChange('next');
handleVersionChange("next");
},
isDisabled: ({ isCurrentVersion }) => {
if (isCurrentVersion) {
@ -48,27 +48,27 @@ export const imageArtifact = new Artifact({
},
{
icon: <CopyIcon size={18} />,
description: 'Copy image to clipboard',
description: "Copy image to clipboard",
onClick: ({ content }) => {
const img = new Image();
img.src = `data:image/png;base64,${content}`;
img.onload = () => {
const canvas = document.createElement('canvas');
const canvas = document.createElement("canvas");
canvas.width = img.width;
canvas.height = img.height;
const ctx = canvas.getContext('2d');
const ctx = canvas.getContext("2d");
ctx?.drawImage(img, 0, 0);
canvas.toBlob((blob) => {
if (blob) {
navigator.clipboard.write([
new ClipboardItem({ 'image/png': blob }),
new ClipboardItem({ "image/png": blob }),
]);
}
}, 'image/png');
}, "image/png");
};
toast.success('Copied image to clipboard!');
toast.success("Copied image to clipboard!");
},
},
],

View file

@ -1,43 +1,37 @@
import { Artifact } from '@/components/create-artifact';
import { parse, unparse } from "papaparse";
import { toast } from "sonner";
import { Artifact } from "@/components/create-artifact";
import {
CopyIcon,
LineChartIcon,
RedoIcon,
SparklesIcon,
UndoIcon,
} from '@/components/icons';
import { SpreadsheetEditor } from '@/components/sheet-editor';
import { parse, unparse } from 'papaparse';
import { toast } from 'sonner';
} from "@/components/icons";
import { SpreadsheetEditor } from "@/components/sheet-editor";
type Metadata = any;
export const sheetArtifact = new Artifact<'sheet', Metadata>({
kind: 'sheet',
description: 'Useful for working with spreadsheets',
initialize: async () => {},
export const sheetArtifact = new Artifact<"sheet", Metadata>({
kind: "sheet",
description: "Useful for working with spreadsheets",
initialize: () => null,
onStreamPart: ({ setArtifact, streamPart }) => {
if (streamPart.type === 'data-sheetDelta') {
if (streamPart.type === "data-sheetDelta") {
setArtifact((draftArtifact) => ({
...draftArtifact,
content: streamPart.data,
isVisible: true,
status: 'streaming',
status: "streaming",
}));
}
},
content: ({
content,
currentVersionIndex,
isCurrentVersion,
onSaveContent,
status,
}) => {
content: ({ content, currentVersionIndex, onSaveContent, status }) => {
return (
<SpreadsheetEditor
content={content}
currentVersionIndex={currentVersionIndex}
isCurrentVersion={isCurrentVersion}
isCurrentVersion={true}
saveContent={onSaveContent}
status={status}
/>
@ -46,9 +40,9 @@ export const sheetArtifact = new Artifact<'sheet', Metadata>({
actions: [
{
icon: <UndoIcon size={18} />,
description: 'View Previous version',
description: "View Previous version",
onClick: ({ handleVersionChange }) => {
handleVersionChange('prev');
handleVersionChange("prev");
},
isDisabled: ({ currentVersionIndex }) => {
if (currentVersionIndex === 0) {
@ -60,9 +54,9 @@ export const sheetArtifact = new Artifact<'sheet', Metadata>({
},
{
icon: <RedoIcon size={18} />,
description: 'View Next version',
description: "View Next version",
onClick: ({ handleVersionChange }) => {
handleVersionChange('next');
handleVersionChange("next");
},
isDisabled: ({ isCurrentVersion }) => {
if (isCurrentVersion) {
@ -74,44 +68,44 @@ export const sheetArtifact = new Artifact<'sheet', Metadata>({
},
{
icon: <CopyIcon />,
description: 'Copy as .csv',
description: "Copy as .csv",
onClick: ({ content }) => {
const parsed = parse<string[]>(content, { skipEmptyLines: true });
const nonEmptyRows = parsed.data.filter((row) =>
row.some((cell) => cell.trim() !== ''),
row.some((cell) => cell.trim() !== "")
);
const cleanedCsv = unparse(nonEmptyRows);
navigator.clipboard.writeText(cleanedCsv);
toast.success('Copied csv to clipboard!');
toast.success("Copied csv to clipboard!");
},
},
],
toolbar: [
{
description: 'Format and clean data',
description: "Format and clean data",
icon: <SparklesIcon />,
onClick: ({ sendMessage }) => {
sendMessage({
role: 'user',
role: "user",
parts: [
{ type: 'text', text: 'Can you please format and clean the data?' },
{ type: "text", text: "Can you please format and clean the data?" },
],
});
},
},
{
description: 'Analyze and visualize data',
description: "Analyze and visualize data",
icon: <LineChartIcon />,
onClick: ({ sendMessage }) => {
sendMessage({
role: 'user',
role: "user",
parts: [
{
type: 'text',
text: 'Can you please analyze and visualize the data by creating a new code artifact in python?',
type: "text",
text: "Can you please analyze and visualize the data by creating a new code artifact in python?",
},
],
});

View file

@ -1,33 +1,33 @@
import { myProvider } from '@/lib/ai/providers';
import { sheetPrompt, updateDocumentPrompt } from '@/lib/ai/prompts';
import { createDocumentHandler } from '@/lib/artifacts/server';
import { streamObject } from 'ai';
import { z } from 'zod';
import { streamObject } from "ai";
import { z } from "zod";
import { sheetPrompt, updateDocumentPrompt } from "@/lib/ai/prompts";
import { myProvider } from "@/lib/ai/providers";
import { createDocumentHandler } from "@/lib/artifacts/server";
export const sheetDocumentHandler = createDocumentHandler<'sheet'>({
kind: 'sheet',
export const sheetDocumentHandler = createDocumentHandler<"sheet">({
kind: "sheet",
onCreateDocument: async ({ title, dataStream }) => {
let draftContent = '';
let draftContent = "";
const { fullStream } = streamObject({
model: myProvider.languageModel('artifact-model'),
model: myProvider.languageModel("artifact-model"),
system: sheetPrompt,
prompt: title,
schema: z.object({
csv: z.string().describe('CSV data'),
csv: z.string().describe("CSV data"),
}),
});
for await (const delta of fullStream) {
const { type } = delta;
if (type === 'object') {
if (type === "object") {
const { object } = delta;
const { csv } = object;
if (csv) {
dataStream.write({
type: 'data-sheetDelta',
type: "data-sheetDelta",
data: csv,
transient: true,
});
@ -38,7 +38,7 @@ export const sheetDocumentHandler = createDocumentHandler<'sheet'>({
}
dataStream.write({
type: 'data-sheetDelta',
type: "data-sheetDelta",
data: draftContent,
transient: true,
});
@ -46,11 +46,11 @@ export const sheetDocumentHandler = createDocumentHandler<'sheet'>({
return draftContent;
},
onUpdateDocument: async ({ document, description, dataStream }) => {
let draftContent = '';
let draftContent = "";
const { fullStream } = streamObject({
model: myProvider.languageModel('artifact-model'),
system: updateDocumentPrompt(document.content, 'sheet'),
model: myProvider.languageModel("artifact-model"),
system: updateDocumentPrompt(document.content, "sheet"),
prompt: description,
schema: z.object({
csv: z.string(),
@ -60,13 +60,13 @@ export const sheetDocumentHandler = createDocumentHandler<'sheet'>({
for await (const delta of fullStream) {
const { type } = delta;
if (type === 'object') {
if (type === "object") {
const { object } = delta;
const { csv } = object;
if (csv) {
dataStream.write({
type: 'data-sheetDelta',
type: "data-sheetDelta",
data: csv,
transient: true,
});

View file

@ -1,7 +1,7 @@
import { Artifact } from '@/components/create-artifact';
import { DiffView } from '@/components/diffview';
import { DocumentSkeleton } from '@/components/document-skeleton';
import { Editor } from '@/components/text-editor';
import { toast } from "sonner";
import { Artifact } from "@/components/create-artifact";
import { DiffView } from "@/components/diffview";
import { DocumentSkeleton } from "@/components/document-skeleton";
import {
ClockRewind,
CopyIcon,
@ -9,18 +9,18 @@ import {
PenIcon,
RedoIcon,
UndoIcon,
} from '@/components/icons';
import type { Suggestion } from '@/lib/db/schema';
import { toast } from 'sonner';
import { getSuggestions } from '../actions';
} from "@/components/icons";
import { Editor } from "@/components/text-editor";
import type { Suggestion } from "@/lib/db/schema";
import { getSuggestions } from "../actions";
interface TextArtifactMetadata {
suggestions: Array<Suggestion>;
}
type TextArtifactMetadata = {
suggestions: Suggestion[];
};
export const textArtifact = new Artifact<'text', TextArtifactMetadata>({
kind: 'text',
description: 'Useful for text content, like drafting essays and emails.',
export const textArtifact = new Artifact<"text", TextArtifactMetadata>({
kind: "text",
description: "Useful for text content, like drafting essays and emails.",
initialize: async ({ documentId, setMetadata }) => {
const suggestions = await getSuggestions({ documentId });
@ -29,7 +29,7 @@ export const textArtifact = new Artifact<'text', TextArtifactMetadata>({
});
},
onStreamPart: ({ streamPart, setMetadata, setArtifact }) => {
if (streamPart.type === 'data-suggestion') {
if (streamPart.type === "data-suggestion") {
setMetadata((metadata) => {
return {
suggestions: [...metadata.suggestions, streamPart.data],
@ -37,18 +37,18 @@ export const textArtifact = new Artifact<'text', TextArtifactMetadata>({
});
}
if (streamPart.type === 'data-textDelta') {
if (streamPart.type === "data-textDelta") {
setArtifact((draftArtifact) => {
return {
...draftArtifact,
content: draftArtifact.content + streamPart.data,
isVisible:
draftArtifact.status === 'streaming' &&
draftArtifact.status === "streaming" &&
draftArtifact.content.length > 400 &&
draftArtifact.content.length < 450
? true
: draftArtifact.isVisible,
status: 'streaming',
status: "streaming",
};
});
}
@ -68,40 +68,38 @@ export const textArtifact = new Artifact<'text', TextArtifactMetadata>({
return <DocumentSkeleton artifactKind="text" />;
}
if (mode === 'diff') {
if (mode === "diff") {
const oldContent = getDocumentContentById(currentVersionIndex - 1);
const newContent = getDocumentContentById(currentVersionIndex);
return <DiffView oldContent={oldContent} newContent={newContent} />;
return <DiffView newContent={newContent} oldContent={oldContent} />;
}
return (
<>
<div className="flex flex-row px-4 py-8 md:p-20">
<Editor
content={content}
suggestions={metadata ? metadata.suggestions : []}
isCurrentVersion={isCurrentVersion}
currentVersionIndex={currentVersionIndex}
status={status}
onSaveContent={onSaveContent}
/>
<div className="flex flex-row px-4 py-8 md:p-20">
<Editor
content={content}
currentVersionIndex={currentVersionIndex}
isCurrentVersion={isCurrentVersion}
onSaveContent={onSaveContent}
status={status}
suggestions={metadata ? metadata.suggestions : []}
/>
{metadata?.suggestions && metadata.suggestions.length > 0 ? (
<div className="h-dvh w-12 shrink-0 md:hidden" />
) : null}
</div>
</>
{metadata?.suggestions && metadata.suggestions.length > 0 ? (
<div className="h-dvh w-12 shrink-0 md:hidden" />
) : null}
</div>
);
},
actions: [
{
icon: <ClockRewind size={18} />,
description: 'View changes',
description: "View changes",
onClick: ({ handleVersionChange }) => {
handleVersionChange('toggle');
handleVersionChange("toggle");
},
isDisabled: ({ currentVersionIndex, setMetadata }) => {
isDisabled: ({ currentVersionIndex }) => {
if (currentVersionIndex === 0) {
return true;
}
@ -111,9 +109,9 @@ export const textArtifact = new Artifact<'text', TextArtifactMetadata>({
},
{
icon: <UndoIcon size={18} />,
description: 'View Previous version',
description: "View Previous version",
onClick: ({ handleVersionChange }) => {
handleVersionChange('prev');
handleVersionChange("prev");
},
isDisabled: ({ currentVersionIndex }) => {
if (currentVersionIndex === 0) {
@ -125,9 +123,9 @@ export const textArtifact = new Artifact<'text', TextArtifactMetadata>({
},
{
icon: <RedoIcon size={18} />,
description: 'View Next version',
description: "View Next version",
onClick: ({ handleVersionChange }) => {
handleVersionChange('next');
handleVersionChange("next");
},
isDisabled: ({ isCurrentVersion }) => {
if (isCurrentVersion) {
@ -139,24 +137,24 @@ export const textArtifact = new Artifact<'text', TextArtifactMetadata>({
},
{
icon: <CopyIcon size={18} />,
description: 'Copy to clipboard',
description: "Copy to clipboard",
onClick: ({ content }) => {
navigator.clipboard.writeText(content);
toast.success('Copied to clipboard!');
toast.success("Copied to clipboard!");
},
},
],
toolbar: [
{
icon: <PenIcon />,
description: 'Add final polish',
description: "Add final polish",
onClick: ({ sendMessage }) => {
sendMessage({
role: 'user',
role: "user",
parts: [
{
type: 'text',
text: 'Please add final polish and check for grammar, add section titles for better structure, and ensure everything reads smoothly.',
type: "text",
text: "Please add final polish and check for grammar, add section titles for better structure, and ensure everything reads smoothly.",
},
],
});
@ -164,14 +162,14 @@ export const textArtifact = new Artifact<'text', TextArtifactMetadata>({
},
{
icon: <MessageIcon />,
description: 'Request suggestions',
description: "Request suggestions",
onClick: ({ sendMessage }) => {
sendMessage({
role: 'user',
role: "user",
parts: [
{
type: 'text',
text: 'Please add suggestions you have that could improve the writing.',
type: "text",
text: "Please add suggestions you have that could improve the writing.",
},
],
});

View file

@ -1,31 +1,31 @@
import { smoothStream, streamText } from 'ai';
import { myProvider } from '@/lib/ai/providers';
import { createDocumentHandler } from '@/lib/artifacts/server';
import { updateDocumentPrompt } from '@/lib/ai/prompts';
import { smoothStream, streamText } from "ai";
import { updateDocumentPrompt } from "@/lib/ai/prompts";
import { myProvider } from "@/lib/ai/providers";
import { createDocumentHandler } from "@/lib/artifacts/server";
export const textDocumentHandler = createDocumentHandler<'text'>({
kind: 'text',
export const textDocumentHandler = createDocumentHandler<"text">({
kind: "text",
onCreateDocument: async ({ title, dataStream }) => {
let draftContent = '';
let draftContent = "";
const { fullStream } = streamText({
model: myProvider.languageModel('artifact-model'),
model: myProvider.languageModel("artifact-model"),
system:
'Write about the given topic. Markdown is supported. Use headings wherever appropriate.',
experimental_transform: smoothStream({ chunking: 'word' }),
"Write about the given topic. Markdown is supported. Use headings wherever appropriate.",
experimental_transform: smoothStream({ chunking: "word" }),
prompt: title,
});
for await (const delta of fullStream) {
const { type } = delta;
if (type === 'text-delta') {
if (type === "text-delta") {
const { text } = delta;
draftContent += text;
dataStream.write({
type: 'data-textDelta',
type: "data-textDelta",
data: text,
transient: true,
});
@ -35,17 +35,17 @@ export const textDocumentHandler = createDocumentHandler<'text'>({
return draftContent;
},
onUpdateDocument: async ({ document, description, dataStream }) => {
let draftContent = '';
let draftContent = "";
const { fullStream } = streamText({
model: myProvider.languageModel('artifact-model'),
system: updateDocumentPrompt(document.content, 'text'),
experimental_transform: smoothStream({ chunking: 'word' }),
model: myProvider.languageModel("artifact-model"),
system: updateDocumentPrompt(document.content, "text"),
experimental_transform: smoothStream({ chunking: "word" }),
prompt: description,
providerOptions: {
openai: {
prediction: {
type: 'content',
type: "content",
content: document.content,
},
},
@ -55,13 +55,13 @@ export const textDocumentHandler = createDocumentHandler<'text'>({
for await (const delta of fullStream) {
const { type } = delta;
if (type === 'text-delta') {
if (type === "text-delta") {
const { text } = delta;
draftContent += text;
dataStream.write({
type: 'data-textDelta',
type: "data-textDelta",
data: text,
transient: true,
});