@@ -82,6 +82,15 @@ class VertexAI extends BaseLLM {
82
82
83
83
// Remove the `model` property and add `anthropic_version`
84
84
const { model, ...finalOptions } = convertedArgs ;
85
+
86
+ // Add tool support - convert tools to Anthropic's format
87
+ if ( options . tools ) {
88
+ finalOptions . tools = options . tools . map ( tool => ( {
89
+ name : tool . function . name ,
90
+ description : tool . function ?. description ,
91
+ input_schema : tool . function ?. parameters
92
+ } ) ) ;
93
+ }
85
94
return {
86
95
...finalOptions ,
87
96
anthropic_version : "vertex-2023-10-16" ,
@@ -123,25 +132,88 @@ class VertexAI extends BaseLLM {
123
132
} ,
124
133
]
125
134
: systemMessage ,
135
+ // Add tools if they exist in options
136
+ ...( options . tools ? {
137
+ tools : options . tools . map ( tool => ( {
138
+ name : tool . function . name ,
139
+ description : tool . function . description ,
140
+ input_schema : tool . function . parameters
141
+ } ) )
142
+ } : { } )
126
143
} ) ,
127
144
} ) ;
128
145
129
146
if ( options . stream === false ) {
130
147
const data = await response . json ( ) ;
131
- yield { role : "assistant" , content : data . content [ 0 ] . text } ;
148
+
149
+ // Check if the response contains a tool call
150
+ if ( data . content && data . content . length > 0 ) {
151
+ const contentItem = data . content [ 0 ] ;
152
+
153
+ if ( contentItem . type === "tool_use" ) {
154
+ // Handle tool calls in non-streaming mode
155
+ yield {
156
+ role : "assistant" ,
157
+ content : "" ,
158
+ toolCalls : [ {
159
+ id : contentItem . id || `call_${ Date . now ( ) } ` ,
160
+ type : "function" ,
161
+ function : {
162
+ name : contentItem . name ,
163
+ arguments : JSON . stringify ( contentItem . input )
164
+ }
165
+ } ]
166
+ } ;
167
+ } else {
168
+ // Regular text response
169
+ yield { role : "assistant" , content : contentItem . text || "" } ;
170
+ }
171
+ }
132
172
return ;
133
173
}
134
174
175
+ // For streaming responses
176
+ let currentToolCall = null ;
177
+
135
178
for await ( const value of streamSse ( response ) ) {
136
- if ( value . type === "message_start" ) {
137
- console . log ( value ) ;
138
- }
139
- if ( value . delta ?. text ) {
179
+ if ( value . type === "content_block_start" ) {
180
+ if ( value . content_block ?. type === "tool_use" ) {
181
+ // Initialize a new tool call
182
+ currentToolCall = {
183
+ id : value . content_block . id || `call_${ Date . now ( ) } ` ,
184
+ name : value . content_block . name ,
185
+ input : { }
186
+ } ;
187
+ }
188
+ } else if ( value . type === "content_block_delta" && currentToolCall ) {
189
+ // Update the tool call with new input data
190
+ if ( value . delta ?. input ) {
191
+ currentToolCall . input = {
192
+ ...currentToolCall . input ,
193
+ ...value . delta . input
194
+ } ;
195
+ }
196
+ } else if ( value . type === "content_block_stop" && currentToolCall ) {
197
+ // Finalize and yield the complete tool call
198
+ yield {
199
+ role : "assistant" ,
200
+ content : "" ,
201
+ toolCalls : [ {
202
+ id : currentToolCall . id ,
203
+ type : "function" ,
204
+ function : {
205
+ name : currentToolCall . name ,
206
+ arguments : JSON . stringify ( currentToolCall . input )
207
+ }
208
+ } ]
209
+ } ;
210
+ currentToolCall = null ;
211
+ } else if ( value . delta ?. text ) {
212
+ // Regular text response
140
213
yield { role : "assistant" , content : value . delta . text } ;
141
214
}
142
215
}
143
216
}
144
-
145
217
//Gemini
146
218
147
219
private async * streamChatGemini (
0 commit comments