muralipala1504 commited on
Commit
27bfdc0
·
1 Parent(s): 8574870

chore: hard-block chat and show admin-only scope message; silence favicon

Browse files
deepshell-backend/deepshell/__main__.py CHANGED
@@ -6,18 +6,17 @@ from pydantic import BaseModel
6
  import os
7
  from pathlib import Path
8
 
9
- from .llm import get_global_client
 
 
10
 
11
- # Resolve paths
12
- # This file is at: deepshell-backend/deepshell/__main__.py
13
- # Repo root is two levels up from here
14
  REPO_ROOT = Path(__file__).resolve().parents[2] # deepshell_modui/
15
  INDEX_PATH = REPO_ROOT / "index.html"
16
- STATIC_ROOT = REPO_ROOT # serve app.js, services.css, etc. from repo root
17
 
18
  app = FastAPI(title="DeepShell Backend + UI")
19
 
20
- # CORS (kept minimal; mostly redundant when same-origin)
21
  default_origins = [
22
  "http://localhost",
23
  "http://localhost:8001",
@@ -36,28 +35,18 @@ app.add_middleware(
36
  allow_headers=["*"],
37
  )
38
 
39
- # Mount static files from repo root at /static (and also expose top-level files)
40
- # We’ll explicitly serve index.html at /, and expose other files at their names.
41
- # Primary mount so /app.js, /services.css, etc. work:
42
  app.mount("/static", StaticFiles(directory=str(STATIC_ROOT), html=False), name="static")
43
 
44
  class ChatRequest(BaseModel):
45
  prompt: str
46
 
47
- # Serve index.html at /
48
  @app.get("/")
49
  def root_page():
50
  if INDEX_PATH.exists():
51
  return FileResponse(str(INDEX_PATH))
52
- return {
53
- "name": "DeepShell Backend",
54
- "status": "ok",
55
- "endpoints": ["/chat/ready", "/chat/run-agent"],
56
- "note": f"index.html not found at {INDEX_PATH}",
57
- }
58
-
59
- # Convenience routes to serve common top-level assets directly
60
- # so your <script src="./app.js"> works without /static prefix.
61
  @app.get("/app.js")
62
  def get_app_js():
63
  path = STATIC_ROOT / "app.js"
@@ -72,32 +61,38 @@ def get_css():
72
  return FileResponse(str(path), media_type="text/css")
73
  return {"detail": "Not Found"}, 404
74
 
 
 
 
 
 
75
  @app.get("/chat/ready")
76
  def chat_ready():
77
- try:
78
- _ = get_global_client(os.getenv("PROVIDER", "groq"))
79
- return {"status": "ok"}
80
- except Exception as e:
81
- return {"status": "error", "detail": str(e)}
 
 
 
 
82
 
83
  @app.post("/chat/run-agent")
84
  def run_agent(req: ChatRequest):
85
  prompt = (req.prompt or "").strip()
86
  if not prompt:
87
- return {"error": "prompt is required"}
88
-
89
- client = get_global_client(os.getenv("PROVIDER", "groq"))
90
- resp = client.chat(prompt, max_tokens=1024, temperature=0.1)
91
 
92
- if not hasattr(resp, "choices") or not resp.choices:
93
- try:
94
- text = resp.choices[0].message.content
95
- except Exception:
96
- text = str(resp)
97
- return {"error": text}
98
 
99
- text = resp.choices[0].message.content
100
- return {"answer": text}
 
 
 
101
 
102
  if __name__ == "__main__":
103
  import uvicorn
 
6
  import os
7
  from pathlib import Path
8
 
9
+ # LLM disabled in hard-block mode. To re-enable later, uncomment this import
10
+ # and the marked section in run_agent.
11
+ # from .llm import get_global_client
12
 
 
 
 
13
  REPO_ROOT = Path(__file__).resolve().parents[2] # deepshell_modui/
14
  INDEX_PATH = REPO_ROOT / "index.html"
15
+ STATIC_ROOT = REPO_ROOT
16
 
17
  app = FastAPI(title="DeepShell Backend + UI")
18
 
19
+ # CORS (mostly redundant when same-origin)
20
  default_origins = [
21
  "http://localhost",
22
  "http://localhost:8001",
 
35
  allow_headers=["*"],
36
  )
37
 
38
+ # Serve static assets from repo root
 
 
39
  app.mount("/static", StaticFiles(directory=str(STATIC_ROOT), html=False), name="static")
40
 
41
  class ChatRequest(BaseModel):
42
  prompt: str
43
 
 
44
  @app.get("/")
45
  def root_page():
46
  if INDEX_PATH.exists():
47
  return FileResponse(str(INDEX_PATH))
48
+ return {"status": "ok", "note": f"index.html not found at {INDEX_PATH}"}
49
+
 
 
 
 
 
 
 
50
  @app.get("/app.js")
51
  def get_app_js():
52
  path = STATIC_ROOT / "app.js"
 
61
  return FileResponse(str(path), media_type="text/css")
62
  return {"detail": "Not Found"}, 404
63
 
64
+ @app.get("/favicon.ico")
65
+ def favicon():
66
+ # Silence 404 noise for favicon
67
+ return {"ok": True}
68
+
69
  @app.get("/chat/ready")
70
  def chat_ready():
71
+ # UI health remains OK in hard-block mode
72
+ return {"status": "ok"}
73
+
74
+ # Polite block message (updated wording)
75
+ BLOCK_MSG = (
76
+ "DeepShell is for Linux SysAdmin, DevOps, IaC, Cloud, and Linux scripting questions only. "
77
+ "Please rephrase your request in that context (e.g., commands, troubleshooting steps, "
78
+ "automation snippets, config examples, or cloud/infra best practices)."
79
+ )
80
 
81
  @app.post("/chat/run-agent")
82
  def run_agent(req: ChatRequest):
83
  prompt = (req.prompt or "").strip()
84
  if not prompt:
85
+ # Still respond politely rather than erroring
86
+ return {"answer": BLOCK_MSG}
 
 
87
 
88
+ # HARD BLOCK: always return the policy message; do not call the LLM.
89
+ return {"answer": BLOCK_MSG}
 
 
 
 
90
 
91
+ # To re-enable later, replace the two lines above with:
92
+ # client = get_global_client(os.getenv("PROVIDER", "groq"))
93
+ # resp = client.chat(prompt, max_tokens=1024, temperature=0.1)
94
+ # text = getattr(resp.choices[0].message, "content", str(resp))
95
+ # return {"answer": text}
96
 
97
  if __name__ == "__main__":
98
  import uvicorn