-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhive_api_server.py
More file actions
128 lines (99 loc) · 3.5 KB
/
Copy pathhive_api_server.py
File metadata and controls
128 lines (99 loc) · 3.5 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env python3
"""REST/OpenAPI service wrapper for Hive.
Exposes Hive operations over HTTP with auto-generated OpenAPI docs.
Usage::
python scripts/hive_api_server.py --port 8080
Endpoints:
POST /route → RouteDecision
POST /compress → CompressedTurn
POST /remember → MemoryNode
GET /recall → value
GET /health → 200 (liveness)
GET /ready → 200/503 (readiness)
GET /openapi.json → OpenAPI schema
"""
from __future__ import annotations
import argparse
from typing import Any
from hive import HiveStack
from hive.rule_fast import RuleFastHoneyComb
try:
from fastapi import FastAPI, HTTPException
from fastapi.responses import JSONResponse
from pydantic import BaseModel, Field
import uvicorn
_HAS_FASTAPI = True
except Exception: # pragma: no cover
_HAS_FASTAPI = False
if _HAS_FASTAPI:
from hive import __version__
app = FastAPI(title="Hive Agent Memory", version=__version__)
stack = HiveStack(honey_comb=RuleFastHoneyComb())
class RouteRequest(BaseModel):
goal: str = Field(default="")
available_tools: list[str] = Field(default_factory=list)
step: int = Field(default=0, ge=0)
class RouteResponse(BaseModel):
tool: str
args: dict[str, Any]
confidence: float
escalated: bool
source: str
class CompressRequest(BaseModel):
role: str
content: str
class CompressResponse(BaseModel):
role: str
content: str
label: str
class RememberRequest(BaseModel):
key: str
value: Any
trust: float = Field(default=1.0, ge=0.0, le=1.0)
@app.post("/route", response_model=RouteResponse)
async def route(req: RouteRequest) -> RouteResponse:
d = stack.route(req.model_dump())
return RouteResponse(
tool=d.tool,
args=d.args,
confidence=d.confidence,
escalated=d.escalated,
source=d.source,
)
@app.post("/compress", response_model=CompressResponse)
async def compress(req: CompressRequest) -> CompressResponse:
c = stack.compress(req.role, req.content)
return CompressResponse(role=c.role, content=c.content, label=c.label)
@app.post("/remember")
async def remember(req: RememberRequest) -> dict:
stack.remember(req.key, req.value, trust=req.trust)
return {"status": "ok"}
@app.get("/recall")
async def recall(key: str) -> dict:
val = stack.recall(key)
return {"key": key, "value": val}
@app.get("/health")
async def health() -> dict:
return {"status": "alive"}
@app.get("/ready")
async def ready() -> JSONResponse:
try:
from hive.health import is_healthy
ready, _backends = is_healthy(stack)
if ready:
return JSONResponse({"status": "ready"})
except Exception:
pass
return JSONResponse({"status": "not_ready"}, status_code=503)
def main(argv: list[str] | None = None) -> int:
if not _HAS_FASTAPI:
print("ERROR: fastapi/uvicorn not installed. Run: pip install fastapi uvicorn")
return 1
p = argparse.ArgumentParser(description="Hive REST API server")
p.add_argument("--host", default="127.0.0.1")
p.add_argument("--port", type=int, default=8080)
args = p.parse_args(argv)
uvicorn.run(app, host=args.host, port=args.port)
return 0
if __name__ == "__main__":
raise SystemExit(main())