Skip to content

Commit 6925676

Browse files
feat: use latest chat UI (#418)
--------- Co-authored-by: Marcus Schiesser <[email protected]>
1 parent 44b34fb commit 6925676

File tree

8 files changed

+64
-118
lines changed

8 files changed

+64
-118
lines changed

.changeset/wet-pears-matter.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"create-llama": patch
3+
---
4+
5+
feat: use latest chat UI

templates/types/streaming/nextjs/app/components/ui/chat/chat-input.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use client";
22

33
import { ChatInput, useChatUI, useFile } from "@llamaindex/chat-ui";
4-
import { DocumentPreview, ImagePreview } from "@llamaindex/chat-ui/widgets";
4+
import { DocumentInfo, ImagePreview } from "@llamaindex/chat-ui/widgets";
55
import { LlamaCloudSelector } from "./custom/llama-cloud-selector";
66
import { useClientConfig } from "./hooks/use-config";
77

@@ -56,9 +56,10 @@ export default function CustomChatInput() {
5656
{files.length > 0 && (
5757
<div className="flex gap-4 w-full overflow-auto py-2">
5858
{files.map((file) => (
59-
<DocumentPreview
59+
<DocumentInfo
6060
key={file.id}
61-
file={file}
61+
document={{ url: file.url, sources: [] }}
62+
className="mb-2 mt-2"
6263
onRemove={() => removeDoc(file)}
6364
/>
6465
))}

templates/types/streaming/nextjs/app/components/ui/chat/chat-message-content.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import {
33
ContentPosition,
44
getSourceAnnotationData,
55
useChatMessage,
6+
useChatUI,
67
} from "@llamaindex/chat-ui";
78
import { Markdown } from "./custom/markdown";
89
import { ToolAnnotations } from "./tools/chat-tools";
910

1011
export function ChatMessageContent() {
12+
const { isLoading, append } = useChatUI();
1113
const { message } = useChatMessage();
1214
const customContent = [
1315
{
@@ -26,5 +28,11 @@ export function ChatMessageContent() {
2628
component: <ToolAnnotations message={message} />,
2729
},
2830
];
29-
return <ChatMessage.Content content={customContent} />;
31+
return (
32+
<ChatMessage.Content
33+
content={customContent}
34+
isLoading={isLoading}
35+
append={append}
36+
/>
37+
);
3038
}

templates/types/streaming/nextjs/app/components/ui/chat/chat-messages.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export default function CustomChatMessages() {
1212
<ChatMessages.List>
1313
{messages.map((message, index) => (
1414
<ChatMessage
15-
key={message.id}
15+
key={index}
1616
message={message}
1717
isLast={index === messages.length - 1}
1818
>
Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,40 @@
11
import {
2-
getAnnotationData,
2+
Message,
33
MessageAnnotation,
4-
useChatMessage,
4+
getAnnotationData,
55
useChatUI,
66
} from "@llamaindex/chat-ui";
7-
import { JSONValue, Message } from "ai";
7+
import { JSONValue } from "ai";
88
import { useMemo } from "react";
99
import { Artifact, CodeArtifact } from "./artifact";
1010
import { WeatherCard, WeatherData } from "./weather-card";
1111

1212
export function ToolAnnotations({ message }: { message: Message }) {
13+
// TODO: This is a bit of a hack to get the artifact version. better to generate the version in the tool call and
14+
// store it in CodeArtifact
15+
const { messages } = useChatUI();
16+
const artifactVersion = useMemo(
17+
() => getArtifactVersion(messages, message),
18+
[messages, message],
19+
);
20+
// Get the tool data from the message annotations
1321
const annotations = message.annotations as MessageAnnotation[] | undefined;
1422
const toolData = annotations
1523
? (getAnnotationData(annotations, "tools") as unknown as ToolData[])
1624
: null;
17-
return toolData?.[0] ? <ChatTools data={toolData[0]} /> : null;
25+
return toolData?.[0] ? (
26+
<ChatTools data={toolData[0]} artifactVersion={artifactVersion} />
27+
) : null;
1828
}
1929

2030
// TODO: Used to render outputs of tools. If needed, add more renderers here.
21-
function ChatTools({ data }: { data: ToolData }) {
22-
const { messages } = useChatUI();
23-
const { message } = useChatMessage();
24-
25-
// build a map of message id to artifact version
26-
const artifactVersionMap = useMemo(() => {
27-
const map = new Map<string, number | undefined>();
28-
let versionIndex = 1;
29-
messages.forEach((m) => {
30-
m.annotations?.forEach((annotation: any) => {
31-
if (
32-
typeof annotation === "object" &&
33-
annotation != null &&
34-
"type" in annotation &&
35-
annotation.type === "tools"
36-
) {
37-
const data = annotation.data as ToolData;
38-
if (data?.toolCall?.name === "artifact") {
39-
map.set(m.id, versionIndex);
40-
versionIndex++;
41-
}
42-
}
43-
});
44-
});
45-
return map;
46-
}, [messages]);
47-
31+
function ChatTools({
32+
data,
33+
artifactVersion,
34+
}: {
35+
data: ToolData;
36+
artifactVersion: number | undefined;
37+
}) {
4838
if (!data) return null;
4939
const { toolCall, toolOutput } = data;
5040

@@ -66,7 +56,7 @@ function ChatTools({ data }: { data: ToolData }) {
6656
return (
6757
<Artifact
6858
artifact={toolOutput.output as CodeArtifact}
69-
version={artifactVersionMap.get(message.id)}
59+
version={artifactVersion}
7060
/>
7161
);
7262
default:
@@ -87,3 +77,25 @@ type ToolData = {
8777
isError: boolean;
8878
};
8979
};
80+
81+
function getArtifactVersion(
82+
messages: Message[],
83+
message: Message,
84+
): number | undefined {
85+
const messageId = "id" in message ? message.id : undefined;
86+
if (!messageId) return undefined;
87+
let versionIndex = 1;
88+
for (const m of messages) {
89+
const toolData = m.annotations
90+
? (getAnnotationData(m.annotations, "tools") as unknown as ToolData[])
91+
: null;
92+
93+
if (toolData?.some((t) => t.toolCall.name === "artifact")) {
94+
if ("id" in m && m.id === messageId) {
95+
return versionIndex;
96+
}
97+
versionIndex++;
98+
}
99+
}
100+
return undefined;
101+
}

templates/types/streaming/nextjs/app/layout.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { Metadata } from "next";
22
import { Inter } from "next/font/google";
33
import "./globals.css";
4-
import "./markdown.css";
54

65
const inter = Inter({ subsets: ["latin"] });
76

templates/types/streaming/nextjs/app/markdown.css

Lines changed: 0 additions & 79 deletions
This file was deleted.

templates/types/streaming/nextjs/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"@radix-ui/react-select": "^2.1.1",
1919
"@radix-ui/react-slot": "^1.0.2",
2020
"@radix-ui/react-tabs": "^1.1.0",
21-
"@llamaindex/chat-ui": "0.0.5",
21+
"@llamaindex/chat-ui": "0.0.7",
2222
"ai": "3.3.42",
2323
"ajv": "^8.12.0",
2424
"class-variance-authority": "^0.7.0",

0 commit comments

Comments
 (0)