@@ -122,11 +122,17 @@ function printJson(data: unknown): void {
122122/** Apply --fields filter to output. Reduces context window for AI agents. */
123123function applyFieldsFilter ( data : unknown , fields ?: string ) : unknown {
124124 if ( ! fields ) return data ;
125- const keys = fields . split ( "," ) . map ( ( f ) => f . trim ( ) ) ;
126- if ( Array . isArray ( data ) ) {
127- return data . map ( ( item ) => pickFields ( item , keys ) ) ;
125+ const keys = fields . split ( "," ) . map ( ( f ) => f . trim ( ) ) . filter ( Boolean ) ;
126+ if ( Array . isArray ( data ) && data . length > 0 ) {
127+ const sample = data [ 0 ] as Record < string , unknown > ;
128+ const available = Object . keys ( sample ) ;
129+ const invalid = keys . filter ( ( k ) => ! available . includes ( k ) ) ;
130+ if ( invalid . length > 0 && invalid . length === keys . length ) {
131+ console . error ( `Warning: No matching fields. Available: ${ available . slice ( 0 , 15 ) . join ( ", " ) } ` ) ;
132+ }
133+ return data . map ( ( item ) => pickFields ( item as Record < string , unknown > , keys ) ) ;
128134 }
129- if ( data && typeof data === "object" ) {
135+ if ( data && typeof data === "object" && ! Array . isArray ( data ) ) {
130136 return pickFields ( data as Record < string , unknown > , keys ) ;
131137 }
132138 return data ;
@@ -3587,14 +3593,23 @@ export function createProgram(): Command {
35873593 const body = loadJsonPayload ( opts . body ) ;
35883594 ensureLocalValidation ( "agent.stream" , validateAgentStreamPayload ( body ) ) ;
35893595 const streamBody = body as import ( "@pixelml/agenticflow-sdk" ) . StreamRequest ;
3590- if ( token ) {
3591- const stream = await client . agents . stream ( opts . agentId , streamBody ) ;
3592- const text = await stream . text ( ) ;
3593- await run ( ( ) => Promise . resolve ( text ) ) ;
3594- } else {
3595- const stream = await client . agents . streamAnonymous ( opts . agentId , streamBody ) ;
3596+ try {
3597+ const stream = token
3598+ ? await client . agents . stream ( opts . agentId , streamBody )
3599+ : await client . agents . streamAnonymous ( opts . agentId , streamBody ) ;
35963600 const text = await stream . text ( ) ;
3597- await run ( ( ) => Promise . resolve ( text ) ) ;
3601+ if ( isJsonFlagEnabled ( ) ) {
3602+ printResult ( {
3603+ schema : "agenticflow.agent.stream.v1" ,
3604+ agent_id : opts . agentId ,
3605+ response : text ,
3606+ } ) ;
3607+ } else {
3608+ console . log ( text ) ;
3609+ }
3610+ } catch ( err ) {
3611+ const message = err instanceof Error ? err . message : String ( err ) ;
3612+ fail ( "stream_failed" , message ) ;
35983613 }
35993614 } ) ;
36003615
0 commit comments