Skip to content

Commit a3db073

Browse files
clean artifact concern
1 parent 5bad605 commit a3db073

File tree

1 file changed

+45
-34
lines changed
  • templates/types/streaming/nextjs/app/components/ui/chat/tools

1 file changed

+45
-34
lines changed
Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,40 @@
11
import {
2-
getAnnotationData,
2+
Message,
33
MessageAnnotation,
4-
Message as RawMessage,
5-
useChatMessage,
4+
getAnnotationData,
65
useChatUI,
76
} from "@llamaindex/chat-ui";
8-
import { JSONValue, Message } from "ai";
7+
import { JSONValue } from "ai";
98
import { useMemo } from "react";
109
import { Artifact, CodeArtifact } from "./artifact";
1110
import { WeatherCard, WeatherData } from "./weather-card";
1211

13-
export function ToolAnnotations({ message }: { message: RawMessage }) {
12+
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
1421
const annotations = message.annotations as MessageAnnotation[] | undefined;
1522
const toolData = annotations
1623
? (getAnnotationData(annotations, "tools") as unknown as ToolData[])
1724
: null;
18-
return toolData?.[0] ? <ChatTools data={toolData[0]} /> : null;
25+
return toolData?.[0] ? (
26+
<ChatTools data={toolData[0]} artifactVersion={artifactVersion} />
27+
) : null;
1928
}
2029

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

@@ -67,7 +56,7 @@ function ChatTools({ data }: { data: ToolData }) {
6756
return (
6857
<Artifact
6958
artifact={toolOutput.output as CodeArtifact}
70-
version={artifactVersionMap.get((message as Message).id)}
59+
version={artifactVersion}
7160
/>
7261
);
7362
default:
@@ -88,3 +77,25 @@ type ToolData = {
8877
isError: boolean;
8978
};
9079
};
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")
91+
: null;
92+
93+
if (toolData?.some((t: any) => t.toolCall.name === "artifact")) {
94+
if (m.id === messageId) {
95+
return versionIndex;
96+
}
97+
versionIndex++;
98+
}
99+
}
100+
return undefined;
101+
}

0 commit comments

Comments
 (0)