refactor: improve block modularity on server (#758)
This commit is contained in:
parent
711da0b94b
commit
9c4dbc8aaa
14 changed files with 419 additions and 259 deletions
73
blocks/code/server.ts
Normal file
73
blocks/code/server.ts
Normal 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;
|
||||
},
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue