@@ -4,8 +4,17 @@ const logger = createLogger('FormatOutput')
44
55// Common text field names to search for
66const TEXT_FIELD_NAMES = [
7- 'text' , 'content' , 'message' , 'body' , 'value' , 'result' ,
8- 'output' , 'response' , 'answer' , 'reply' , 'data'
7+ 'text' ,
8+ 'content' ,
9+ 'message' ,
10+ 'body' ,
11+ 'value' ,
12+ 'result' ,
13+ 'output' ,
14+ 'response' ,
15+ 'answer' ,
16+ 'reply' ,
17+ 'data' ,
918] as const
1019
1120// Maximum depth for recursive traversal
@@ -16,11 +25,7 @@ const MAX_ARRAY_ITEMS = 1000
1625/**
1726 * Deep traversal to find text content in nested structures
1827 */
19- function deepExtractText (
20- obj : any ,
21- depth = 0 ,
22- visited = new Set < any > ( )
23- ) : string | null {
28+ function deepExtractText ( obj : any , depth = 0 , visited = new Set < any > ( ) ) : string | null {
2429 // Prevent infinite recursion
2530 if ( depth > MAX_DEPTH ) return null
2631 if ( ! obj || typeof obj !== 'object' ) return null
@@ -69,23 +74,27 @@ function safeStringify(obj: any, indent = 2): string {
6974 const seen = new Set ( )
7075
7176 try {
72- return JSON . stringify ( obj , ( key , value ) => {
73- // Handle undefined, functions, symbols
74- if ( value === undefined ) return '[undefined]'
75- if ( typeof value === 'function' ) return '[Function]'
76- if ( typeof value === 'symbol' ) return '[Symbol]'
77-
78- // Handle BigInt
79- if ( typeof value === 'bigint' ) return value . toString ( )
80-
81- // Handle circular references
82- if ( typeof value === 'object' && value !== null ) {
83- if ( seen . has ( value ) ) return '[Circular]'
84- seen . add ( value )
85- }
77+ return JSON . stringify (
78+ obj ,
79+ ( key , value ) => {
80+ // Handle undefined, functions, symbols
81+ if ( value === undefined ) return '[undefined]'
82+ if ( typeof value === 'function' ) return '[Function]'
83+ if ( typeof value === 'symbol' ) return '[Symbol]'
84+
85+ // Handle BigInt
86+ if ( typeof value === 'bigint' ) return value . toString ( )
87+
88+ // Handle circular references
89+ if ( typeof value === 'object' && value !== null ) {
90+ if ( seen . has ( value ) ) return '[Circular]'
91+ seen . add ( value )
92+ }
8693
87- return value
88- } , indent )
94+ return value
95+ } ,
96+ indent
97+ )
8998 } catch ( error ) {
9099 logger . warn ( 'Failed to stringify object' , { error } )
91100 return '[Serialization Error]'
@@ -126,10 +135,7 @@ function extractContent(item: any): string | null {
126135 return `[Large Array: ${ item . length } items]`
127136 }
128137
129- const contents = item
130- . slice ( 0 , MAX_ARRAY_ITEMS )
131- . map ( extractContent )
132- . filter ( Boolean )
138+ const contents = item . slice ( 0 , MAX_ARRAY_ITEMS ) . map ( extractContent ) . filter ( Boolean )
133139
134140 // Join with space for inline display in chat UI
135141 return contents . length > 0 ? contents . join ( ' ' ) : null
@@ -159,7 +165,7 @@ export function formatOutputForDisplay(
159165 mode = 'chat' ,
160166 maxLength = MAX_STRING_LENGTH ,
161167 truncate = true ,
162- preserveWhitespace = false
168+ preserveWhitespace = false ,
163169 } = options
164170
165171 try {
@@ -174,7 +180,7 @@ export function formatOutputForDisplay(
174180
175181 // Apply length limits
176182 if ( truncate && result . length > maxLength ) {
177- result = result . substring ( 0 , maxLength ) + ' ... [truncated]'
183+ result = ` ${ result . substring ( 0 , maxLength ) } ... [truncated]`
178184 }
179185
180186 // Clean whitespace unless preserved (but keep code formatting intact)
@@ -196,6 +202,11 @@ export function formatOutputForDisplay(
196202 case 'raw' :
197203 return json
198204 case 'chat' :
205+ // For chat, try to make it more readable
206+ if ( json . length > 500 ) {
207+ return '[Complex Object - See logs for details]'
208+ }
209+ return json
199210 default :
200211 // For chat, try to make it more readable
201212 if ( json . length > 500 ) {
@@ -216,21 +227,21 @@ export const formatOutputForChat = (output: unknown) =>
216227 formatOutputForDisplay ( output , {
217228 mode : 'chat' ,
218229 maxLength : 5000 ,
219- truncate : true
230+ truncate : true ,
220231 } )
221232
222233export const formatOutputForWorkflow = ( output : unknown ) =>
223234 formatOutputForDisplay ( output , {
224235 mode : 'workflow' ,
225236 maxLength : 10000 ,
226- truncate : true
237+ truncate : true ,
227238 } )
228239
229240export const formatOutputRaw = ( output : unknown ) =>
230241 formatOutputForDisplay ( output , {
231242 mode : 'raw' ,
232243 truncate : false ,
233- preserveWhitespace : true
244+ preserveWhitespace : true ,
234245 } )
235246
236247/**
@@ -246,10 +257,10 @@ export function isOutputSafe(output: unknown): boolean {
246257 / o n \w + \s * = / i, // onclick, onload, etc.
247258 / < i f r a m e / i,
248259 / < e m b e d / i,
249- / < o b j e c t / i
260+ / < o b j e c t / i,
250261 ]
251262
252- return ! dangerous . some ( pattern => pattern . test ( str ) )
263+ return ! dangerous . some ( ( pattern ) => pattern . test ( str ) )
253264 } catch {
254265 return false
255266 }
@@ -272,4 +283,4 @@ export function formatOutputSafe(output: unknown): string {
272283 }
273284
274285 return formatted
275- }
286+ }
0 commit comments