Files
PraisonAIUI/examples/python/16-gateway-integration/app.py
T
praisonai-triage-agent[bot]andMervin Praison 6fd27d0ed7 feat: safe_print for examples to avoid cp1252 UnicodeEncodeError (fixes #231)
Add examples/python/_shared/console.py with safe_print/icon helpers that
degrade emoji to ASCII on legacy Windows code pages (cp1252) instead of
crashing, mirroring the CLI _supports_unicode/_icon pattern. Wire the helper
into the high-severity gateway (16, 17, 18) and channel (20-23) examples so
they start on default Windows PowerShell without PYTHONUTF8.

Co-authored-by: Mervin Praison <[email protected]>
2026-07-27 09:04:48 +00:00

209 lines
8.8 KiB
Python

"""PraisonAIUI Gateway Integration — Full Execution + Streaming.
This example uses AIUIGateway instead of create_app(), enabling:
- Real agent execution via praisonai
- WebSocket streaming at /ws
- Full dashboard with all features
- Live agent interaction from the browser
Architecture:
┌──────────────────────────────────────────────────────────┐
│ AIUIGateway (port 8083) │
│ │
│ ┌────────────┐ ┌──────────────────┐ ┌─────────────┐ │
│ │ Frontend │ │ Dashboard APIs │ │ WebSocket │ │
│ │ HTML/JS │ │ /api/* │ │ /ws │ │
│ │ Inspector │ │ 16 features │ │ streaming │ │
│ └──────┬─────┘ └───────┬──────────┘ └──────┬──────┘ │
│ │ │ │ │
│ └────────────────┴─────────────────────┘ │
│ │ │
│ ┌───────────┴──────────────┐ │
│ │ praisonai Gateway │ │
│ │ WebSocketGateway │ │
│ │ • Agent execution │ │
│ │ • LLM routing │ │
│ │ • Session management │ │
│ └──────────────────────────┘ │
└──────────────────────────────────────────────────────────┘
Usage:
conda activate test
pip install -e /path/to/PraisonAIUI
pip install praisonai praisonaiagents
export OPENAI_API_KEY=sk-... # or any supported LLM key
python app.py
# Open http://localhost:8083
# Dashboard + Feature Inspector + live agent chat via WebSocket
"""
import asyncio
import os
import sys
# ── Import praisonaiui ──────────────────────────────────────
import praisonaiui as aiui
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
from _shared.console import icon, safe_print
# ── Check gateway availability ──────────────────────────────
try:
from praisonaiui.integration import AIUIGateway
from praisonaiagents import Agent
GATEWAY_OK = True
except ImportError as e:
safe_print(f"{icon('⚠️', '[WARN]')} Gateway dependencies missing: {e}")
safe_print(" Install with: pip install praisonai praisonaiagents")
safe_print(" Falling back to standalone mode.")
GATEWAY_OK = False
# ── Set dashboard style ─────────────────────────────────────
aiui.set_style("dashboard")
# ── Agent definitions ───────────────────────────────────────
# These define the agents that will be registered with both
# the gateway (for execution) and the dashboard (for display)
AGENTS = [
{
"agent_id": "researcher",
"name": "Researcher",
"description": "Research assistant for finding and summarizing information",
"instructions": (
"You are a research assistant. When asked a question, "
"provide a well-structured answer with key facts. "
"Keep responses concise but informative."
),
"model": "gpt-4o-mini",
"icon": "🔬",
},
{
"agent_id": "writer",
"name": "Writer",
"description": "Creative writer for content generation",
"instructions": (
"You are a creative writer. When given a topic, "
"write engaging content in a clear, professional style. "
"Keep responses focused and well-organized."
),
"model": "gpt-4o-mini",
"icon": "✍️",
},
{
"agent_id": "coder",
"name": "Coder",
"description": "Coding assistant for Python, JavaScript, and more",
"instructions": (
"You are a coding assistant. Write clean, well-commented "
"code with explanations. Support Python, JavaScript, "
"and other popular languages."
),
"model": "gpt-4o-mini",
"icon": "💻",
},
]
# ── Register custom pages ───────────────────────────────────
@aiui.page("analytics", title="Analytics", icon="📊", group="Custom")
async def analytics_page():
"""Custom analytics page — same protocol as standalone mode."""
return aiui.layout([
aiui.columns([
aiui.card("Total Users", value=142, footer="+12% this week"),
aiui.card("API Calls", value="3,847", footer="Last 24h"),
aiui.card("Avg Latency", value="47ms", footer="-5ms from yesterday"),
aiui.card("Success Rate", value="99.2%", footer="✓ Healthy"),
]),
aiui.table(
headers=["Agent", "Tasks Completed", "Status"],
rows=[
["Researcher", 15, "Active"],
["Code Writer", 8, "Idle"],
["Reviewer", 12, "Active"],
],
),
])
def _register_agents_in_dashboard():
"""Register gateway agents in the agents_crud feature storage.
This bridges the gateway's internal agent registry with the
dashboard CRUD feature, so agents appear in the Agents page.
"""
from praisonaiui.features.agents import get_agent_registry
registry = get_agent_registry()
existing = registry.list_all()
existing_names = {a.get("name") for a in existing}
for agent_def in AGENTS:
# Skip if already registered
if agent_def["name"] in existing_names:
continue
registry.create({
"name": agent_def["name"],
"description": agent_def["description"],
"instructions": agent_def["instructions"],
"model": agent_def["model"],
"icon": agent_def["icon"],
})
safe_print(f" {icon('✓', '[OK]')} Dashboard: {agent_def['name']}")
# ── Main ────────────────────────────────────────────────────
async def main():
host = os.getenv("HOST", "127.0.0.1")
if GATEWAY_OK:
safe_print(f"{icon('🚀', '[START]')} Starting with Gateway mode (full execution + streaming)")
safe_print()
# Create the gateway
gateway = AIUIGateway(
host=host,
port=8083,
)
# ── Register agents with gateway (for execution) ────
for agent_def in AGENTS:
agent = Agent(
name=agent_def["name"],
instructions=agent_def["instructions"],
llm=agent_def["model"],
self_reflect=False,
)
gateway.register_agent(agent, agent_id=agent_def["agent_id"])
safe_print(f" {icon('✓', '[OK]')} Gateway: {agent_def['name']} ({agent_def['model']})")
# ── Register agents in dashboard CRUD storage ───────
_register_agents_in_dashboard()
safe_print()
safe_print(" Dashboard: http://localhost:8083")
safe_print(" WebSocket: ws://localhost:8083/ws")
safe_print(f" Inspector: http://localhost:8083 {icon('→', '->')} {icon('🔍', '')} Feature Inspector")
safe_print()
# Start the gateway — this runs the full server
await gateway.start()
else:
# Fallback to standalone mode (same as example 15)
safe_print(f"{icon('📦', '[INFO]')} Starting in standalone mode (CRUD only, no agent execution)")
from praisonaiui.server import create_app
import uvicorn
_register_agents_in_dashboard()
app = create_app()
config = uvicorn.Config(app, host=host, port=8083, log_level="info")
server = uvicorn.Server(config)
await server.serve()
if __name__ == "__main__":
asyncio.run(main())