File size: 2,696 Bytes
fa39d87
8377cb8
eda2658
8377cb8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eda2658
8377cb8
 
 
 
 
 
 
eda2658
fa39d87
 
 
 
 
 
 
 
 
 
 
 
8377cb8
 
fa39d87
 
 
 
 
 
 
 
 
8377cb8
fa39d87
 
8377cb8
fa39d87
 
 
 
 
 
 
 
eda2658
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import os
import sys
import shutil
import subprocess
import urllib.request

NODE_DIR = "/home/user/node22"
NPM_GLOBAL = "/home/user/.npm-global"

def setup_openclaw():
    # 1. Download & Extract portable Node.js 22 if not present
    if not os.path.exists(NODE_DIR):
        print("Downloading Node.js v22...")
        node_url = "https://nodejs.org/dist/v22.14.0/node-v22.14.0-linux-x64.tar.xz"
        tar_path = "/home/user/node22.tar.xz"
        
        urllib.request.urlretrieve(node_url, tar_path)
        
        print("Extracting Node.js v22...")
        os.makedirs(NODE_DIR, exist_ok=True)
        subprocess.run(["tar", "-xJf", tar_path, "-C", NODE_DIR, "--strip-components=1"], check=True)
        os.remove(tar_path)

    # 2. Add Node 22 binary folder to environment PATH
    node_bin = os.path.join(NODE_DIR, "bin")
    os.environ["PATH"] = f"{node_bin}:{NPM_GLOBAL}/bin:{os.environ['PATH']}"

    # 3. Redirect global npm installs to /home/user/.npm-global to bypass permission errors
    os.makedirs(NPM_GLOBAL, exist_ok=True)
    subprocess.run(["npm", "config", "set", "prefix", NPM_GLOBAL], check=True)

    # 4. Install OpenClaw globally in user space
    if not shutil.which("openclaw"):
        print("Installing OpenClaw globally under user directory...")
        subprocess.run(["npm", "install", "-g", "openclaw@latest"], check=True)
        print("OpenClaw installed successfully!")

setup_openclaw()

import gradio as gr

def run_command(cmd):
    if not cmd.strip():
        return "Please enter a valid command."
    try:
        result = subprocess.run(
            cmd,
            shell=True,
            capture_output=True,
            text=True,
            timeout=60,
            cwd=os.getcwd(),
            env=os.environ
        )
        output = result.stdout
        if result.stderr:
            output += f"\n[STDERR]\n{result.stderr}"
        return output if output else "Command completed with no output."
    except Exception as e:
        return f"Error executing command: {str(e)}"

with gr.Blocks(title="Gradio Terminal") as demo:
    gr.Markdown("## 🛠️ Gradio Terminal Executor (Node 22 + OpenClaw)")
    
    with gr.Row():
        cmd_input = gr.Textbox(placeholder="Type command here (e.g., openclaw --version)...", label="Command", scale=4)
        run_btn = gr.Button("Run", scale=1)
        
    output_box = gr.Code(label="Terminal Output", language="shell", interactive=False)

    run_btn.click(fn=run_command, inputs=cmd_input, outputs=output_box)
    cmd_input.submit(fn=run_command, inputs=cmd_input, outputs=output_box)

if __name__ == "__main__":
    demo.launch(ssr_mode=False, server_name="0.0.0.0", server_port=7860)