@@ -342,19 +342,32 @@ function responseInputRoles(input: unknown): string {
342342 return [ ...roles ] . toSorted ( ) . join ( "," ) ;
343343}
344344
345+ function readToolPayloadField ( record : Record < string , unknown > , field : string ) : unknown {
346+ try {
347+ return record [ field ] ;
348+ } catch {
349+ return undefined ;
350+ }
351+ }
352+
345353function readResponsesToolDisplayName ( tool : unknown ) : string {
346354 if ( ! tool || typeof tool !== "object" ) {
347355 return "" ;
348356 }
349357 const record = tool as Record < string , unknown > ;
350- if ( typeof record . name === "string" ) {
351- return record . name ;
352- }
353- const fn = record . function ;
354- if ( fn && typeof fn === "object" && typeof ( fn as Record < string , unknown > ) . name === "string" ) {
355- return ( fn as Record < string , unknown > ) . name as string ;
358+ const name = readToolPayloadField ( record , "name" ) ;
359+ if ( typeof name === "string" ) {
360+ return name ;
361+ }
362+ const fn = readToolPayloadField ( record , "function" ) ;
363+ if ( fn && typeof fn === "object" ) {
364+ const fnName = readToolPayloadField ( fn as Record < string , unknown > , "name" ) ;
365+ if ( typeof fnName === "string" ) {
366+ return fnName ;
367+ }
356368 }
357- return typeof record . type === "string" ? record . type : "" ;
369+ const type = readToolPayloadField ( record , "type" ) ;
370+ return typeof type === "string" && type !== "function" ? type : "" ;
358371}
359372
360373function summarizeResponsesTools ( tools : unknown ) : string {
@@ -373,11 +386,16 @@ function responsesPayloadToolName(tool: unknown): string | undefined {
373386 if ( ! isRecord ( tool ) ) {
374387 return undefined ;
375388 }
376- if ( typeof tool . name === "string" ) {
377- return tool . name ;
389+ const name = readToolPayloadField ( tool , "name" ) ;
390+ if ( typeof name === "string" ) {
391+ return name ;
392+ }
393+ const fn = readToolPayloadField ( tool , "function" ) ;
394+ if ( ! isRecord ( fn ) ) {
395+ return undefined ;
378396 }
379- const fn = tool . function ;
380- return isRecord ( fn ) && typeof fn . name === "string" ? fn . name : undefined ;
397+ const fnName = readToolPayloadField ( fn , "name" ) ;
398+ return typeof fnName === "string" ? fnName : undefined ;
381399}
382400
383401function enforceCodeModeResponsesToolSurface ( payload : unknown ) : void {
@@ -2026,14 +2044,15 @@ function hasResponsesWebSearchTool(tools: unknown): boolean {
20262044 if ( ! isRecord ( tool ) ) {
20272045 return false ;
20282046 }
2029- if ( tool . type === "web_search" ) {
2047+ const type = readToolPayloadField ( tool , "type" ) ;
2048+ if ( type === "web_search" ) {
20302049 return true ;
20312050 }
2032- if ( tool . type === "function" && tool . name === "web_search" ) {
2051+ if ( type === "function" && readToolPayloadField ( tool , " name" ) === "web_search" ) {
20332052 return true ;
20342053 }
2035- const fn = tool . function ;
2036- return isRecord ( fn ) && fn . name === "web_search" ;
2054+ const fn = readToolPayloadField ( tool , " function" ) ;
2055+ return isRecord ( fn ) && readToolPayloadField ( fn , " name" ) === "web_search" ;
20372056 } ) ;
20382057}
20392058
0 commit comments