"""Business-first presentation helpers for the governed tool-using agent demo.""" from html import escape from src.schemas import AgentRunEvent, AgentRunResult APP_CSS = """ .gradio-container { max-width: 1080px !important; margin: 0 auto !important; } .agent-shell { max-width: 1080px; margin: 0 auto; } .hero { padding: 8px 0 4px 0; } .hero h1 { font-size: 2.35rem; line-height: 1.08; margin-bottom: 0.55rem; } .hero p { font-size: 1.08rem; line-height: 1.65; max-width: 820px; } .business-card, .tool-card, .authority-card, .activity-panel, .summary-card { border: 1px solid var(--border-color-primary); border-radius: 14px; padding: 18px; margin: 10px 0; background: var(--background-fill-secondary); } .tool-grid { display: grid; grid-template-columns: 1fr; gap: 10px; } .tool-name { font-size: 1.06rem; font-weight: 700; margin-bottom: 5px; } .tool-boundary { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-top: 10px; } .boundary-good, .boundary-no { padding: 10px 12px; border-radius: 10px; background: var(--background-fill-primary); } .authority-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; } .authority-title { font-weight: 700; margin-bottom: 8px; } .activity-header { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; } .status-badge { display: inline-block; border-radius: 999px; padding: 4px 10px; font-size: 0.78rem; font-weight: 700; letter-spacing: 0.04em; } .status-idle { background: rgba(107, 114, 128, 0.15); } .status-running { background: rgba(245, 158, 11, 0.16); } .status-complete { background: rgba(34, 197, 94, 0.16); } .status-stopped { background: rgba(239, 68, 68, 0.14); } .spinner { width: 15px; height: 15px; border: 2px solid rgba(120,120,120,0.3); border-top-color: currentColor; border-radius: 50%; animation: spin 0.85s linear infinite; } @keyframes spin { to { transform: rotate(360deg); } } .event-feed { display: flex; flex-direction: column; gap: 8px; } .event-feed-complete { max-height: 380px; overflow-y: auto; padding-right: 4px; } .event-row { padding: 10px 12px; border-radius: 10px; background: var(--background-fill-primary); line-height: 1.45; } .event-label { display: inline-block; min-width: 126px; font-weight: 700; } .event-meta { opacity: 0.72; font-size: 0.86rem; margin-top: 4px; } .summary-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; margin-top: 8px; } .metric { text-align: center; padding: 12px; border-radius: 10px; background: var(--background-fill-primary); } .metric strong { display: block; font-size: 1.35rem; } @media (max-width: 760px) { .authority-grid, .tool-boundary, .summary-grid { grid-template-columns: 1fr; } .hero h1 { font-size: 1.9rem; } } """ HERO_HTML = """

How can an AI use company tools without being given the keys to everything?

This demo shows a governed tool-using agent. The model can decide that it needs a capability, but it cannot execute arbitrary code, query arbitrary databases, or reach arbitrary systems. Every requested action must cross an application-controlled authorization and execution boundary.

""" BUSINESS_CASE_HTML = """
Business scenario
Imagine an operations copilot helping a team answer routine questions that may require internal inventory data, deterministic calculations, or an approved external reference API. Giving the model unrestricted system access would be convenientโ€”but unsafe. This agent demonstrates delegated authority instead: the model proposes; application code decides what may actually run.
""" TOOL_BELT_HTML = """
๐Ÿงฎ Calculator โ€” deterministic local computation
Safely evaluates approved arithmetic expressions through a constrained AST parser.
Allowed
Approved arithmetic operations
Not allowed
Arbitrary Python or unrestricted eval()
๐Ÿ“ฆ Inventory โ€” controlled internal data access
Searches a small SQLite inventory database through a narrow parameterized interface.
Allowed
Item/category inventory search
Not allowed
Arbitrary SQL, writes, or database administration
๐ŸŒ Country lookup โ€” controlled external API access
Retrieves structured country information through the approved World Bank endpoint.
Allowed
Structured country lookup
Not allowed
Arbitrary URLs, browsing, or open-ended network access
""" AUTHORITY_HTML = """
๐Ÿค– The model controls
๐Ÿ›ก๏ธ Application code controls
""" ARCHITECTURE_MARKDOWN = """ ### Architecture ```text User request โ†“ LLM proposes an action โ†“ Structured ToolCall โ†“ Application authorization boundary โ”œโ”€ blocked โ†’ normalized blocked result + audit record โ””โ”€ approved โ†“ Controlled executor โ†“ Approved capability โ†“ Normalized ToolResult + audit record โ†“ Result returned to model โ†“ Additional approved tool request or final answer ``` The model never receives unrestricted shell, Python, filesystem, SQL, or arbitrary network execution. The reusable primitive is the **application-owned capability boundary**. """ SECURITY_MARKDOWN = """ ### Security and failure semantics The demo distinguishes two classes of failure that should not be conflated: **Blocked before execution** โ€” the application refuses the model's request because the tool is not registered or the arguments violate the approved schema. The underlying capability never runs. **Approved but execution failed** โ€” the requested capability was allowed, but the tool encountered a runtime or dependency error such as division by zero or an external API failure. Both outcomes are normalized into structured results and written to the current-run audit trail. This is a portfolio demonstration of a narrow trust boundary, not a production-certified sandbox or multi-tenant security platform. """ EVENT_LABELS = { "run_started": "Request accepted", "model_thinking": "AI deciding", "tool_requested": "Model requested", "tool_approved": "Application approved", "tool_blocked": "Application blocked", "tool_succeeded": "Tool completed", "tool_failed": "Execution failed", "final_answer": "Final response", "max_rounds_reached": "Safety stop", } def idle_activity_html() -> str: """Return the resting state shown before a user starts a request.""" return """
READY Live Controlled Execution
Run a request to watch model proposals cross the application trust boundary.
""" def starting_activity_html() -> str: """Return an immediate visible state before provider work begins.""" return """
RUNNING Live Controlled Execution
Request submitted. Preparing the approved tool registry and model context.
""" def render_activity( events: list[AgentRunEvent], complete: bool = False, ) -> str: """Render real run events as a readable control-boundary timeline.""" stopped = bool( events and events[-1].event in {"max_rounds_reached", "tool_blocked"} and not any(event.event == "final_answer" for event in events) ) if complete: badge_class = "status-complete" if not stopped else "status-stopped" badge_text = "COMPLETE" if not stopped else "STOPPED" spinner = "" visible_events = events feed_class = "event-feed event-feed-complete" else: badge_class = "status-running" badge_text = "RUNNING" spinner = '' visible_events = events[-9:] feed_class = "event-feed" rows = [] for event in visible_events: label = EVENT_LABELS[event.event] message = escape(event.message) meta_parts = [] if event.round_index is not None: meta_parts.append(f"round {event.round_index}") if event.call is not None: meta_parts.append(f"tool: {escape(event.call.tool_name)}") meta_parts.append(f"call: {escape(event.call.call_id)}") if event.result is not None: meta_parts.append(f"status: {escape(event.result.status)}") meta = "" if meta_parts: meta = ( '
' + " ยท ".join(meta_parts) + "
" ) rows.append( '
' f'{escape(label)}' f"{message}{meta}
" ) if not rows: rows.append( '
Waiting for the run to begin.
' ) return ( '
' '
' f'{badge_text}' f"{spinner}Live Controlled Execution" "
" f'
{"".join(rows)}
' "
" ) def render_summary(result: AgentRunResult | None) -> str: """Summarize the authority boundary for the completed request.""" if result is None: return '
Run a request to see the execution summary.
' succeeded = sum(item.status == "success" for item in result.tool_results) blocked = sum(item.status == "blocked" for item in result.tool_results) failed = sum(item.status == "error" for item in result.tool_results) return f"""
Controlled execution summary
{len(result.tool_calls)}requested
{succeeded}executed
{blocked}blocked
{failed}failed

A model request is not authority. Every tool call shown above had to pass through the application-owned registry and argument checks before an approved capability could execute.

"""