@@ -434,6 +434,7 @@ async def run_script(req):
434434 if not os .path .exists (path ):
435435 return web .json_response ({"error" : "Script not found" }, status = 404 )
436436
437+ t0 = time .monotonic ()
437438 try :
438439 proc = await asyncio .create_subprocess_exec (
439440 "bash" ,
@@ -444,18 +445,22 @@ async def run_script(req):
444445 )
445446 stdout , stderr = await asyncio .wait_for (proc .communicate (), timeout = 120 )
446447
447- return web . json_response (
448- {
449- "success " : proc . returncode == 0 ,
450- "stdout " : stdout .decode () if stdout else "" ,
451- "stderr " : stderr . decode () if stderr else "" ,
452- "returncode" : proc . returncode ,
453- }
454- )
448+ result = {
449+ "success" : proc . returncode == 0 ,
450+ "stdout " : stdout . decode () if stdout else "" ,
451+ "stderr " : stderr .decode () if stderr else "" ,
452+ "returncode " : proc . returncode ,
453+ }
454+ _add_debug_log ( f"bash { name } " , result , time . monotonic () - t0 )
455+ return web . json_response ( result )
455456 except asyncio .TimeoutError :
456457 proc .kill ()
458+ result = {"success" : False , "error" : "Script execution timed out" , "returncode" : - 1 , "stdout" : "" , "stderr" : "Script execution timed out" }
459+ _add_debug_log (f"bash { name } " , result , time .monotonic () - t0 )
457460 return web .json_response ({"error" : "Script execution timed out" }, status = 500 )
458461 except Exception as e :
462+ result = {"success" : False , "error" : str (e ), "returncode" : - 1 , "stdout" : "" , "stderr" : str (e )}
463+ _add_debug_log (f"bash { name } " , result , time .monotonic () - t0 )
459464 return web .json_response ({"error" : str (e )}, status = 500 )
460465
461466 ctx .add_post ("/browser/scripts/{name}/run" , run_script )
@@ -465,10 +470,11 @@ async def run_script(req):
465470 # =========================================================================
466471
467472 async def generate_script (req ):
468- """Generate script from prompt using AI."""
473+ """Generate or modify a script from prompt using AI."""
469474 data = await req .json ()
470475 prompt = data .get ("prompt" , "" )
471476 name = data .get ("name" , "generated-script.sh" )
477+ existing_script = data .get ("existing_script" , "" )
472478
473479 if not prompt :
474480 return web .json_response ({"error" : "Prompt required" }, status = 400 )
@@ -496,11 +502,16 @@ async def generate_script(req):
496502
497503Output ONLY the bash script, no explanations."""
498504
505+ if existing_script .strip ():
506+ user_message = f"Here is an existing browser automation script:\n \n ```bash\n { existing_script } \n ```\n \n Modify this script to: { prompt } \n \n Output the complete updated script."
507+ else :
508+ user_message = f"Create a browser automation script that: { prompt } "
509+
499510 chat_request = {
500511 "model" : ctx .config .get ("defaults" , {}).get ("text" , {}).get ("model" , "MiniMax-M2.1" ),
501512 "messages" : [
502513 {"role" : "system" , "content" : system_prompt },
503- {"role" : "user" , "content" : f"Create a browser automation script that: { prompt } " },
514+ {"role" : "user" , "content" : user_message },
504515 ],
505516 }
506517
@@ -528,7 +539,12 @@ async def generate_script(req):
528539 ctx .add_post ("/browser/scripts/generate" , generate_script )
529540
530541 ctx .add_importmaps ({"xterm" : f"{ ctx .ext_prefix } /xterm-esm.js" })
531- ctx .add_index_footer (f"""<link rel="stylesheet" href="{ ctx .ext_prefix } /xterm.css">""" )
542+ ctx .add_index_footer (
543+ f"""
544+ <link rel="stylesheet" href="{ ctx .ext_prefix } /xterm.css">
545+ <script src="{ ctx .ext_prefix } /shell.js"></script>
546+ """
547+ )
532548
533549
534550__install__ = install
0 commit comments