Skip to content

Commit ef7a5b3

Browse files
more fixes
1 parent 6e531d6 commit ef7a5b3

File tree

15 files changed

+72
-101
lines changed

15 files changed

+72
-101
lines changed

app/(chat)/chat/[id]/page.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import { auth } from '@/app/(auth)/auth';
44
import { Chat } from '@/components/chat';
55
import { getChatById, getMessagesByChatId } from '@/lib/db/queries';
66
import { DEFAULT_CHAT_MODEL } from '@/lib/ai/models';
7-
import type { DBMessage } from '@/lib/db/schema';
87
import type { UIMessage } from 'ai';
98
import type { Attachment, ChatMessage } from '@/lib/types';
9+
import type { Tables } from '@/lib/db/schema';
1010

1111
export default async function Page(props: { params: Promise<{ id: string }> }) {
1212
const params = await props.params;
@@ -37,7 +37,9 @@ export default async function Page(props: { params: Promise<{ id: string }> }) {
3737
id,
3838
});
3939

40-
function convertToUIMessages(messages: Array<DBMessage>): Array<ChatMessage> {
40+
function convertToUIMessages(
41+
messages: Array<Tables<'Message'>>,
42+
): Array<ChatMessage> {
4143
// @ts-expect-error todo: fix conversion of types
4244
return messages.map((message) => ({
4345
id: message.id,

artifacts/code/client.tsx

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -75,21 +75,7 @@ export const codeArtifact = new Artifact<'code', Metadata>({
7575
outputs: [],
7676
});
7777
},
78-
onStreamPart: ({ streamPart, setArtifact }) => {
79-
if (streamPart.type === 'data-document') {
80-
setArtifact((draftArtifact) => ({
81-
...draftArtifact,
82-
content: streamPart.data as string,
83-
isVisible:
84-
draftArtifact.status === 'streaming' &&
85-
draftArtifact.content.length > 300 &&
86-
draftArtifact.content.length < 310
87-
? true
88-
: draftArtifact.isVisible,
89-
status: 'streaming',
90-
}));
91-
}
92-
},
78+
onStreamPart: ({ streamPart, setArtifact }) => {},
9379
content: ({ metadata, setMetadata, ...props }) => {
9480
return (
9581
<>

artifacts/code/server.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ export const codeDocumentHandler = createDocumentHandler<'code'>({
2727

2828
if (code) {
2929
streamWriter.write({
30-
type: 'data-artifacts-code-delta',
31-
data: code ?? '',
30+
type: 'data-document',
31+
data: {
32+
content: code,
33+
},
3234
});
3335

3436
draftContent = code;
@@ -59,8 +61,10 @@ export const codeDocumentHandler = createDocumentHandler<'code'>({
5961

6062
if (code) {
6163
streamWriter.write({
62-
type: 'data-artifacts-code-delta',
63-
data: code ?? '',
64+
type: 'data-document',
65+
data: {
66+
content: code,
67+
},
6468
});
6569

6670
draftContent = code;

artifacts/image/client.tsx

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,7 @@ import { toast } from 'sonner';
66
export const imageArtifact = new Artifact({
77
kind: 'image',
88
description: 'Useful for image generation',
9-
onStreamPart: ({ streamPart, setArtifact }) => {
10-
if (streamPart.type === 'data-artifacts-image-delta') {
11-
setArtifact((draftArtifact) => ({
12-
...draftArtifact,
13-
content: streamPart.data as string,
14-
isVisible: true,
15-
status: 'streaming',
16-
}));
17-
}
18-
},
9+
onStreamPart: ({ streamPart, setArtifact }) => {},
1910
content: ImageEditor,
2011
actions: [
2112
{

artifacts/image/server.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ export const imageDocumentHandler = createDocumentHandler<'image'>({
1616
draftContent = image.base64;
1717

1818
streamWriter.write({
19-
type: 'data-artifacts-image-delta',
20-
data: image.base64,
19+
type: 'data-document',
20+
data: {
21+
content: image.base64,
22+
},
2123
});
2224

2325
return draftContent;
@@ -34,8 +36,10 @@ export const imageDocumentHandler = createDocumentHandler<'image'>({
3436
draftContent = image.base64;
3537

3638
streamWriter.write({
37-
type: 'data-artifacts-image-delta',
38-
data: image.base64,
39+
type: 'data-document',
40+
data: {
41+
content: image.base64,
42+
},
3943
});
4044

4145
return draftContent;

artifacts/sheet/client.tsx

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,7 @@ export const sheetArtifact = new Artifact<'sheet', Metadata>({
1616
kind: 'sheet',
1717
description: 'Useful for working with spreadsheets',
1818
initialize: async () => {},
19-
onStreamPart: ({ setArtifact, streamPart }) => {
20-
if (streamPart.type === 'data-artifacts-sheet-delta') {
21-
setArtifact((draftArtifact) => ({
22-
...draftArtifact,
23-
content: streamPart.data as string,
24-
isVisible: true,
25-
status: 'streaming',
26-
}));
27-
}
28-
},
19+
onStreamPart: ({ setArtifact, streamPart }) => {},
2920
content: ({
3021
content,
3122
currentVersionIndex,

artifacts/sheet/server.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ export const sheetDocumentHandler = createDocumentHandler<'sheet'>({
2727

2828
if (csv) {
2929
streamWriter.write({
30-
type: 'data-artifacts-sheet-delta',
31-
data: csv,
30+
type: 'data-document',
31+
data: {
32+
content: csv,
33+
},
3234
});
3335

3436
draftContent = csv;
@@ -37,8 +39,10 @@ export const sheetDocumentHandler = createDocumentHandler<'sheet'>({
3739
}
3840

3941
streamWriter.write({
40-
type: 'data-artifacts-sheet-delta',
41-
data: draftContent,
42+
type: 'data-document',
43+
data: {
44+
content: draftContent,
45+
},
4246
});
4347

4448
return draftContent;
@@ -64,8 +68,10 @@ export const sheetDocumentHandler = createDocumentHandler<'sheet'>({
6468

6569
if (csv) {
6670
streamWriter.write({
67-
type: 'data-artifacts-sheet-delta',
68-
data: csv,
71+
type: 'data-document',
72+
data: {
73+
content: csv,
74+
},
6975
});
7076

7177
draftContent = csv;

artifacts/text/client.tsx

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,30 +30,6 @@ export const textArtifact = new Artifact<'text', TextArtifactMetadata>({
3030
},
3131
onStreamPart: ({ streamPart, setMetadata, setArtifact }) => {
3232
const { type, data } = streamPart;
33-
34-
// if (type === 'data-artifacts-suggestion') {
35-
// setMetadata((metadata) => {
36-
// return {
37-
// suggestions: [...metadata.suggestions, data as Suggestion],
38-
// };
39-
// });
40-
// }
41-
42-
if (type === 'data-document') {
43-
setArtifact((draftArtifact) => {
44-
return {
45-
...draftArtifact,
46-
content: draftArtifact.content + data.content,
47-
isVisible:
48-
draftArtifact.status === 'streaming' &&
49-
draftArtifact.content.length > 400 &&
50-
draftArtifact.content.length < 450
51-
? true
52-
: draftArtifact.isVisible,
53-
status: 'streaming',
54-
};
55-
});
56-
}
5733
},
5834
content: ({
5935
mode,

components/artifact-actions.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { Button } from './ui/button';
22
import { Tooltip, TooltipContent, TooltipTrigger } from './ui/tooltip';
3-
import { artifactDefinitions, UIArtifact } from './artifact';
4-
import { Dispatch, memo, SetStateAction, useState } from 'react';
5-
import { ArtifactActionContext } from './create-artifact';
3+
import { artifactDefinitions } from './artifact';
4+
import { type Dispatch, memo, type SetStateAction, useState } from 'react';
5+
import type { ArtifactActionContext } from './create-artifact';
66
import { cn } from '@/lib/utils';
77
import { toast } from 'sonner';
8+
import type { Document } from '@/lib/types';
89

910
interface ArtifactActionsProps {
10-
artifact: UIArtifact;
11+
artifact: Document;
1112
handleVersionChange: (type: 'next' | 'prev' | 'toggle' | 'latest') => void;
1213
currentVersionIndex: number;
1314
isCurrentVersion: boolean;
@@ -68,7 +69,7 @@ function PureArtifactActions({
6869
}
6970
}}
7071
disabled={
71-
isLoading || artifact.status === 'streaming'
72+
isLoading || artifact.status === 'in_progress'
7273
? true
7374
: action.isDisabled
7475
? action.isDisabled(actionContext)

components/artifact-messages.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ import { PreviewMessage } from './message';
22
import type { Vote } from '@/lib/db/schema';
33
import { memo } from 'react';
44
import equal from 'fast-deep-equal';
5-
import type { UIArtifact } from './artifact';
65
import type { UseChatHelpers } from '@ai-sdk/react';
76
import { motion } from 'framer-motion';
87
import { useMessages } from '@/hooks/use-messages';
9-
import type { ChatMessage } from '@/lib/types';
8+
import type { ChatMessage, Document } from '@/lib/types';
109
import { ThinkingMessage } from './message-thinking';
1110

1211
interface ArtifactMessagesProps {
@@ -17,7 +16,7 @@ interface ArtifactMessagesProps {
1716
setMessages: UseChatHelpers<ChatMessage>['setMessages'];
1817
regenerate: UseChatHelpers<ChatMessage>['regenerate'];
1918
isReadonly: boolean;
20-
artifactStatus: UIArtifact['status'];
19+
artifactStatus: Document['status'];
2120
}
2221

2322
function PureArtifactMessages({
@@ -84,8 +83,8 @@ function areEqual(
8483
nextProps: ArtifactMessagesProps,
8584
) {
8685
if (
87-
prevProps.artifactStatus === 'streaming' &&
88-
nextProps.artifactStatus === 'streaming'
86+
prevProps.artifactStatus === 'in_progress' &&
87+
nextProps.artifactStatus === 'in_progress'
8988
)
9089
return true;
9190

components/document-skeleton.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
'use client';
22

3-
import { ArtifactKind } from './artifact';
3+
import type { DocumentKind } from '@/lib/types';
44

55
export const DocumentSkeleton = ({
66
artifactKind,
77
}: {
8-
artifactKind: ArtifactKind;
8+
artifactKind: DocumentKind;
99
}) => {
1010
return artifactKind === 'image' ? (
1111
<div className="flex flex-col gap-4 w-full justify-center items-center h-[calc(100dvh-60px)]">

components/document.tsx

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { memo } from 'react';
22

3-
import type { ArtifactKind } from './artifact';
43
import { FileIcon, LoaderIcon, MessageIcon, PencilEditIcon } from './icons';
54
import { toast } from 'sonner';
65
import { useArtifact } from '@/hooks/use-artifact';
6+
import type { DocumentKind } from '@/lib/types';
77

88
const getActionText = (
99
type: 'create' | 'update' | 'request-suggestions',
@@ -25,7 +25,7 @@ const getActionText = (
2525

2626
interface DocumentToolResultProps {
2727
type: 'create' | 'update' | 'request-suggestions';
28-
result: { id: string; title: string; kind: ArtifactKind };
28+
result: { id: string; title: string; kind: DocumentKind };
2929
isReadonly: boolean;
3030
}
3131

@@ -119,12 +119,6 @@ function PureDocumentToolCall({
119119
width: rect.width,
120120
height: rect.height,
121121
};
122-
123-
setArtifact((currentArtifact) => ({
124-
...currentArtifact,
125-
isVisible: true,
126-
boundingBox,
127-
}));
128122
}}
129123
>
130124
<div className="flex flex-row gap-3 items-start">

components/suggestion.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type { UISuggestion } from '@/lib/editor/suggestions';
99
import { CrossIcon, MessageIcon } from './icons';
1010
import { Button } from './ui/button';
1111
import { cn } from '@/lib/utils';
12-
import { ArtifactKind } from './artifact';
12+
import type { DocumentKind } from '@/lib/types';
1313

1414
export const Suggestion = ({
1515
suggestion,
@@ -18,7 +18,7 @@ export const Suggestion = ({
1818
}: {
1919
suggestion: UISuggestion;
2020
onApply: () => void;
21-
artifactKind: ArtifactKind;
21+
artifactKind: DocumentKind;
2222
}) => {
2323
const [isExpanded, setIsExpanded] = useState(false);
2424
const { width: windowWidth } = useWindowSize();

components/toolbar.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ import {
2424
TooltipTrigger,
2525
} from '@/components/ui/tooltip';
2626
import { ArrowUpIcon, StopIcon, SummarizeIcon } from './icons';
27-
import { artifactDefinitions, type ArtifactKind } from './artifact';
27+
import { artifactDefinitions } from './artifact';
2828
import type { ArtifactToolbarItem } from './create-artifact';
2929
import type { UseChatHelpers } from '@ai-sdk/react';
30-
import type { ChatMessage } from '@/lib/types';
30+
import type { ChatMessage, DocumentKind } from '@/lib/types';
3131

3232
type ToolProps = {
3333
description: string;
@@ -314,7 +314,7 @@ const PureToolbar = ({
314314
sendMessage: UseChatHelpers<ChatMessage>['sendMessage'];
315315
stop: UseChatHelpers<ChatMessage>['stop'];
316316
setMessages: UseChatHelpers<ChatMessage>['setMessages'];
317-
artifactKind: ArtifactKind;
317+
artifactKind: DocumentKind;
318318
}) => {
319319
const toolbarRef = useRef<HTMLDivElement>(null);
320320
const timeoutRef = useRef<ReturnType<typeof setTimeout>>();

hooks/use-document.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { ChatMessage, Document } from '@/lib/types';
22
import { useChat, type UseChatHelpers } from '@ai-sdk/react';
33
import { useEffect, useMemo } from 'react';
4+
import useSWR from 'swr';
45

56
export const useRecentDocumentPart = ({
67
chatId,
@@ -32,5 +33,21 @@ export const useRecentDocumentPart = ({
3233
return recentDocumentPart.data;
3334
}, [messages]);
3435

35-
return { recentDocumentPart };
36+
const { data: localDocumentMetadata, mutate: setLocalDocumentMetadata } =
37+
useSWR<any>(
38+
() =>
39+
recentDocumentPart?.id
40+
? `document-metadata-${recentDocumentPart.id}`
41+
: null,
42+
null,
43+
{
44+
fallbackData: null,
45+
},
46+
);
47+
48+
return {
49+
recentDocumentPart,
50+
metadata: localDocumentMetadata,
51+
setMetadata: setLocalDocumentMetadata,
52+
};
3653
};

0 commit comments

Comments
 (0)