11import type { AssetResource , OrchestrationV2TurnItem } from "@t3tools/contracts" ;
2- import { readToolOutputImage , toolOutputImages } from "@t3tools/shared/toolOutput" ;
2+ import { toolOutputImageBlocks , toolOutputImages } from "@t3tools/shared/toolOutput" ;
33import * as DateTime from "effect/DateTime" ;
44
55const MAX_TEXT_BLOCK_DEPTH = 4 ;
@@ -12,28 +12,33 @@ function isSummarizedValue(value: unknown): boolean {
1212 return isRecord ( value ) && value . truncated === true && typeof value . summary === "string" ;
1313}
1414
15- function textFromBlocks ( value : unknown , depth : number ) : string | null {
15+ function textFromBlocks (
16+ value : unknown ,
17+ depth : number ,
18+ imageBlocks : ReadonlyArray < unknown > ,
19+ ) : string | null {
1620 if ( depth > MAX_TEXT_BLOCK_DEPTH ) return null ;
1721 if ( typeof value === "string" ) return value ;
1822 if ( Array . isArray ( value ) ) {
19- const parts = value . map ( ( block ) => textFromBlocks ( block , depth + 1 ) ) ;
23+ const parts = value . map ( ( block ) => textFromBlocks ( block , depth + 1 , imageBlocks ) ) ;
2024 return parts . every ( ( part ) => part !== null )
2125 ? parts . filter ( ( part ) => part !== "" ) . join ( "\n" )
2226 : null ;
2327 }
2428 if ( ! isRecord ( value ) ) return null ;
2529 if ( value . type === "text" && typeof value . text === "string" ) return value . text ;
26- if ( value . type === "image" ) return readToolOutputImage ( value ) ? "" : "[image]" ;
30+ if ( value . type === "image" ) return imageBlocks . includes ( value ) ? "" : "[image]" ;
2731 if ( value . type === "resource_link" && typeof value . uri === "string" ) return value . uri ;
2832 if ( value . type === "resource" && isRecord ( value . resource ) ) {
2933 const resource = value . resource ;
3034 if ( typeof resource . text === "string" ) return resource . text ;
3135 if ( typeof resource . uri === "string" ) return resource . uri ;
3236 }
3337 const keys = Object . keys ( value ) . filter ( ( key ) => key !== "isError" && key !== "is_error" ) ;
34- if ( keys . length === 1 && keys [ 0 ] === "content" ) return textFromBlocks ( value . content , depth + 1 ) ;
38+ if ( keys . length === 1 && keys [ 0 ] === "content" )
39+ return textFromBlocks ( value . content , depth + 1 , imageBlocks ) ;
3540 if ( keys . length === 2 && keys . includes ( "content" ) && keys . includes ( "structuredContent" ) ) {
36- const text = textFromBlocks ( value . content , depth + 1 ) ;
41+ const text = textFromBlocks ( value . content , depth + 1 , imageBlocks ) ;
3742 return text ?. trim ( ) ? text : null ;
3843 }
3944 return null ;
@@ -61,7 +66,7 @@ function prettyJsonText(text: string): string {
6166function formatToolValue ( value : unknown , hasImagePreview = false ) : string | null {
6267 if ( value === undefined || value === null ) return null ;
6368 if ( hasImagePreview && isRecord ( value ) && value . type === "image" ) return null ;
64- const text = textFromBlocks ( value , 0 ) ;
69+ const text = textFromBlocks ( value , 0 , toolOutputImageBlocks ( value ) ) ;
6570 if ( text !== null ) return text . trim ( ) ? prettyJsonText ( text ) : null ;
6671 let json : string | undefined ;
6772 try {
0 commit comments