chore: update to ai sdk v5 beta (#1074)
This commit is contained in:
parent
7d8e71383f
commit
4c281fe09d
54 changed files with 1372 additions and 1060 deletions
|
|
@ -1,28 +1,13 @@
|
|||
'use client';
|
||||
|
||||
import { useChat } from '@ai-sdk/react';
|
||||
import { useEffect, useRef } from 'react';
|
||||
import { artifactDefinitions, ArtifactKind } from './artifact';
|
||||
import { Suggestion } from '@/lib/db/schema';
|
||||
import { artifactDefinitions } from './artifact';
|
||||
import { initialArtifactData, useArtifact } from '@/hooks/use-artifact';
|
||||
import { useDataStream } from './data-stream-provider';
|
||||
|
||||
export type DataStreamDelta = {
|
||||
type:
|
||||
| 'text-delta'
|
||||
| 'code-delta'
|
||||
| 'sheet-delta'
|
||||
| 'image-delta'
|
||||
| 'title'
|
||||
| 'id'
|
||||
| 'suggestion'
|
||||
| 'clear'
|
||||
| 'finish'
|
||||
| 'kind';
|
||||
content: string | Suggestion;
|
||||
};
|
||||
export function DataStreamHandler() {
|
||||
const { dataStream } = useDataStream();
|
||||
|
||||
export function DataStreamHandler({ id }: { id: string }) {
|
||||
const { data: dataStream } = useChat({ id });
|
||||
const { artifact, setArtifact, setMetadata } = useArtifact();
|
||||
const lastProcessedIndex = useRef(-1);
|
||||
|
||||
|
|
@ -32,7 +17,7 @@ export function DataStreamHandler({ id }: { id: string }) {
|
|||
const newDeltas = dataStream.slice(lastProcessedIndex.current + 1);
|
||||
lastProcessedIndex.current = dataStream.length - 1;
|
||||
|
||||
(newDeltas as DataStreamDelta[]).forEach((delta: DataStreamDelta) => {
|
||||
newDeltas.forEach((delta) => {
|
||||
const artifactDefinition = artifactDefinitions.find(
|
||||
(artifactDefinition) => artifactDefinition.kind === artifact.kind,
|
||||
);
|
||||
|
|
@ -51,35 +36,35 @@ export function DataStreamHandler({ id }: { id: string }) {
|
|||
}
|
||||
|
||||
switch (delta.type) {
|
||||
case 'id':
|
||||
case 'data-id':
|
||||
return {
|
||||
...draftArtifact,
|
||||
documentId: delta.content as string,
|
||||
documentId: delta.data,
|
||||
status: 'streaming',
|
||||
};
|
||||
|
||||
case 'title':
|
||||
case 'data-title':
|
||||
return {
|
||||
...draftArtifact,
|
||||
title: delta.content as string,
|
||||
title: delta.data,
|
||||
status: 'streaming',
|
||||
};
|
||||
|
||||
case 'kind':
|
||||
case 'data-kind':
|
||||
return {
|
||||
...draftArtifact,
|
||||
kind: delta.content as ArtifactKind,
|
||||
kind: delta.data,
|
||||
status: 'streaming',
|
||||
};
|
||||
|
||||
case 'clear':
|
||||
case 'data-clear':
|
||||
return {
|
||||
...draftArtifact,
|
||||
content: '',
|
||||
status: 'streaming',
|
||||
};
|
||||
|
||||
case 'finish':
|
||||
case 'data-finish':
|
||||
return {
|
||||
...draftArtifact,
|
||||
status: 'idle',
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue