@@ -288,17 +288,31 @@ function redactInlineDataUriValue(value: string): string {
288288 return `[inline data URI: ${ value . length } chars]` ;
289289}
290290
291+ function carriesBinaryData ( record : Record < string , unknown > ) : boolean {
292+ const type = normalizeOptionalLowercaseString ( record . type ) ;
293+ if ( type === "audio" || type === "image" || type === "base64" ) {
294+ return true ;
295+ }
296+ const mediaType = normalizeOptionalLowercaseString ( record . media_type ?? record . mimeType ) ;
297+ return (
298+ mediaType ?. startsWith ( "image/" ) === true ||
299+ mediaType ?. startsWith ( "audio/" ) === true ||
300+ mediaType ?. startsWith ( "video/" ) === true ||
301+ mediaType === "application/pdf"
302+ ) ;
303+ }
304+
291305function sanitizeStructuredToolResultValue (
292306 value : unknown ,
293307 key = "" ,
294- parentType ?: string ,
308+ parentCarriesBinaryData = false ,
295309 seen = new WeakSet < object > ( ) ,
296310) : unknown {
297311 if ( typeof value === "string" ) {
298312 if ( SENSITIVE_STRUCTURED_HEADER_FIELDS . has ( key . toLowerCase ( ) ) ) {
299313 return "***" ;
300314 }
301- if ( key === "blob" || ( key === "data" && ( parentType === "audio" || parentType === "image" ) ) ) {
315+ if ( key === "blob" || ( key === "data" && parentCarriesBinaryData ) ) {
302316 return `[binary omitted: ${ value . length } chars]` ;
303317 }
304318 // Claude CLI result blocks carry replay-only ciphertext that is not useful display text.
@@ -319,14 +333,16 @@ function sanitizeStructuredToolResultValue(
319333 seen . add ( value ) ;
320334 if ( Array . isArray ( value ) ) {
321335 // Keep the owning key so arrays of credentials inherit the same redaction policy.
322- return value . map ( ( item ) => sanitizeStructuredToolResultValue ( item , key , parentType , seen ) ) ;
336+ return value . map ( ( item ) =>
337+ sanitizeStructuredToolResultValue ( item , key , parentCarriesBinaryData , seen ) ,
338+ ) ;
323339 }
324340 const record = value as Record < string , unknown > ;
325- const type = readStringValue ( record . type ) ;
341+ const hasBinaryData = carriesBinaryData ( record ) ;
326342 return Object . fromEntries (
327343 Object . entries ( record ) . map ( ( [ childKey , child ] ) => [
328344 childKey ,
329- sanitizeStructuredToolResultValue ( child , childKey , type , seen ) ,
345+ sanitizeStructuredToolResultValue ( child , childKey , hasBinaryData , seen ) ,
330346 ] ) ,
331347 ) ;
332348}
@@ -379,7 +395,7 @@ export function extractToolResultText(result: unknown): string | undefined {
379395 } )
380396 . filter ( ( value ) : value is string => Boolean ( value ) ) ;
381397 if ( texts . length > 0 ) {
382- return texts . join ( "\n" ) ;
398+ return truncateToolText ( texts . join ( "\n" ) ) ;
383399 }
384400 const structuredTexts : string [ ] = [ ] ;
385401 for ( const item of content ) {
0 commit comments