-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevbrain_service.py
More file actions
103 lines (86 loc) · 3.41 KB
/
Copy pathdevbrain_service.py
File metadata and controls
103 lines (86 loc) · 3.41 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import uvicorn
from fastapi import FastAPI, HTTPException, BackgroundTasks
from pydantic import BaseModel
from typing import Optional, Dict, Any
import devbrain_core
import asyncio
import get_dashboard_data
app = FastAPI(title="DevBrain Core Service")
# Track active background tasks to report to the frontend
active_tasks = {
"capture": False,
"optimize": False,
"last_capture_time": None,
"last_optimize_time": None
}
# Initialize memory connection when the service starts
@app.on_event("startup")
async def startup_event():
print("[DevBrain Service] Initializing Cognee local configurations...")
await devbrain_core.init_memory()
class CaptureRequest(BaseModel):
type: str = "manual_note"
content: str
metadata: Optional[Dict[str, Any]] = {}
class RecallRequest(BaseModel):
query: str
class PurgeRequest(BaseModel):
dataset: str
async def run_capture_in_background(event_type: str, content: str, metadata: dict):
active_tasks["capture"] = True
start_time = asyncio.get_event_loop().time()
try:
await devbrain_core.capture_memory_event(event_type, content, metadata)
except Exception as e:
print(f"[DevBrain Service] Background capture error: {e}")
finally:
active_tasks["capture"] = False
active_tasks["last_capture_time"] = asyncio.get_event_loop().time() - start_time
async def run_optimize_in_background():
active_tasks["optimize"] = True
start_time = asyncio.get_event_loop().time()
try:
await devbrain_core.optimize_memory_result()
except Exception as e:
print(f"[DevBrain Service] Background optimize error: {e}")
finally:
active_tasks["optimize"] = False
active_tasks["last_optimize_time"] = asyncio.get_event_loop().time() - start_time
@app.get("/health")
async def health():
h = devbrain_core.get_memory_health()
h["active_tasks"] = active_tasks
return {"ok": True, "data": h}
@app.get("/datasets")
async def datasets():
return {"ok": True, "data": devbrain_core.list_datasets()}
@app.get("/dashboard_data")
async def dashboard_data():
data = await get_dashboard_data.get_dashboard_data()
# Inject active tasks for frontend stopwatch syncing
data["active_tasks"] = active_tasks
return data
@app.post("/optimize")
async def optimize(background_tasks: BackgroundTasks):
if active_tasks["optimize"]:
return {"ok": False, "error": "An optimization task is already in progress."}
background_tasks.add_task(run_optimize_in_background)
return {"ok": True, "status": "queued", "message": "Graph optimization queued in the background."}
@app.post("/purge")
async def purge(req: PurgeRequest):
result = await devbrain_core.purge_memory_result(req.dataset)
return result
@app.post("/capture")
async def capture(req: CaptureRequest, background_tasks: BackgroundTasks):
if active_tasks["capture"]:
return {"ok": False, "error": "A capture task is already in progress."}
background_tasks.add_task(run_capture_in_background, req.type, req.content, req.metadata)
return {"ok": True, "status": "queued", "source": "cognee"}
@app.post("/recall")
async def recall(req: RecallRequest):
result = await devbrain_core.query_memory_result(req.query)
return result
def run_service(host: str = "127.0.0.1", port: int = 8000):
uvicorn.run("devbrain_service:app", host=host, port=port, log_level="info")
if __name__ == "__main__":
run_service()