@@ -14,6 +14,7 @@ const logger = new Logger('AXQueryExecutor');
1414export interface AXQueryExecutionResult {
1515 result : Record < string , unknown > ;
1616 execution_time_seconds : number ;
17+ debug_logs ?: string [ ] ;
1718}
1819
1920export class AXQueryExecutor {
@@ -57,15 +58,18 @@ export class AXQueryExecutor {
5758 cmd : queryData . command ,
5859 multi : queryData . return_all_matches ,
5960 locator : {
60- app : queryData . locator . app ,
61- role : queryData . locator . role ,
62- match : queryData . locator . match ,
63- pathHint : queryData . locator . navigation_path_hint ,
61+ app : ( queryData . locator as { app : string } ) . app ,
62+ role : ( queryData . locator as { role : string } ) . role ,
63+ match : ( queryData . locator as { match : Record < string , string > } ) . match ,
64+ pathHint : ( queryData . locator as { navigation_path_hint ?: string [ ] } ) . navigation_path_hint ,
6465 } ,
6566 attributes : queryData . attributes_to_query ,
6667 requireAction : queryData . required_action_name ,
6768 action : queryData . action_to_perform ,
6869 // report_execution_time is not sent to the Swift binary
70+ debug_logging : queryData . debug_logging ,
71+ max_elements : queryData . max_elements ,
72+ output_format : queryData . output_format
6973 } ;
7074 logger . debug ( 'Mapped AX query for Swift binary:' , mappedQueryData ) ;
7175
@@ -131,8 +135,10 @@ export class AXQueryExecutor {
131135 // If we got any JSON output, try to parse it
132136 if ( stdoutData . trim ( ) ) {
133137 try {
134- const result = JSON . parse ( stdoutData ) as Record < string , unknown > ;
135- return resolve ( { result, execution_time_seconds } ) ;
138+ const parsedJson = JSON . parse ( stdoutData ) as ( Record < string , unknown > & { debug_logs ?: string [ ] } ) ;
139+ // Separate the core result from potential debug_logs
140+ const { debug_logs, ...coreResult } = parsedJson ;
141+ return resolve ( { result : coreResult , execution_time_seconds, debug_logs } ) ;
136142 } catch ( error ) {
137143 logger . error ( 'Failed to parse JSON output' , { error, stdout : stdoutData } ) ;
138144 // Fall through to error handling below if JSON parsing fails
@@ -145,10 +151,30 @@ export class AXQueryExecutor {
145151 } else if ( code !== 0 ) {
146152 errorMessage = `Process exited with code ${ code } : ${ stderrData } ` ;
147153 } else {
148- errorMessage = `Process completed but no valid output: ${ stderrData } ` ;
154+ // Attempt to parse stderr as JSON ErrorResponse if stdout was empty but exit was 0
155+ try {
156+ const errorJson = JSON . parse ( stderrData . split ( '\n' ) . filter ( line => line . startsWith ( "{\"error\":" ) ) . join ( '' ) || stderrData ) ;
157+ if ( errorJson . error ) {
158+ errorMessage = `AX tool reported error: ${ errorJson . error } ` ;
159+ const errorToReject = new Error ( errorMessage ) as Error & { execution_time_seconds ?: number ; debug_logs ?: string [ ] } ;
160+ errorToReject . execution_time_seconds = execution_time_seconds ;
161+ errorToReject . debug_logs = errorJson . debug_logs ; // Capture debug logs from error JSON
162+ return reject ( errorToReject ) ;
163+ }
164+ } catch {
165+ // stderr was not a JSON error response, proceed with generic message
166+ }
167+ errorMessage = `Process completed but no valid JSON output on stdout. Stderr: ${ stderrData } ` ;
149168 }
150- const errorToReject = new Error ( errorMessage ) as Error & { execution_time_seconds ?: number } ;
169+ const errorToReject = new Error ( errorMessage ) as Error & { execution_time_seconds ?: number ; debug_logs ?: string [ ] } ;
151170 errorToReject . execution_time_seconds = execution_time_seconds ;
171+ // If stderrData might contain our JSON error object with debug_logs, try to parse it
172+ try {
173+ const errorJson = JSON . parse ( stderrData . split ( '\n' ) . filter ( line => line . startsWith ( "{\"error\":" ) ) . join ( '' ) || stderrData ) ;
174+ if ( errorJson . debug_logs ) {
175+ errorToReject . debug_logs = errorJson . debug_logs ;
176+ }
177+ } catch { /* ignore if stderr is not our JSON error */ }
152178 reject ( errorToReject ) ;
153179 } ) ;
154180
0 commit comments