@@ -101,15 +101,17 @@ export function toChatBody(
101101 } ;
102102
103103 if ( options . tools ?. length ) {
104- params . tools = options . tools . map ( ( tool ) => ( {
105- type : tool . type ,
106- function : {
107- name : tool . function . name ,
108- description : tool . function . description ,
109- parameters : tool . function . parameters ,
110- strict : tool . function . strict ,
111- } ,
112- } ) ) ;
104+ params . tools = options . tools
105+ . filter ( ( tool ) => ! tool . type || tool . type === "function" )
106+ . map ( ( tool ) => ( {
107+ type : tool . type ,
108+ function : {
109+ name : tool . function . name ,
110+ description : tool . function . description ,
111+ parameters : tool . function . parameters ,
112+ strict : tool . function . strict ,
113+ } ,
114+ } ) ) ;
113115 }
114116
115117 return params ;
@@ -158,7 +160,16 @@ export function fromChatResponse(response: ChatCompletion): ChatMessage {
158160 return {
159161 role : "assistant" ,
160162 content : "" ,
161- toolCalls : message . tool_calls ,
163+ toolCalls : message . tool_calls
164+ ?. filter ( ( tc ) => ! tc . type || tc . type === "function" )
165+ . map ( ( tc ) => ( {
166+ id : tc . id ,
167+ type : "function" as const ,
168+ function : {
169+ name : ( tc as any ) . function ?. name ,
170+ arguments : ( tc as any ) . function ?. arguments ,
171+ } ,
172+ } ) ) ,
162173 } ;
163174 }
164175
@@ -179,18 +190,24 @@ export function fromChatCompletionChunk(
179190 content : delta . content ,
180191 } ;
181192 } else if ( delta ?. tool_calls ) {
182- return {
183- role : "assistant" ,
184- content : "" ,
185- toolCalls : delta ?. tool_calls . map ( ( tool_call ) => ( {
193+ const toolCalls = delta ?. tool_calls
194+ . filter ( ( tool_call ) => ! tool_call . type || tool_call . type === "function" )
195+ . map ( ( tool_call ) => ( {
186196 id : tool_call . id ,
187- type : tool_call . type ,
197+ type : "function" as const ,
188198 function : {
189- name : tool_call . function ?. name ,
190- arguments : tool_call . function ?. arguments ,
199+ name : ( tool_call as any ) . function ?. name ,
200+ arguments : ( tool_call as any ) . function ?. arguments ,
191201 } ,
192- } ) ) ,
193- } ;
202+ } ) ) ;
203+
204+ if ( toolCalls . length > 0 ) {
205+ return {
206+ role : "assistant" ,
207+ content : "" ,
208+ toolCalls,
209+ } ;
210+ }
194211 }
195212
196213 return undefined ;
0 commit comments