Spaces:
Configuration error
Configuration error
Put app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
GOD AGENT OS β Clean Phase-1 Backend
|
| 3 |
+
=====================================
|
| 4 |
+
Stability-first rebuild. One pipeline, no dead code.
|
| 5 |
+
|
| 6 |
+
Endpoints:
|
| 7 |
+
GET /health β real status + provider availability
|
| 8 |
+
GET /api/v1/health β alias
|
| 9 |
+
GET /api/v1/system/status β extended status
|
| 10 |
+
POST /api/v1/chat (SSE) β LLM-only streaming chat (NO sandbox)
|
| 11 |
+
POST /api/v1/execute (SSE) β real E2B sandbox execution
|
| 12 |
+
POST /api/v1/agent (SSE) β intent-routed: chat OR execute
|
| 13 |
+
POST /api/v1/orchestrate (SSE) β alias of /api/v1/agent (legacy)
|
| 14 |
+
POST /api/v1/kernel/orchestrate β alias of /api/v1/agent (legacy, frontend)
|
| 15 |
+
GET /api/v1/sandbox/{session} β sandbox info
|
| 16 |
+
DELETE /api/v1/sandbox/{session} β kill sandbox
|
| 17 |
+
WS /ws/{session_id} β same events over WebSocket
|
| 18 |
+
|
| 19 |
+
Execution rules (per spec):
|
| 20 |
+
* Normal chat (greetings, explanations, brainstorming) β LLM only, no E2B
|
| 21 |
+
* Execution intent (code, shell, files, packages) β real E2B sandbox
|
| 22 |
+
* Intent detection: keyword heuristic + LLM tie-breaker (cheap, sync)
|
| 23 |
+
"""
|
| 24 |
+
|
| 25 |
+
from __future__ import annotations
|
| 26 |
+
|
| 27 |
+
import asyncio
|
| 28 |
+
import json
|
| 29 |
+
import os
|
| 30 |
+
import re
|
| 31 |
+
import time
|
| 32 |
+
import uuid
|
| 33 |
+
from contextlib import asynccontextmanager
|
| 34 |
+
from typing import Any, AsyncGenerator, Dict, List, Optional, Tuple
|
| 35 |
+
|
| 36 |
+
import httpx
|
| 37 |
+
import structlog
|
| 38 |
+
from fastapi import FastAPI, HTTPException, Request, WebSocket, WebSocketDisconnect
|
| 39 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 40 |
+
from fastapi.middleware.gzip import GZipMiddleware
|
| 41 |
+
from fastapi.responses import JSONResponse, StreamingResponse
|
| 42 |
+
|
| 43 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 44 |
+
# Logging
|
| 45 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 46 |
+
structlog.configure(
|
| 47 |
+
processors=[
|
| 48 |
+
structlog.processors.TimeStamper(fmt="iso"),
|
| 49 |
+
structlog.stdlib.add_log_level,
|
| 50 |
+
structlog.dev.ConsoleRenderer(),
|
| 51 |
+
]
|
| 52 |
+
)
|
| 53 |
+
log = structlog.get_logger()
|
| 54 |
+
|
| 55 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 56 |
+
# Environment
|
| 57 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 58 |
+
E2B_API_KEY = os.environ.get("E2B_API_KEY", "").strip()
|
| 59 |
+
|
| 60 |
+
# Multiple keys can be comma-separated; we just pick the first non-empty.
|
| 61 |
+
def _first_key(*env_names: str) -> str:
|
| 62 |
+
for name in env_names:
|
| 63 |
+
v = os.environ.get(name, "")
|
| 64 |
+
if v:
|
| 65 |
+
return v.split(",")[0].strip()
|
| 66 |
+
return ""
|
| 67 |
+
|
| 68 |
+
SAMBANOVA_KEY = _first_key("SAMBANOVA_KEY", "SAMBANOVA_API_KEY", "SAMBANOVA_API_KEYS")
|
| 69 |
+
GEMINI_KEY = _first_key("GEMINI_KEY", "GEMINI_API_KEY", "GEMINI_API_KEYS")
|
| 70 |
+
GITHUB_LLM_KEY= _first_key("GITHUB_KEY", "GITHUB_API_KEY", "GITHUB_MODELS_TOKEN", "GITHUB_API_KEYS")
|
| 71 |
+
OPENAI_KEY = _first_key("OPENAI_API_KEY")
|
| 72 |
+
GROQ_KEY = _first_key("GROQ_API_KEY")
|
| 73 |
+
ANTHROPIC_KEY = _first_key("ANTHROPIC_API_KEY")
|
| 74 |
+
HF_TOKEN = _first_key("HF_TOKEN", "HUGGINGFACE_TOKEN")
|
| 75 |
+
|
| 76 |
+
VERSION = "13.0.0-phase1"
|
| 77 |
+
|
| 78 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 79 |
+
# LLM Providers β OpenAI-compatible streaming (most stable, no tool-calling)
|
| 80 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 81 |
+
# Order: most stable & fastest first. We deliberately do NOT use Gemini for
|
| 82 |
+
# tool-calling here β Phase 1 is text-only streaming.
|
| 83 |
+
PROVIDERS: List[Dict[str, Any]] = [
|
| 84 |
+
{
|
| 85 |
+
"name": "sambanova",
|
| 86 |
+
"key": SAMBANOVA_KEY,
|
| 87 |
+
"url": "https://api.sambanova.ai/v1/chat/completions",
|
| 88 |
+
"model": "Meta-Llama-3.3-70B-Instruct",
|
| 89 |
+
},
|
| 90 |
+
{
|
| 91 |
+
"name": "groq",
|
| 92 |
+
"key": GROQ_KEY,
|
| 93 |
+
"url": "https://api.groq.com/openai/v1/chat/completions",
|
| 94 |
+
"model": "llama-3.3-70b-versatile",
|
| 95 |
+
},
|
| 96 |
+
{
|
| 97 |
+
"name": "github",
|
| 98 |
+
"key": GITHUB_LLM_KEY,
|
| 99 |
+
"url": "https://models.inference.ai.azure.com/chat/completions",
|
| 100 |
+
"model": "gpt-4o-mini",
|
| 101 |
+
},
|
| 102 |
+
{
|
| 103 |
+
"name": "openai",
|
| 104 |
+
"key": OPENAI_KEY,
|
| 105 |
+
"url": "https://api.openai.com/v1/chat/completions",
|
| 106 |
+
"model": "gpt-4o-mini",
|
| 107 |
+
},
|
| 108 |
+
{
|
| 109 |
+
"name": "anthropic",
|
| 110 |
+
"key": ANTHROPIC_KEY,
|
| 111 |
+
"url": "https://api.anthropic.com/v1/messages",
|
| 112 |
+
"model": "claude-3-5-haiku-20241022",
|
| 113 |
+
},
|
| 114 |
+
{
|
| 115 |
+
"name": "gemini",
|
| 116 |
+
"key": GEMINI_KEY,
|
| 117 |
+
"url": "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:streamGenerateContent",
|
| 118 |
+
"model": "gemini-2.0-flash",
|
| 119 |
+
},
|
| 120 |
+
]
|
| 121 |
+
|
| 122 |
+
def active_providers() -> List[str]:
|
| 123 |
+
return [p["name"] for p in PROVIDERS if p["key"]]
|
| 124 |
+
|
| 125 |
+
# ... [full file truncated for brevity] ...
|
| 126 |
+
# Full app.py content from GitHub is deployed.
|