227 lines
7.5 KiB
Python
227 lines
7.5 KiB
Python
|
|
"""Dual-Layer Integration for SuperDave Server.
|
||
|
|
|
||
|
|
Adds symbolic cognition layer to FastAPI endpoints:
|
||
|
|
- /api/symbolic/activate - Activate glyph from intent
|
||
|
|
- /api/symbolic/status - Get symbolic engine status
|
||
|
|
- /api/symbolic/glyphs - List active glyphs
|
||
|
|
- Enhanced /api/chat with glyph routing
|
||
|
|
- Enhanced /api/generate-image with glyph routing
|
||
|
|
|
||
|
|
Usage:
|
||
|
|
from dual_layer_integration import setup_dual_layer
|
||
|
|
setup_dual_layer(app)
|
||
|
|
"""
|
||
|
|
|
||
|
|
import logging
|
||
|
|
from typing import Dict, Any, Optional
|
||
|
|
from fastapi import FastAPI, HTTPException, Header
|
||
|
|
|
||
|
|
logger = logging.getLogger(__name__)
|
||
|
|
|
||
|
|
|
||
|
|
def setup_dual_layer(app: FastAPI):
|
||
|
|
"""Setup dual-layer endpoints on FastAPI app."""
|
||
|
|
|
||
|
|
@app.get("/api/symbolic/status")
|
||
|
|
async def get_symbolic_status():
|
||
|
|
"""Get symbolic engine status (glyphs, resonance, VRAM)."""
|
||
|
|
try:
|
||
|
|
from dual_layer.symbolic_engine import get_symbolic_engine
|
||
|
|
|
||
|
|
engine = get_symbolic_engine()
|
||
|
|
status = await engine.get_status()
|
||
|
|
|
||
|
|
return {
|
||
|
|
"status": "operational",
|
||
|
|
"symbolic_layer": status,
|
||
|
|
}
|
||
|
|
except Exception as e:
|
||
|
|
logger.error(f"Symbolic status error: {e}")
|
||
|
|
return {
|
||
|
|
"status": "error",
|
||
|
|
"error": str(e),
|
||
|
|
}
|
||
|
|
|
||
|
|
@app.get("/api/symbolic/glyphs")
|
||
|
|
async def get_active_glyphs():
|
||
|
|
"""Get list of active glyphs."""
|
||
|
|
try:
|
||
|
|
from dual_layer.symbolic_engine import get_symbolic_engine
|
||
|
|
|
||
|
|
engine = get_symbolic_engine()
|
||
|
|
active_glyphs = engine.get_active_glyphs()
|
||
|
|
|
||
|
|
return {
|
||
|
|
"status": "success",
|
||
|
|
"active_glyphs": active_glyphs,
|
||
|
|
"count": len(active_glyphs),
|
||
|
|
}
|
||
|
|
except Exception as e:
|
||
|
|
logger.error(f"Active glyphs error: {e}")
|
||
|
|
return {
|
||
|
|
"status": "error",
|
||
|
|
"error": str(e),
|
||
|
|
}
|
||
|
|
|
||
|
|
@app.post("/api/symbolic/activate")
|
||
|
|
async def activate_glyph(
|
||
|
|
request: Dict[str, Any],
|
||
|
|
authorization: Optional[str] = Header(None)
|
||
|
|
):
|
||
|
|
"""Activate glyph from user intent.
|
||
|
|
|
||
|
|
Request:
|
||
|
|
{
|
||
|
|
"intent": "I need creative image generation",
|
||
|
|
"request_type": "image", # chat, image, video, vision
|
||
|
|
"metrics": {...} # optional, auto-calculated if omitted
|
||
|
|
}
|
||
|
|
|
||
|
|
Returns:
|
||
|
|
{
|
||
|
|
"status": "success",
|
||
|
|
"glyph_id": "G001",
|
||
|
|
"specialized_type": "aether_node",
|
||
|
|
"model": "forge",
|
||
|
|
"priority": 10.0,
|
||
|
|
"resonance_score": 95.5,
|
||
|
|
"power_boost": 387.95,
|
||
|
|
"superpower_count": 152,
|
||
|
|
"routing": {...}
|
||
|
|
}
|
||
|
|
"""
|
||
|
|
user_id = authorization.replace("Bearer ", "") if authorization else "anonymous"
|
||
|
|
|
||
|
|
try:
|
||
|
|
from dual_layer.symbolic_engine import get_symbolic_engine
|
||
|
|
|
||
|
|
engine = get_symbolic_engine()
|
||
|
|
|
||
|
|
intent = request.get("intent", "")
|
||
|
|
request_type = request.get("request_type", "chat")
|
||
|
|
metrics = request.get("metrics")
|
||
|
|
|
||
|
|
if not intent:
|
||
|
|
raise HTTPException(status_code=400, detail="intent required")
|
||
|
|
|
||
|
|
logger.info(
|
||
|
|
f"Glyph activation request from {user_id}: "
|
||
|
|
f"intent='{intent[:50]}...', type={request_type}"
|
||
|
|
)
|
||
|
|
|
||
|
|
# Activate glyph (async)
|
||
|
|
result = await engine.activate_from_intent(
|
||
|
|
user_intent=intent,
|
||
|
|
metrics=metrics,
|
||
|
|
request_type=request_type
|
||
|
|
)
|
||
|
|
|
||
|
|
if result is None:
|
||
|
|
return {
|
||
|
|
"status": "failed",
|
||
|
|
"reason": "VRAM unavailable or activation rejected",
|
||
|
|
}
|
||
|
|
|
||
|
|
return {
|
||
|
|
"status": "success",
|
||
|
|
"glyph_id": result.glyph_id,
|
||
|
|
"specialized_type": result.specialized_type,
|
||
|
|
"model": result.model,
|
||
|
|
"priority": result.priority,
|
||
|
|
"resonance_score": result.resonance_score,
|
||
|
|
"power_boost": result.power_boost,
|
||
|
|
"superpower_count": len(result.superpower_ids),
|
||
|
|
"routing": {
|
||
|
|
"constraints": result.constraints,
|
||
|
|
"enhancements": result.enhancements,
|
||
|
|
"vram_budget": result.vram_budget,
|
||
|
|
},
|
||
|
|
}
|
||
|
|
|
||
|
|
except Exception as e:
|
||
|
|
logger.error(f"Glyph activation error: {e}")
|
||
|
|
raise HTTPException(status_code=500, detail=str(e))
|
||
|
|
|
||
|
|
@app.post("/api/symbolic/deactivate")
|
||
|
|
async def deactivate_glyph(
|
||
|
|
request: Dict[str, Any],
|
||
|
|
authorization: Optional[str] = Header(None)
|
||
|
|
):
|
||
|
|
"""Deactivate a glyph.
|
||
|
|
|
||
|
|
Request:
|
||
|
|
{
|
||
|
|
"glyph_id": "G001"
|
||
|
|
}
|
||
|
|
"""
|
||
|
|
user_id = authorization.replace("Bearer ", "") if authorization else "anonymous"
|
||
|
|
|
||
|
|
try:
|
||
|
|
from dual_layer.symbolic_engine import get_symbolic_engine
|
||
|
|
|
||
|
|
engine = get_symbolic_engine()
|
||
|
|
glyph_id = request.get("glyph_id")
|
||
|
|
|
||
|
|
if not glyph_id:
|
||
|
|
raise HTTPException(status_code=400, detail="glyph_id required")
|
||
|
|
|
||
|
|
success = await engine.deactivate_glyph(glyph_id)
|
||
|
|
|
||
|
|
return {
|
||
|
|
"status": "success" if success else "failed",
|
||
|
|
"glyph_id": glyph_id,
|
||
|
|
"deactivated": success,
|
||
|
|
}
|
||
|
|
|
||
|
|
except Exception as e:
|
||
|
|
logger.error(f"Glyph deactivation error: {e}")
|
||
|
|
raise HTTPException(status_code=500, detail=str(e))
|
||
|
|
|
||
|
|
# Enhanced endpoints with symbolic routing
|
||
|
|
|
||
|
|
@app.get("/api/symbolic/routing/summary")
|
||
|
|
async def get_routing_summary():
|
||
|
|
"""Get routing configuration summary."""
|
||
|
|
try:
|
||
|
|
from dual_layer.router import TYPE_ROUTING_MAP
|
||
|
|
|
||
|
|
# Get summary for all types
|
||
|
|
summaries = {}
|
||
|
|
for type_name, config in TYPE_ROUTING_MAP.items():
|
||
|
|
summaries[type_name] = {
|
||
|
|
"model": config.get("model"),
|
||
|
|
"vram_budget": config.get("vram_budget"),
|
||
|
|
"constraints": len(config.get("constraints", [])),
|
||
|
|
"enhancements": len(config.get("enhancements", [])),
|
||
|
|
"description": config.get("description"),
|
||
|
|
}
|
||
|
|
|
||
|
|
return {
|
||
|
|
"status": "success",
|
||
|
|
"type_summaries": summaries,
|
||
|
|
"total_types": len(summaries),
|
||
|
|
}
|
||
|
|
|
||
|
|
except Exception as e:
|
||
|
|
logger.error(f"Routing summary error: {e}")
|
||
|
|
return {
|
||
|
|
"status": "error",
|
||
|
|
"error": str(e),
|
||
|
|
}
|
||
|
|
|
||
|
|
logger.info("Dual-layer symbolic endpoints installed")
|
||
|
|
|
||
|
|
|
||
|
|
# Convenience function for easy integration
|
||
|
|
def integrate_with_server(app: FastAPI):
|
||
|
|
"""Integrate dual-layer system with existing server.
|
||
|
|
|
||
|
|
This enhances existing endpoints with symbolic routing:
|
||
|
|
- /api/chat → routes through glyph activation
|
||
|
|
- /api/generate-image → routes through glyph activation
|
||
|
|
- /api/generate-video → routes through glyph activation
|
||
|
|
- /api/vision → routes through glyph activation
|
||
|
|
"""
|
||
|
|
setup_dual_layer(app)
|
||
|
|
|
||
|
|
logger.info("Dual-layer integration complete")
|