FerrellSyntheticIntelligence
Add model router: task classification + model routing with 5 categories (code/reasoning/research/creative/general), router tool in server.py, router tab in Gradio UI
7764be8 | """ | |
| Vitalis LOREIN MCP Server β Cognitive toolkit for any model. | |
| The server has no model, no weights, no LLM calls. | |
| The model (client) calls these tools to improve its own output. | |
| """ | |
| import json | |
| import traceback | |
| from mcp.server.fastmcp import FastMCP | |
| from pipeline.lorein import LOREIN | |
| mcp = FastMCP("Vitalis LOREIN Server") | |
| lorein = LOREIN() | |
| def safe_call(fn, *args, **kwargs): | |
| """Wrap MCP tools with structured error handling.""" | |
| try: | |
| return fn(*args, **kwargs) | |
| except Exception as e: | |
| return json.dumps({ | |
| "error": str(e), | |
| "traceback": traceback.format_exc(), | |
| }, indent=2) | |
| # ββ Chat / Cognitive Prep ββ | |
| def lorein_chat(message: str) -> str: | |
| return safe_call(lambda: json.dumps(lorein.prepare_chat(message), indent=2)) | |
| def lorein_truth_check(response: str, context: str = "") -> str: | |
| return safe_call(lambda: json.dumps(lorein.truth_check(response, context), indent=2)) | |
| # ββ MIRROR Tools ββ | |
| def lorein_mirror_think(input_text: str) -> str: | |
| return safe_call(lambda: json.dumps({ | |
| "narrative": lorein.mirror.think(input_text)[0], | |
| "threads": lorein.mirror.think(input_text)[1], | |
| }, indent=2)) | |
| # ββ Journal Tools ββ | |
| def lorein_journal_query(action: str | None = None, actor: str | None = None, limit: int = 50) -> str: | |
| return safe_call(lambda: json.dumps({ | |
| "entries": lorein.journal.query(action, actor, limit), | |
| "total": lorein.journal.size(), | |
| "chain_integrity": lorein.journal.verify_chain(), | |
| }, indent=2)) | |
| def lorein_journal_verify() -> str: | |
| return safe_call(lambda: json.dumps({ | |
| "chain_integrity": lorein.journal.verify_chain(), | |
| "entry_count": lorein.journal.size(), | |
| "last_hash": lorein.journal.last_hash(), | |
| }, indent=2)) | |
| # ββ Memory Tools ββ | |
| def lorein_memory_query(query: str, memory_type: str = "semantic") -> str: | |
| return safe_call(lambda: _query_memory(query, memory_type)) | |
| def _query_memory(query: str, memory_type: str): | |
| if memory_type == "semantic": | |
| results = lorein.semantic.query(query, top_k=10) | |
| return json.dumps({ | |
| "type": "semantic", | |
| "results": [t.to_dict() for t in results], | |
| "total_facts": lorein.semantic.count(), | |
| }, indent=2) | |
| elif memory_type == "episodic": | |
| results = lorein.episodic.query(limit=20) | |
| return json.dumps({ | |
| "type": "episodic", | |
| "results": results, | |
| "total_events": lorein.episodic.count(), | |
| }, indent=2) | |
| elif memory_type == "procedural": | |
| results = lorein.procedural.retrieve(query) | |
| return json.dumps({ | |
| "type": "procedural", | |
| "results": [{"name": n, "skill": s} for n, s in results], | |
| "total_skills": lorein.procedural.count(), | |
| }, indent=2) | |
| return json.dumps({"error": f"Unknown memory type: {memory_type}"}) | |
| def lorein_memory_consolidate(force: bool = True) -> str: | |
| return safe_call(lambda: json.dumps(lorein.consolidation.consolidate(force=force), indent=2)) | |
| # ββ Room Tools ββ | |
| def lorein_room_create(sandbox_id: str | None = None) -> str: | |
| return safe_call(lambda: json.dumps({"sandbox_id": lorein.room.create(sandbox_id)})) | |
| def lorein_room_execute(code: str, language: str = "python", sandbox_id: str | None = None) -> str: | |
| return safe_call(lambda: json.dumps(lorein.run_code(code, language, sandbox_id), indent=2)) | |
| def lorein_room_write(sandbox_id: str, path: str, content: str) -> str: | |
| return safe_call(lambda: json.dumps(lorein.room.write_file(sandbox_id, path, content))) | |
| def lorein_room_read(sandbox_id: str, path: str) -> str: | |
| return safe_call(lambda: json.dumps(lorein.room.read_file(sandbox_id, path))) | |
| def lorein_room_destroy(sandbox_id: str) -> str: | |
| return safe_call(lambda: json.dumps({"destroyed": lorein.room.destroy(sandbox_id)})) | |
| def lorein_room_list() -> str: | |
| return safe_call(lambda: json.dumps({"sandboxes": lorein.room.list_sandboxes()})) | |
| # ββ Debug Loop Tool ββ | |
| def lorein_debug_loop(query: str, code: str, language: str = "python", | |
| max_iterations: int = 3, session_id: str | None = None, | |
| sandbox_id: str | None = None) -> str: | |
| return safe_call(lambda: json.dumps( | |
| lorein.debug_loop(query, code, language, max_iterations, sandbox_id), indent=2 | |
| )) | |
| # ββ Active Inference Tools ββ | |
| def lorein_inference_state() -> str: | |
| return safe_call(lambda: json.dumps(lorein.inference.state(), indent=2)) | |
| def lorein_empowerment() -> str: | |
| return safe_call(lambda: json.dumps({ | |
| "empowerment": round(lorein.empowerment.compute(), 3), | |
| "should_explore": lorein.inference.should_explore(), | |
| "exploration_urgency": lorein.inference.exploration_urgency(), | |
| }, indent=2)) | |
| # ββ Governor Tools ββ | |
| def lorein_governor_evaluate(action: str, context: str = "") -> str: | |
| return safe_call(lambda: json.dumps(lorein.governor.evaluate(action, context), indent=2)) | |
| def lorein_governor_add_constraint(name: str, patterns: str, cost: float) -> str: | |
| return safe_call(lambda: _add_constraint(name, patterns, cost)) | |
| def _add_constraint(name: str, patterns: str, cost: float): | |
| pat_list = [p.strip() for p in patterns.split(",")] | |
| lorein.governor.add_constraint(name, pat_list, cost) | |
| return json.dumps({"added": name, "patterns": pat_list, "cost": cost}) | |
| # ββ Shadow Mode Tools ββ | |
| def lorein_shadow_create(name: str, config: str = "{}") -> str: | |
| return safe_call(lambda: _create_shadow(name, config)) | |
| def _create_shadow(name: str, config: str): | |
| parsed = json.loads(config) | |
| sid = lorein.shadow.create_shadow(name, parsed) | |
| return json.dumps({"shadow_id": sid}) | |
| def lorein_shadow_compare(live_decision: str, shadow_id: str) -> str: | |
| return safe_call(lambda: _compare_shadow(live_decision, shadow_id)) | |
| def _compare_shadow(live_decision: str, shadow_id: str): | |
| live = json.loads(live_decision) | |
| shadow_agent = lorein.shadow.get_shadow(shadow_id) | |
| if not shadow_agent or not shadow_agent["decisions"]: | |
| return json.dumps({"error": "No shadow decisions available"}) | |
| shadow_dec = shadow_agent["decisions"][-1]["decision"] | |
| result = lorein.shadow.compare(live, shadow_dec) | |
| return json.dumps(result, indent=2) | |
| # ββ Perception Tools ββ | |
| def lorein_desktop_context() -> str: | |
| return safe_call(lambda: _get_desktop()) | |
| def _get_desktop(): | |
| from perception import get_desktop_context, list_recent_activity | |
| ctx = get_desktop_context() | |
| ctx["recent_activity"] = list_recent_activity(10) | |
| return json.dumps(ctx, indent=2) | |
| def lorein_search_memory(query: str, path: str | None = None) -> str: | |
| return safe_call(lambda: _search_mem(query, path)) | |
| def _search_mem(query: str, path: str | None): | |
| from perception import search_content | |
| results = search_content(query, path) | |
| return json.dumps({"query": query, "results": results, "count": len(results)}, indent=2) | |
| # ββ Router Tools ββ | |
| from router import route as _route_task, classify_task, get_routing_history | |
| from router import TASK_ROUTES, TASK_CATEGORIES | |
| def vitalis_route(message: str, preferred_model: str = "") -> str: | |
| return safe_call(lambda: json.dumps({ | |
| "routing": _route_task(message, preferred_model or None), | |
| "available_tasks": list(TASK_ROUTES.keys()), | |
| }, indent=2)) | |
| def vitalis_classify(message: str) -> str: | |
| return safe_call(lambda: json.dumps({ | |
| "task": classify_task(message), | |
| "categories": {k: v for k, v in TASK_CATEGORIES.items()}, | |
| }, indent=2)) | |
| # ββ Identity Tools ββ | |
| def lorein_identity() -> str: | |
| return safe_call(lambda: json.dumps(lorein.get_identity(), indent=2)) | |
| def lorein_integrity_check() -> str: | |
| return safe_call(lambda: json.dumps(lorein.verify_integrity(), indent=2)) | |
| # ββ Resources ββ | |
| def resource_identity() -> str: | |
| return json.dumps(lorein.get_identity(), indent=2) | |
| def resource_journal() -> str: | |
| entries = lorein.journal.query(limit=20) | |
| return json.dumps({ | |
| "entries": entries, | |
| "total": lorein.journal.size(), | |
| "chain_integrity": lorein.journal.verify_chain(), | |
| }, indent=2) | |
| def resource_inference() -> str: | |
| return json.dumps(lorein.inference.state(), indent=2) | |
| def main(): | |
| mcp.run() | |
| if __name__ == "__main__": | |
| main() | |