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
70
blocks/text/server.ts
Normal file
70
blocks/text/server.ts
Normal file
|
|
@ -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;
|
||||
},
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue