@@ -169,10 +169,67 @@ class CodeExecutionService {
169169 }
170170
171171 const submissionResult = await response . json ( ) ;
172+
173+ // Check if this is a synchronous response (Piston) with immediate results
174+ // Piston returns stdout, stderr, status all in the initial response
175+ if ( submissionResult . stdout !== undefined || submissionResult . stderr !== undefined ) {
176+ // Synchronous execution (Piston) - results are already available
177+ const statusId = submissionResult . status ?. id || 3 ;
178+ const statusDescription = submissionResult . status ?. description || 'Accepted' ;
179+
180+ if ( onStatusUpdate ) {
181+ onStatusUpdate ( { id : statusId , description : statusDescription } ) ;
182+ }
183+
184+ // Piston returns plain text (not base64), but check just in case
185+ const stdout = this . decodeBase64 ( submissionResult . stdout ) || submissionResult . stdout || '' ;
186+ const stderr = this . decodeBase64 ( submissionResult . stderr ) || submissionResult . stderr || '' ;
187+ const compileOutput = this . decodeBase64 ( submissionResult . compile_output ) || submissionResult . compile_output || '' ;
188+
189+ if ( statusId === 3 ) {
190+ return {
191+ output : stdout ,
192+ error : undefined ,
193+ executionTime : parseFloat ( submissionResult . time || '0' ) || Date . now ( ) - startTime ,
194+ language,
195+ status : {
196+ id : statusId ,
197+ description : statusDescription ,
198+ } ,
199+ } ;
200+ } else {
201+ let errorMessage = statusDescription ;
202+
203+ if ( stderr ) {
204+ const stderrContent = stderr . trim ( ) ;
205+ if ( stderrContent ) {
206+ errorMessage += `\n\nDetails:\n${ stderrContent } ` ;
207+ }
208+ } else if ( compileOutput ) {
209+ const compileOutputContent = compileOutput . trim ( ) ;
210+ if ( compileOutputContent ) {
211+ errorMessage += `\n\nCompilation details:\n${ compileOutputContent } ` ;
212+ }
213+ }
214+
215+ return {
216+ output : stdout || '' ,
217+ error : errorMessage ,
218+ executionTime : parseFloat ( submissionResult . time || '0' ) || Date . now ( ) - startTime ,
219+ language,
220+ status : {
221+ id : statusId ,
222+ description : statusDescription ,
223+ } ,
224+ } ;
225+ }
226+ }
227+
228+ // Asynchronous execution (Judge0) - need to poll for results
172229 const submissionToken = submissionResult . token ;
173230
174231 if ( ! submissionToken ) {
175- throw new Error ( 'No submission token returned from Judge0 ' ) ;
232+ throw new Error ( 'No submission token returned from execution service ' ) ;
176233 }
177234
178235 return await this . pollForResult (
@@ -243,7 +300,7 @@ class CodeExecutionService {
243300 }
244301
245302 if ( statusId && statusId !== 1 && statusId !== 2 ) {
246- // Decode base64 encoded output from Judge0
303+ // Decode base64 encoded output ( Judge0 uses base64 encoding)
247304 const stdout = this . decodeBase64 ( result . stdout ) ;
248305 const stderr = this . decodeBase64 ( result . stderr ) ;
249306 const compileOutput = this . decodeBase64 ( result . compile_output ) ;
@@ -307,7 +364,7 @@ class CodeExecutionService {
307364
308365 return {
309366 output : '' ,
310- error : 'Execution timeout - Judge0 took too long to process' ,
367+ error : 'Execution timeout - code execution took too long to process' ,
311368 executionTime : Date . now ( ) - startTime ,
312369 language,
313370 status : {
0 commit comments