Update app/main.py
Browse files- app/main.py +55 -0
app/main.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import os, json, traceback
|
| 2 |
from pathlib import Path
|
| 3 |
from fastapi import FastAPI, Request, BackgroundTasks
|
|
@@ -115,6 +116,60 @@ async def chat(request: Request):
|
|
| 115 |
|
| 116 |
|
| 117 |
# ββ Package installer ββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
@app.post("/api/install-package")
|
| 119 |
async def install_package(request: Request):
|
| 120 |
try:
|
|
|
|
| 1 |
+
import json
|
| 2 |
import os, json, traceback
|
| 3 |
from pathlib import Path
|
| 4 |
from fastapi import FastAPI, Request, BackgroundTasks
|
|
|
|
| 116 |
|
| 117 |
|
| 118 |
# ββ Package installer ββββββββββββββββββββββββββββββββββββββββββ
|
| 119 |
+
|
| 120 |
+
# ββ Cross-platform: Web SSE push from Telegram ββββββββββββββββ
|
| 121 |
+
import asyncio as _asyncio
|
| 122 |
+
_sse_queues: dict = {} # session_id -> asyncio.Queue
|
| 123 |
+
|
| 124 |
+
@app.get("/api/sse/{session_id}")
|
| 125 |
+
async def sse_endpoint(session_id: str, request: Request):
|
| 126 |
+
"""SSE stream β pushes messages from Telegram to Web UI."""
|
| 127 |
+
queue = _asyncio.Queue()
|
| 128 |
+
_sse_queues[session_id] = queue
|
| 129 |
+
|
| 130 |
+
from agent_system import orchestrator
|
| 131 |
+
async def tg_notify(role, content):
|
| 132 |
+
await queue.put(json.dumps({"type":"tg_message","role":role,"content":content}))
|
| 133 |
+
orchestrator.register_tg_notify(session_id, tg_notify)
|
| 134 |
+
|
| 135 |
+
async def event_stream():
|
| 136 |
+
try:
|
| 137 |
+
yield "data: " + json.dumps({"type":"connected","session":session_id}) + "\n\n"
|
| 138 |
+
while True:
|
| 139 |
+
if await request.is_disconnected():
|
| 140 |
+
break
|
| 141 |
+
try:
|
| 142 |
+
msg = await _asyncio.wait_for(queue.get(), timeout=25)
|
| 143 |
+
yield f"data: {msg}\n\n"
|
| 144 |
+
except _asyncio.TimeoutError:
|
| 145 |
+
yield "data: " + json.dumps({"type":"ping"}) + "\n\n"
|
| 146 |
+
finally:
|
| 147 |
+
_sse_queues.pop(session_id, None)
|
| 148 |
+
orchestrator.unregister_tg_notify(session_id)
|
| 149 |
+
|
| 150 |
+
return StreamingResponse(event_stream(), media_type="text/event-stream",
|
| 151 |
+
headers={"X-Accel-Buffering":"no","Cache-Control":"no-cache"})
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
@app.post("/api/send-to-telegram")
|
| 155 |
+
async def send_to_telegram(request: Request):
|
| 156 |
+
"""Send a web UI message to linked Telegram chat."""
|
| 157 |
+
try:
|
| 158 |
+
body = await request.json()
|
| 159 |
+
chat_id = body.get("chat_id")
|
| 160 |
+
message = body.get("message","")
|
| 161 |
+
api_key = (body.get("api_key") or "").strip() or cfg.get_longcat_key()
|
| 162 |
+
model = body.get("model","LongCat-Flash-Lite")
|
| 163 |
+
if not chat_id:
|
| 164 |
+
return JSONResponse({"ok":False,"detail":"No chat_id"})
|
| 165 |
+
from telegram_bot import handle_update
|
| 166 |
+
fake_update = {"message":{"chat":{"id":chat_id},"from":{"first_name":"WebUI"},"text":message}}
|
| 167 |
+
import asyncio as _aio
|
| 168 |
+
_aio.create_task(handle_update(fake_update, api_key, model))
|
| 169 |
+
return {"ok":True}
|
| 170 |
+
except Exception as e:
|
| 171 |
+
return JSONResponse(status_code=500, content={"ok":False,"detail":str(e)})
|
| 172 |
+
|
| 173 |
@app.post("/api/install-package")
|
| 174 |
async def install_package(request: Request):
|
| 175 |
try:
|