Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,22 @@
|
|
| 1 |
import os
|
| 2 |
import subprocess
|
| 3 |
from fastapi import FastAPI, HTTPException
|
|
|
|
| 4 |
from pydantic import BaseModel
|
| 5 |
from google import genai
|
| 6 |
from google.genai import types
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 11 |
client = genai.Client(api_key=GEMINI_API_KEY)
|
| 12 |
|
|
@@ -15,6 +25,7 @@ class ChatRequest(BaseModel):
|
|
| 15 |
|
| 16 |
def execute_in_sandbox(command: str):
|
| 17 |
try:
|
|
|
|
| 18 |
full_command = f"cd /tmp && {command}"
|
| 19 |
|
| 20 |
result = subprocess.run(
|
|
@@ -22,7 +33,7 @@ def execute_in_sandbox(command: str):
|
|
| 22 |
shell=True,
|
| 23 |
capture_output=True,
|
| 24 |
text=True,
|
| 25 |
-
timeout=
|
| 26 |
)
|
| 27 |
return {
|
| 28 |
"stdout": result.stdout,
|
|
@@ -32,17 +43,17 @@ def execute_in_sandbox(command: str):
|
|
| 32 |
except Exception as e:
|
| 33 |
return {"error": str(e)}
|
| 34 |
|
| 35 |
-
@app.post("/chat
|
| 36 |
async def agent_chat(request: ChatRequest):
|
| 37 |
user_prompt = request.message
|
| 38 |
|
|
|
|
| 39 |
system_instruction = (
|
| 40 |
-
"You are an AI Agent Builder inside a limited Linux sandbox. "
|
| 41 |
-
"You CANNOT use 'sudo'
|
| 42 |
-
"
|
| 43 |
-
"
|
| 44 |
-
"
|
| 45 |
-
"Return the exact script inside a triple backtick block starting with 'bash'."
|
| 46 |
)
|
| 47 |
|
| 48 |
try:
|
|
@@ -65,4 +76,3 @@ async def agent_chat(request: ChatRequest):
|
|
| 65 |
|
| 66 |
except Exception as e:
|
| 67 |
raise HTTPException(status_code=500, detail=str(e))
|
| 68 |
-
|
|
|
|
| 1 |
import os
|
| 2 |
import subprocess
|
| 3 |
from fastapi import FastAPI, HTTPException
|
| 4 |
+
from fastapi.middleware.cors import CORSMiddleware # CORS fix karne ke liye add kiya
|
| 5 |
from pydantic import BaseModel
|
| 6 |
from google import genai
|
| 7 |
from google.genai import types
|
| 8 |
|
| 9 |
app = FastAPI()
|
| 10 |
|
| 11 |
+
# --- CORS SETTING (Isse mobile app aur browser me Failed to Fetch nahi aayega) ---
|
| 12 |
+
app.add_middleware(
|
| 13 |
+
CORSMiddleware,
|
| 14 |
+
allow_origins=["*"], # Sabhi origins ko allow karega
|
| 15 |
+
allow_credentials=True,
|
| 16 |
+
allow_methods=["*"], # POST, GET sab allow karega
|
| 17 |
+
allow_headers=["*"],
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 21 |
client = genai.Client(api_key=GEMINI_API_KEY)
|
| 22 |
|
|
|
|
| 25 |
|
| 26 |
def execute_in_sandbox(command: str):
|
| 27 |
try:
|
| 28 |
+
# /tmp directory me user ko bina sudo ke full write access milti hai
|
| 29 |
full_command = f"cd /tmp && {command}"
|
| 30 |
|
| 31 |
result = subprocess.run(
|
|
|
|
| 33 |
shell=True,
|
| 34 |
capture_output=True,
|
| 35 |
text=True,
|
| 36 |
+
timeout=600
|
| 37 |
)
|
| 38 |
return {
|
| 39 |
"stdout": result.stdout,
|
|
|
|
| 43 |
except Exception as e:
|
| 44 |
return {"error": str(e)}
|
| 45 |
|
| 46 |
+
@app.post("/chat")
|
| 47 |
async def agent_chat(request: ChatRequest):
|
| 48 |
user_prompt = request.message
|
| 49 |
|
| 50 |
+
# AI ko strictly bol rahe hain ki sudo use na kare, balki /tmp me local tools setup kare
|
| 51 |
system_instruction = (
|
| 52 |
+
"You are an AI Agent Builder operating inside a limited Linux sandbox. "
|
| 53 |
+
"You CANNOT use 'sudo' or root commands because the platform restricts it. "
|
| 54 |
+
"Instead, you must install everything locally inside the `/tmp` directory using pre-compiled binaries, curl, or local zip extractions. "
|
| 55 |
+
"For example, to use Flutter, download its tar.xz via curl into /tmp, extract it, and run it from there directly. "
|
| 56 |
+
"If you want to execute any bash setup, return the exact script inside a triple backtick block starting with 'bash'."
|
|
|
|
| 57 |
)
|
| 58 |
|
| 59 |
try:
|
|
|
|
| 76 |
|
| 77 |
except Exception as e:
|
| 78 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|