@@ -393,18 +393,27 @@ function App() {
393393 const model = getCurrentModel ( ) ;
394394 const response = await callAI ( defaultProvider , apiKey , prompt , model ) ;
395395
396- // Risposta di fallback
397- let result = {
398- filename : "script.sh" ,
399- script : response . length > 200 ? response . substring ( 0 , 200 ) + "\n# ... (script troncato)" : response ,
400- usage : "chmod +x script.sh && ./script.sh" ,
401- dependencies : "bash"
402- } ;
403-
404- // Prova a estrarre JSON pulito
405- const jsonMatch = response . match ( / \{ [ ^ { } ] * " s c r i p t " \s * : \s * " ( [ ^ " ] * ) " [ ^ { } ] * \} / ) ;
406- if ( jsonMatch && jsonMatch [ 1 ] ) {
407- result . script = jsonMatch [ 1 ] . replace ( / \\ n / g, '\n' ) . replace ( / \\ " / g, '"' ) ;
396+ const parsedScript = extractJsonObject ( response ) ;
397+ if ( parsedScript ?. script ) {
398+ const result = {
399+ filename : parsedScript . filename || `script.${ scriptType === 'python' ? 'py' : scriptType === 'powershell' ? 'ps1' : scriptType === 'nodejs' ? 'js' : 'sh' } ` ,
400+ script : parsedScript . script ,
401+ usage : parsedScript . usage || "" ,
402+ dependencies : parsedScript . dependencies || "" ,
403+ severity : parsedScript . severity || parsedScript . risk_level || "MEDIUM" ,
404+ confidence : parsedScript . confidence || "MEDIUM" ,
405+ requires_sudo : Boolean ( parsedScript . requires_sudo ) ,
406+ destructive : Boolean ( parsedScript . destructive ) ,
407+ detected_stack : parsedScript . detected_stack || [ ] ,
408+ next_best_action : parsedScript . next_best_action || "" ,
409+ verification_commands : parsedScript . verification_commands || [ ] ,
410+ rollback_commands : parsedScript . rollback_commands || [ ] ,
411+ verification : parsedScript . verification || "" ,
412+ rollback : parsedScript . rollback || "" ,
413+ safety_notes : parsedScript . safety_notes || "" ,
414+ assumptions : parsedScript . assumptions || [ ] ,
415+ } ;
416+
408417 showToast ( "Script generato!" , "success" ) ;
409418 history . addEntry ( {
410419 tool : 'scriptBuilder' ,
@@ -417,30 +426,15 @@ function App() {
417426 } ) ;
418427 return result ;
419428 }
420-
421- // Cerca JSON anche se malformato
422- const start = response . indexOf ( '{' ) ;
423- const end = response . lastIndexOf ( '}' ) ;
424- if ( start !== - 1 && end !== - 1 && end > start ) {
425- try {
426- const parsed = extractJsonObject ( response . substring ( start , end + 1 ) ) ;
427- if ( parsed . script ) {
428- result = { ...result , ...parsed } ;
429- showToast ( "Script generato!" , "success" ) ;
430- history . addEntry ( {
431- tool : 'scriptBuilder' ,
432- toolName : t . modes . scriptBuilder . name ,
433- toolIcon : t . modes . scriptBuilder . icon ,
434- input : `[${ scriptType } ] ${ description } ` ,
435- output : result ,
436- provider : defaultProvider ,
437- model : model ,
438- } ) ;
439- return result ;
440- }
441- } catch ( e ) { }
442- }
443-
429+
430+ // Fallback: show raw response only if JSON extraction failed.
431+ let result = {
432+ filename : `script.${ scriptType === 'python' ? 'py' : scriptType === 'powershell' ? 'ps1' : scriptType === 'nodejs' ? 'js' : 'sh' } ` ,
433+ script : response . length > 2000 ? response . substring ( 0 , 2000 ) + "\n# ... (script truncated)" : response ,
434+ usage : "" ,
435+ dependencies : ""
436+ } ;
437+
444438 showToast ( "Script generato (formato semplice)!" , "success" ) ;
445439 history . addEntry ( {
446440 tool : 'scriptBuilder' ,
0 commit comments