1
1
import {
2
- getAnnotationData ,
2
+ Message ,
3
3
MessageAnnotation ,
4
- Message as RawMessage ,
5
- useChatMessage ,
4
+ getAnnotationData ,
6
5
useChatUI ,
7
6
} from "@llamaindex/chat-ui" ;
8
- import { JSONValue , Message } from "ai" ;
7
+ import { JSONValue } from "ai" ;
9
8
import { useMemo } from "react" ;
10
9
import { Artifact , CodeArtifact } from "./artifact" ;
11
10
import { WeatherCard , WeatherData } from "./weather-card" ;
12
11
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
14
21
const annotations = message . annotations as MessageAnnotation [ ] | undefined ;
15
22
const toolData = annotations
16
23
? ( getAnnotationData ( annotations , "tools" ) as unknown as ToolData [ ] )
17
24
: null ;
18
- return toolData ?. [ 0 ] ? < ChatTools data = { toolData [ 0 ] } /> : null ;
25
+ return toolData ?. [ 0 ] ? (
26
+ < ChatTools data = { toolData [ 0 ] } artifactVersion = { artifactVersion } />
27
+ ) : null ;
19
28
}
20
29
21
30
// 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
+ } ) {
49
38
if ( ! data ) return null ;
50
39
const { toolCall, toolOutput } = data ;
51
40
@@ -67,7 +56,7 @@ function ChatTools({ data }: { data: ToolData }) {
67
56
return (
68
57
< Artifact
69
58
artifact = { toolOutput . output as CodeArtifact }
70
- version = { artifactVersionMap . get ( ( message as Message ) . id ) }
59
+ version = { artifactVersion }
71
60
/>
72
61
) ;
73
62
default :
@@ -88,3 +77,25 @@ type ToolData = {
88
77
isError : boolean ;
89
78
} ;
90
79
} ;
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