chore: rename blocks to artifacts (#793)

This commit is contained in:
Jeremy 2025-02-13 08:25:57 -08:00 committed by GitHub
parent 01f589b603
commit 81f909ac3a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
41 changed files with 473 additions and 473 deletions

8
artifacts/actions.ts Normal file
View file

@ -0,0 +1,8 @@
'use server';
import { getSuggestionsByDocumentId } from '@/lib/db/queries';
export async function getSuggestions({ documentId }: { documentId: string }) {
const suggestions = await getSuggestionsByDocumentId({ documentId });
return suggestions ?? [];
}

270
artifacts/code/client.tsx Normal file
View file

@ -0,0 +1,270 @@
import { Artifact } from '@/components/create-artifact';
import { CodeEditor } from '@/components/code-editor';
import {
CopyIcon,
LogsIcon,
MessageIcon,
PlayIcon,
RedoIcon,
UndoIcon,
} from '@/components/icons';
import { toast } from 'sonner';
import { generateUUID } from '@/lib/utils';
import {
Console,
ConsoleOutput,
ConsoleOutputContent,
} from '@/components/console';
const OUTPUT_HANDLERS = {
matplotlib: `
import io
import base64
from matplotlib import pyplot as plt
# Clear any existing plots
plt.clf()
plt.close('all')
# Switch to agg backend
plt.switch_backend('agg')
def setup_matplotlib_output():
def custom_show():
if plt.gcf().get_size_inches().prod() * plt.gcf().dpi ** 2 > 25_000_000:
print("Warning: Plot size too large, reducing quality")
plt.gcf().set_dpi(100)
png_buf = io.BytesIO()
plt.savefig(png_buf, format='png')
png_buf.seek(0)
png_base64 = base64.b64encode(png_buf.read()).decode('utf-8')
print(f'data:image/png;base64,{png_base64}')
png_buf.close()
plt.clf()
plt.close('all')
plt.show = custom_show
`,
basic: `
# Basic output capture setup
`,
};
function detectRequiredHandlers(code: string): string[] {
const handlers: string[] = ['basic'];
if (code.includes('matplotlib') || code.includes('plt.')) {
handlers.push('matplotlib');
}
return handlers;
}
interface Metadata {
outputs: Array<ConsoleOutput>;
}
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 }) => {
setMetadata({
outputs: [],
});
},
onStreamPart: ({ streamPart, setArtifact }) => {
if (streamPart.type === 'code-delta') {
setArtifact((draftArtifact) => ({
...draftArtifact,
content: streamPart.content as string,
isVisible:
draftArtifact.status === 'streaming' &&
draftArtifact.content.length > 300 &&
draftArtifact.content.length < 310
? true
: draftArtifact.isVisible,
status: 'streaming',
}));
}
},
content: ({ metadata, setMetadata, ...props }) => {
return (
<>
<div className="px-1">
<CodeEditor {...props} />
</div>
{metadata?.outputs && (
<Console
consoleOutputs={metadata.outputs}
setConsoleOutputs={() => {
setMetadata({
...metadata,
outputs: [],
});
}}
/>
)}
</>
);
},
actions: [
{
icon: <PlayIcon size={18} />,
label: 'Run',
description: 'Execute code',
onClick: async ({ content, setMetadata }) => {
const runId = generateUUID();
const outputContent: Array<ConsoleOutputContent> = [];
setMetadata((metadata) => ({
...metadata,
outputs: [
...metadata.outputs,
{
id: runId,
contents: [],
status: 'in_progress',
},
],
}));
try {
// @ts-expect-error - loadPyodide is not defined
const currentPyodideInstance = await globalThis.loadPyodide({
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',
value: output,
});
},
});
await currentPyodideInstance.loadPackagesFromImports(content, {
messageCallback: (message: string) => {
setMetadata((metadata) => ({
...metadata,
outputs: [
...metadata.outputs.filter((output) => output.id !== runId),
{
id: runId,
contents: [{ type: 'text', value: message }],
status: 'loading_packages',
},
],
}));
},
});
const requiredHandlers = detectRequiredHandlers(content);
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],
);
if (handler === 'matplotlib') {
await currentPyodideInstance.runPythonAsync(
'setup_matplotlib_output()',
);
}
}
}
await currentPyodideInstance.runPythonAsync(content);
setMetadata((metadata) => ({
...metadata,
outputs: [
...metadata.outputs.filter((output) => output.id !== runId),
{
id: runId,
contents: outputContent,
status: 'completed',
},
],
}));
} catch (error: any) {
setMetadata((metadata) => ({
...metadata,
outputs: [
...metadata.outputs.filter((output) => output.id !== runId),
{
id: runId,
contents: [{ type: 'text', value: error.message }],
status: 'failed',
},
],
}));
}
},
},
{
icon: <UndoIcon size={18} />,
description: 'View Previous version',
onClick: ({ handleVersionChange }) => {
handleVersionChange('prev');
},
isDisabled: ({ currentVersionIndex }) => {
if (currentVersionIndex === 0) {
return true;
}
return false;
},
},
{
icon: <RedoIcon size={18} />,
description: 'View Next version',
onClick: ({ handleVersionChange }) => {
handleVersionChange('next');
},
isDisabled: ({ isCurrentVersion }) => {
if (isCurrentVersion) {
return true;
}
return false;
},
},
{
icon: <CopyIcon size={18} />,
description: 'Copy code to clipboard',
onClick: ({ content }) => {
navigator.clipboard.writeText(content);
toast.success('Copied to clipboard!');
},
},
],
toolbar: [
{
icon: <MessageIcon />,
description: 'Add comments',
onClick: ({ appendMessage }) => {
appendMessage({
role: 'user',
content: 'Add comments to the code snippet for understanding',
});
},
},
{
icon: <LogsIcon />,
description: 'Add logs',
onClick: ({ appendMessage }) => {
appendMessage({
role: 'user',
content: 'Add logs to the code snippet for debugging',
});
},
},
],
});

73
artifacts/code/server.ts Normal file
View file

@ -0,0 +1,73 @@
import { z } from 'zod';
import { streamObject } from 'ai';
import { myProvider } from '@/lib/ai/models';
import { codePrompt, updateDocumentPrompt } from '@/lib/ai/prompts';
import { createDocumentHandler } from '@/lib/artifacts/server';
export const codeDocumentHandler = createDocumentHandler<'code'>({
kind: 'code',
onCreateDocument: async ({ title, dataStream }) => {
let draftContent = '';
const { fullStream } = streamObject({
model: myProvider.languageModel('artifact-model'),
system: codePrompt,
prompt: title,
schema: z.object({
code: z.string(),
}),
});
for await (const delta of fullStream) {
const { type } = delta;
if (type === 'object') {
const { object } = delta;
const { code } = object;
if (code) {
dataStream.writeData({
type: 'code-delta',
content: code ?? '',
});
draftContent = code;
}
}
}
return draftContent;
},
onUpdateDocument: async ({ document, description, dataStream }) => {
let draftContent = '';
const { fullStream } = streamObject({
model: myProvider.languageModel('artifacts-model'),
system: updateDocumentPrompt(document.content, 'code'),
prompt: description,
schema: z.object({
code: z.string(),
}),
});
for await (const delta of fullStream) {
const { type } = delta;
if (type === 'object') {
const { object } = delta;
const { code } = object;
if (code) {
dataStream.writeData({
type: 'code-delta',
content: code ?? '',
});
draftContent = code;
}
}
}
return draftContent;
},
});

View file

@ -0,0 +1,76 @@
import { Artifact } from '@/components/create-artifact';
import { CopyIcon, RedoIcon, UndoIcon } from '@/components/icons';
import { ImageEditor } from '@/components/image-editor';
import { toast } from 'sonner';
export const imageArtifact = new Artifact({
kind: 'image',
description: 'Useful for image generation',
onStreamPart: ({ streamPart, setArtifact }) => {
if (streamPart.type === 'image-delta') {
setArtifact((draftArtifact) => ({
...draftArtifact,
content: streamPart.content as string,
isVisible: true,
status: 'streaming',
}));
}
},
content: ImageEditor,
actions: [
{
icon: <UndoIcon size={18} />,
description: 'View Previous version',
onClick: ({ handleVersionChange }) => {
handleVersionChange('prev');
},
isDisabled: ({ currentVersionIndex }) => {
if (currentVersionIndex === 0) {
return true;
}
return false;
},
},
{
icon: <RedoIcon size={18} />,
description: 'View Next version',
onClick: ({ handleVersionChange }) => {
handleVersionChange('next');
},
isDisabled: ({ isCurrentVersion }) => {
if (isCurrentVersion) {
return true;
}
return false;
},
},
{
icon: <CopyIcon size={18} />,
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');
canvas.width = img.width;
canvas.height = img.height;
const ctx = canvas.getContext('2d');
ctx?.drawImage(img, 0, 0);
canvas.toBlob((blob) => {
if (blob) {
navigator.clipboard.write([
new ClipboardItem({ 'image/png': blob }),
]);
}
}, 'image/png');
};
toast.success('Copied image to clipboard!');
},
},
],
toolbar: [],
});

43
artifacts/image/server.ts Normal file
View file

@ -0,0 +1,43 @@
import { myProvider } from '@/lib/ai/models';
import { createDocumentHandler } from '@/lib/artifacts/server';
import { experimental_generateImage } from 'ai';
export const imageDocumentHandler = createDocumentHandler<'image'>({
kind: 'image',
onCreateDocument: async ({ title, dataStream }) => {
let draftContent = '';
const { image } = await experimental_generateImage({
model: myProvider.imageModel('small-model'),
prompt: title,
n: 1,
});
draftContent = image.base64;
dataStream.writeData({
type: 'image-delta',
content: image.base64,
});
return draftContent;
},
onUpdateDocument: async ({ description, dataStream }) => {
let draftContent = '';
const { image } = await experimental_generateImage({
model: myProvider.imageModel('small-model'),
prompt: description,
n: 1,
});
draftContent = image.base64;
dataStream.writeData({
type: 'image-delta',
content: image.base64,
});
return draftContent;
},
});

115
artifacts/sheet/client.tsx Normal file
View file

@ -0,0 +1,115 @@
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';
type Metadata = any;
export const sheetArtifact = new Artifact<'sheet', Metadata>({
kind: 'sheet',
description: 'Useful for working with spreadsheets',
initialize: async () => {},
onStreamPart: ({ setArtifact, streamPart }) => {
if (streamPart.type === 'sheet-delta') {
setArtifact((draftArtifact) => ({
...draftArtifact,
content: streamPart.content as string,
isVisible: true,
status: 'streaming',
}));
}
},
content: ({
content,
currentVersionIndex,
isCurrentVersion,
onSaveContent,
status,
}) => {
return (
<SpreadsheetEditor
content={content}
currentVersionIndex={currentVersionIndex}
isCurrentVersion={isCurrentVersion}
saveContent={onSaveContent}
status={status}
/>
);
},
actions: [
{
icon: <UndoIcon size={18} />,
description: 'View Previous version',
onClick: ({ handleVersionChange }) => {
handleVersionChange('prev');
},
isDisabled: ({ currentVersionIndex }) => {
if (currentVersionIndex === 0) {
return true;
}
return false;
},
},
{
icon: <RedoIcon size={18} />,
description: 'View Next version',
onClick: ({ handleVersionChange }) => {
handleVersionChange('next');
},
isDisabled: ({ isCurrentVersion }) => {
if (isCurrentVersion) {
return true;
}
return false;
},
},
{
icon: <CopyIcon />,
description: 'Copy as .csv',
onClick: ({ content }) => {
const parsed = parse<string[]>(content, { skipEmptyLines: true });
const nonEmptyRows = parsed.data.filter((row) =>
row.some((cell) => cell.trim() !== ''),
);
const cleanedCsv = unparse(nonEmptyRows);
navigator.clipboard.writeText(cleanedCsv);
toast.success('Copied csv to clipboard!');
},
},
],
toolbar: [
{
description: 'Format and clean data',
icon: <SparklesIcon />,
onClick: ({ appendMessage }) => {
appendMessage({
role: 'user',
content: 'Can you please format and clean the data?',
});
},
},
{
description: 'Analyze and visualize data',
icon: <LineChartIcon />,
onClick: ({ appendMessage }) => {
appendMessage({
role: 'user',
content:
'Can you please analyze and visualize the data by creating a new code artifact in python?',
});
},
},
],
});

78
artifacts/sheet/server.ts Normal file
View file

@ -0,0 +1,78 @@
import { myProvider } from '@/lib/ai/models';
import { sheetPrompt, updateDocumentPrompt } from '@/lib/ai/prompts';
import { createDocumentHandler } from '@/lib/artifacts/server';
import { streamObject } from 'ai';
import { z } from 'zod';
export const sheetDocumentHandler = createDocumentHandler<'sheet'>({
kind: 'sheet',
onCreateDocument: async ({ title, dataStream }) => {
let draftContent = '';
const { fullStream } = streamObject({
model: myProvider.languageModel('artifact-model'),
system: sheetPrompt,
prompt: title,
schema: z.object({
csv: z.string().describe('CSV data'),
}),
});
for await (const delta of fullStream) {
const { type } = delta;
if (type === 'object') {
const { object } = delta;
const { csv } = object;
if (csv) {
dataStream.writeData({
type: 'sheet-delta',
content: csv,
});
draftContent = csv;
}
}
}
dataStream.writeData({
type: 'sheet-delta',
content: draftContent,
});
return draftContent;
},
onUpdateDocument: async ({ document, description, dataStream }) => {
let draftContent = '';
const { fullStream } = streamObject({
model: myProvider.languageModel('artifact-model'),
system: updateDocumentPrompt(document.content, 'sheet'),
prompt: description,
schema: z.object({
csv: z.string(),
}),
});
for await (const delta of fullStream) {
const { type } = delta;
if (type === 'object') {
const { object } = delta;
const { csv } = object;
if (csv) {
dataStream.writeData({
type: 'sheet-delta',
content: csv,
});
draftContent = csv;
}
}
}
return draftContent;
},
});

178
artifacts/text/client.tsx Normal file
View file

@ -0,0 +1,178 @@
import { Artifact } from '@/components/create-artifact';
import { DiffView } from '@/components/diffview';
import { DocumentSkeleton } from '@/components/document-skeleton';
import { Editor } from '@/components/text-editor';
import {
ClockRewind,
CopyIcon,
MessageIcon,
PenIcon,
RedoIcon,
UndoIcon,
} from '@/components/icons';
import { Suggestion } from '@/lib/db/schema';
import { toast } from 'sonner';
import { getSuggestions } from '../actions';
interface TextArtifactMetadata {
suggestions: Array<Suggestion>;
}
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 });
setMetadata({
suggestions,
});
},
onStreamPart: ({ streamPart, setMetadata, setArtifact }) => {
if (streamPart.type === 'suggestion') {
setMetadata((metadata) => {
return {
suggestions: [
...metadata.suggestions,
streamPart.content as Suggestion,
],
};
});
}
if (streamPart.type === 'text-delta') {
setArtifact((draftArtifact) => {
return {
...draftArtifact,
content: draftArtifact.content + (streamPart.content as string),
isVisible:
draftArtifact.status === 'streaming' &&
draftArtifact.content.length > 400 &&
draftArtifact.content.length < 450
? true
: draftArtifact.isVisible,
status: 'streaming',
};
});
}
},
content: ({
mode,
status,
content,
isCurrentVersion,
currentVersionIndex,
onSaveContent,
getDocumentContentById,
isLoading,
metadata,
}) => {
if (isLoading) {
return <DocumentSkeleton artifactKind="text" />;
}
if (mode === 'diff') {
const oldContent = getDocumentContentById(currentVersionIndex - 1);
const newContent = getDocumentContentById(currentVersionIndex);
return <DiffView oldContent={oldContent} newContent={newContent} />;
}
return (
<>
<div className="flex flex-row py-8 md:p-20 px-4">
<Editor
content={content}
suggestions={metadata ? metadata.suggestions : []}
isCurrentVersion={isCurrentVersion}
currentVersionIndex={currentVersionIndex}
status={status}
onSaveContent={onSaveContent}
/>
{metadata &&
metadata.suggestions &&
metadata.suggestions.length > 0 ? (
<div className="md:hidden h-dvh w-12 shrink-0" />
) : null}
</div>
</>
);
},
actions: [
{
icon: <ClockRewind size={18} />,
description: 'View changes',
onClick: ({ handleVersionChange }) => {
handleVersionChange('toggle');
},
isDisabled: ({ currentVersionIndex, setMetadata }) => {
if (currentVersionIndex === 0) {
return true;
}
return false;
},
},
{
icon: <UndoIcon size={18} />,
description: 'View Previous version',
onClick: ({ handleVersionChange }) => {
handleVersionChange('prev');
},
isDisabled: ({ currentVersionIndex }) => {
if (currentVersionIndex === 0) {
return true;
}
return false;
},
},
{
icon: <RedoIcon size={18} />,
description: 'View Next version',
onClick: ({ handleVersionChange }) => {
handleVersionChange('next');
},
isDisabled: ({ isCurrentVersion }) => {
if (isCurrentVersion) {
return true;
}
return false;
},
},
{
icon: <CopyIcon size={18} />,
description: 'Copy to clipboard',
onClick: ({ content }) => {
navigator.clipboard.writeText(content);
toast.success('Copied to clipboard!');
},
},
],
toolbar: [
{
icon: <PenIcon />,
description: 'Add final polish',
onClick: ({ appendMessage }) => {
appendMessage({
role: 'user',
content:
'Please add final polish and check for grammar, add section titles for better structure, and ensure everything reads smoothly.',
});
},
},
{
icon: <MessageIcon />,
description: 'Request suggestions',
onClick: ({ appendMessage }) => {
appendMessage({
role: 'user',
content:
'Please add suggestions you have that could improve the writing.',
});
},
},
],
});

70
artifacts/text/server.ts Normal file
View file

@ -0,0 +1,70 @@
import { smoothStream, streamText } from 'ai';
import { myProvider } from '@/lib/ai/models';
import { createDocumentHandler } from '@/lib/artifacts/server';
import { updateDocumentPrompt } from '@/lib/ai/prompts';
export const textDocumentHandler = createDocumentHandler<'text'>({
kind: 'text',
onCreateDocument: async ({ title, dataStream }) => {
let draftContent = '';
const { fullStream } = streamText({
model: myProvider.languageModel('artifact-model'),
system:
'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') {
const { textDelta } = delta;
draftContent += textDelta;
dataStream.writeData({
type: 'text-delta',
content: textDelta,
});
}
}
return draftContent;
},
onUpdateDocument: async ({ document, description, dataStream }) => {
let draftContent = '';
const { fullStream } = streamText({
model: myProvider.languageModel('artifact-model'),
system: updateDocumentPrompt(document.content, 'text'),
experimental_transform: smoothStream({ chunking: 'word' }),
prompt: description,
experimental_providerMetadata: {
openai: {
prediction: {
type: 'content',
content: document.content,
},
},
},
});
for await (const delta of fullStream) {
const { type } = delta;
if (type === 'text-delta') {
const { textDelta } = delta;
draftContent += textDelta;
dataStream.writeData({
type: 'text-delta',
content: textDelta,
});
}
}
return draftContent;
},
});