refactor: improve block modularity on server (#758)

This commit is contained in:
Jeremy 2025-02-04 17:15:39 +03:00 committed by GitHub
parent 711da0b94b
commit 9c4dbc8aaa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 419 additions and 259 deletions

73
blocks/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/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;
},
});