Skip to content

Commit

Permalink
refactor: improve block modularity on server (#758)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyphilemon authored Feb 4, 2025
1 parent 711da0b commit 9c4dbc8
Show file tree
Hide file tree
Showing 14 changed files with 419 additions and 259 deletions.
2 changes: 1 addition & 1 deletion app/(chat)/api/chat/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export async function POST(request: Request) {
sendReasoning: true,
});
},
onError: (error) => {
onError: () => {
return 'Oops, an error occured!';
},
});
Expand Down
File renamed without changes.
73 changes: 73 additions & 0 deletions blocks/code/server.ts
Original file line number Diff line number Diff line change
@@ -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/blocks/server';

export const codeDocumentHandler = createDocumentHandler<'code'>({
kind: 'code',
onCreateDocument: async ({ title, dataStream }) => {
let draftContent = '';

const { fullStream } = streamObject({
model: myProvider.languageModel('block-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('block-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;
},
});
File renamed without changes.
54 changes: 54 additions & 0 deletions blocks/image/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { myProvider } from '@/lib/ai/models';
import { createDocumentHandler } from '@/lib/blocks/server';
import { saveDocument } from '@/lib/db/queries';
import { experimental_generateImage } from 'ai';

export const imageDocumentHandler = createDocumentHandler<'image'>({
kind: 'image',
onCreateDocument: async ({ id, title, dataStream, session }) => {
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,
});

if (session?.user?.id) {
await saveDocument({
id,
title,
kind: 'image',
content: draftContent,
userId: session.user.id,
});
}

return draftContent;
},
onUpdateDocument: async ({ description, dataStream }) => {
let draftContent = '';

const { image } = await experimental_generateImage({
model: myProvider.imageModel('image-model'),
prompt: description,
n: 1,
});

draftContent = image.base64;

dataStream.writeData({
type: 'image-delta',
content: image.base64,
});

return draftContent;
},
});
File renamed without changes.
78 changes: 78 additions & 0 deletions blocks/sheet/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { myProvider } from '@/lib/ai/models';
import { sheetPrompt, updateDocumentPrompt } from '@/lib/ai/prompts';
import { createDocumentHandler } from '@/lib/blocks/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('block-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('block-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;
},
});
2 changes: 1 addition & 1 deletion blocks/text.tsx → blocks/text/client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from '@/components/icons';
import { Suggestion } from '@/lib/db/schema';
import { toast } from 'sonner';
import { getSuggestions } from './actions';
import { getSuggestions } from '../actions';

interface TextBlockMetadata {
suggestions: Array<Suggestion>;
Expand Down
70 changes: 70 additions & 0 deletions blocks/text/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { smoothStream, streamText } from 'ai';
import { myProvider } from '@/lib/ai/models';
import { createDocumentHandler } from '@/lib/blocks/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('block-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('block-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;
},
});
1 change: 1 addition & 0 deletions components/block-actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export const BlockActions = memo(PureBlockActions, (prevProps, nextProps) => {
if (prevProps.currentVersionIndex !== nextProps.currentVersionIndex)
return false;
if (prevProps.isCurrentVersion !== nextProps.isCurrentVersion) return false;
if (prevProps.block.content !== nextProps.block.content) return false;

return true;
});
8 changes: 4 additions & 4 deletions components/block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ import { BlockCloseButton } from './block-close-button';
import { BlockMessages } from './block-messages';
import { useSidebar } from './ui/sidebar';
import { useBlock } from '@/hooks/use-block';
import { textBlock } from '@/blocks/text';
import { imageBlock } from '@/blocks/image';
import { codeBlock } from '@/blocks/code';
import { imageBlock } from '@/blocks/image/client';
import { codeBlock } from '@/blocks/code/client';
import { sheetBlock } from '@/blocks/sheet/client';
import { textBlock } from '@/blocks/text/client';
import equal from 'fast-deep-equal';
import { sheetBlock } from '@/blocks/sheet';

export const blockDefinitions = [textBlock, codeBlock, imageBlock, sheetBlock];
export type BlockKind = (typeof blockDefinitions)[number]['kind'];
Expand Down
Loading

0 comments on commit 9c4dbc8

Please sign in to comment.