@@ -57,7 +57,10 @@ export function buildResponsesInputFromMessages(messages: Message[]): ResponsesI
5757 continue
5858 }
5959
60- if ( message . content ) {
60+ if (
61+ message . content &&
62+ ( message . role === 'system' || message . role === 'user' || message . role === 'assistant' )
63+ ) {
6164 input . push ( {
6265 role : message . role ,
6366 content : message . content ,
@@ -200,6 +203,28 @@ export function convertResponseOutputToInputItems(output: unknown): ResponsesInp
200203 content : text ,
201204 } )
202205 }
206+
207+ const toolCalls = Array . isArray ( item . tool_calls ) ? item . tool_calls : [ ]
208+ for ( const toolCall of toolCalls ) {
209+ const callId = toolCall ?. id
210+ const name = toolCall ?. function ?. name ?? toolCall ?. name
211+ if ( ! callId || ! name ) {
212+ continue
213+ }
214+
215+ const argumentsValue =
216+ typeof toolCall ?. function ?. arguments === 'string'
217+ ? toolCall . function . arguments
218+ : JSON . stringify ( toolCall ?. function ?. arguments ?? { } )
219+
220+ items . push ( {
221+ type : 'function_call' ,
222+ call_id : callId ,
223+ name,
224+ arguments : argumentsValue ,
225+ } )
226+ }
227+
203228 continue
204229 }
205230
@@ -233,28 +258,54 @@ export function extractResponseToolCalls(output: unknown): ResponsesToolCall[] {
233258 return [ ]
234259 }
235260
236- return output
237- . map ( ( item ) => {
238- if ( ! item || item . type !== 'function_call' ) {
239- return null
240- }
261+ const toolCalls : ResponsesToolCall [ ] = [ ]
262+
263+ for ( const item of output ) {
264+ if ( ! item || typeof item !== 'object' ) {
265+ continue
266+ }
241267
268+ if ( item . type === 'function_call' ) {
242269 const callId = item . call_id ?? item . id
243270 const name = item . name ?? item . function ?. name
244271 if ( ! callId || ! name ) {
245- return null
272+ continue
246273 }
247274
248275 const argumentsValue =
249276 typeof item . arguments === 'string' ? item . arguments : JSON . stringify ( item . arguments ?? { } )
250277
251- return {
278+ toolCalls . push ( {
252279 id : callId ,
253280 name,
254281 arguments : argumentsValue ,
282+ } )
283+ continue
284+ }
285+
286+ if ( item . type === 'message' && Array . isArray ( item . tool_calls ) ) {
287+ for ( const toolCall of item . tool_calls ) {
288+ const callId = toolCall ?. id
289+ const name = toolCall ?. function ?. name ?? toolCall ?. name
290+ if ( ! callId || ! name ) {
291+ continue
292+ }
293+
294+ const argumentsValue =
295+ typeof toolCall ?. function ?. arguments === 'string'
296+ ? toolCall . function . arguments
297+ : JSON . stringify ( toolCall ?. function ?. arguments ?? { } )
298+
299+ toolCalls . push ( {
300+ id : callId ,
301+ name,
302+ arguments : argumentsValue ,
303+ } )
255304 }
256- } )
257- . filter ( Boolean ) as ResponsesToolCall [ ]
305+ }
306+ }
307+
308+ return toolCalls
258309}
259310
260311/**
@@ -269,8 +320,8 @@ export function parseResponsesUsage(usage: any): ResponsesUsageTokens | undefine
269320 const outputTokens = Number ( usage . output_tokens ?? 0 )
270321 const cachedTokens = Number ( usage . input_tokens_details ?. cached_tokens ?? 0 )
271322 const reasoningTokens = Number ( usage . output_tokens_details ?. reasoning_tokens ?? 0 )
272- const completionTokens = outputTokens + reasoningTokens
273- const totalTokens = inputTokens + completionTokens
323+ const completionTokens = outputTokens
324+ const totalTokens = inputTokens + outputTokens
274325
275326 return {
276327 promptTokens : inputTokens ,
0 commit comments