Skip to main content

Easy AGUI integration for FastHTML applications - as simple as pydantic-ai's to_web()!

Project description

ft-agui

Easy AGUI (Agent GUI) integration for FastHTML applications. Build real-time chat interfaces with AI agents as simple as pydantic-ai's to_web() but with full control and flexibility for FastHTML.

Features

  • Simple Setup - One line to integrate: agui = setup_agui(app, agent, state)
  • Real-time Chat - WebSocket-based communication with streaming responses
  • Customizable UI - FastHTML components with beautiful default styles
  • State Management - Thread-safe state with automatic UI updates
  • AGUI Protocol - Full compatibility with pydantic-ai's AGUI protocol
  • Multi-threading - Support for multiple conversation threads

Installation

pip install ft-agui

Quick Start

from fasthtml.common import *
from pydantic_ai import Agent
from ft_agui import setup_agui

# Create FastHTML app with WebSocket support
app, rt = fast_app(exts='ws')

# Create your agent
agent = Agent('openai:gpt-4o-mini', instructions='Be helpful and friendly')

# Setup AGUI with one line!
agui = setup_agui(app, agent)

@rt('/')
def index():
    return Titled(
        "My AI Chat",
        Container(
            agui.chat("my-thread")
        )
    )

serve()

Advanced Usage with State Management

from pydantic import BaseModel, Field
from pydantic_ai import Agent, RunContext, ToolReturn
from pydantic_ai.ui import StateDeps
from ft_agui import setup_agui
from typing import List

# Define your state model
class Note(BaseModel):
    title: str
    content: str

class ChatState(BaseModel):
    notes: List[Note] = Field(default_factory=list)
    topic: str = "General"

    def __ft__(self):
        """Custom FastHTML rendering"""
        return Card(
            H3(f"Topic: {self.topic}"),
            Ul(*[Li(f"{n.title}: {n.content}") for n in self.notes]),
            header="Session State"
        )

# Create agent with state
agent = Agent[StateDeps[ChatState]](
    'openai:gpt-4o-mini',
    instructions='You can take notes and manage topics.',
    deps_type=StateDeps[ChatState]
)

# Add tools for state management
@agent.tool
def add_note(ctx: RunContext[StateDeps[ChatState]], title: str, content: str) -> ToolReturn:
    ctx.deps.state.notes.append(Note(title=title, content=content))
    return ToolReturn(return_value=f"Added note: {title}")

# Setup AGUI with state
app, rt = fast_app(exts='ws')
agui = setup_agui(app, agent, ChatState(), ChatState)

@rt('/')
def index():
    thread_id = "main-thread"
    return Container(
        Grid(
            Div(agui.state(thread_id)),      # State display
            Div(agui.chat(thread_id, autoscroll=True))  # Chat interface
        )
    )

Thread Management

ft-agui provides flexible thread management for handling multiple conversations:

Option 1: Explicit Thread IDs

# Pass thread_id directly
agui.state(thread_id="user-123")
agui.chat(thread_id="user-123", autoscroll=True)

Option 2: Thread Instances (Recommended)

# Create a thread instance
thread = agui.thread("user-123")
return Container(
    Grid(
        Div(thread.state()),
        Div(thread.chat(autoscroll=True))
    )
)

Option 3: Multiple Threads

# Manage multiple conversations
thread1 = agui.thread("support-chat")
thread2 = agui.thread("sales-chat")

return Container(
    Div(thread1.chat()),
    Div(thread2.chat())
)

Custom State Rendering

Define custom __ft__() methods on your Pydantic models for rich UI rendering:

class TodoItem(BaseModel):
    task: str
    done: bool = False

    def __ft__(self):
        return Li(
            Input(type="checkbox", checked=self.done),
            Span(self.task, style="text-decoration: line-through;" if self.done else "")
        )

class TodoList(BaseModel):
    items: List[TodoItem] = []

    def __ft__(self):
        return Card(
            Ul(*[item.__ft__() for item in self.items]),
            header="My Todos"
        )

API Reference

setup_agui(app, agent, initial_state=None, state_type=None)

Initialize AGUI for your FastHTML application.

Parameters:

  • app: FastHTML application (must have exts='ws' enabled)
  • agent: pydantic-ai Agent instance
  • initial_state: Initial state instance (optional)
  • state_type: State type class for validation (optional)

Returns: AGUISetup instance

AGUISetup.chat(thread_id, autoscroll=False, **kwargs)

Create a chat interface component.

Parameters:

  • thread_id: Thread identifier
  • autoscroll: Auto-scroll to bottom on new messages
  • **kwargs: Additional HTML attributes

AGUISetup.state(thread_id, **kwargs)

Create a state display component.

Parameters:

  • thread_id: Thread identifier
  • **kwargs: Additional HTML attributes

AGUISetup.thread(thread_id)

Create a thread instance for cleaner component management.

Parameters:

  • thread_id: Thread identifier (auto-generated if None)

Returns: AGUIThread instance with chat() and state() methods

Requirements

  • Python 3.11+
  • FastHTML with WebSocket support
  • pydantic-ai
  • ag-ui (AGUI protocol)

Examples

Check out the /example directory for complete examples:

  • simple_agui.py - Basic chat interface
  • agui_with_state.py - State management with tools
  • thread_management.py - Multi-thread handling

License

MIT

Credits

Built using FastHTML and currently supporting pydantic-ai

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ft_agui-0.1.0.tar.gz (200.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ft_agui-0.1.0-py3-none-any.whl (13.3 kB view details)

Uploaded Python 3

File details

Details for the file ft_agui-0.1.0.tar.gz.

File metadata

  • Download URL: ft_agui-0.1.0.tar.gz
  • Upload date:
  • Size: 200.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.13

File hashes

Hashes for ft_agui-0.1.0.tar.gz
Algorithm Hash digest
SHA256 79622a5d31a34280d27a44818f8f783965b5e530b5cd124d731cccc53e1cc478
MD5 f8918d24498e9a9b911712dd6321602d
BLAKE2b-256 f84fdd81c6961f2612008382b68596592b65aeef4e3a7b85bf8502a5fa37e3c5

See more details on using hashes here.

File details

Details for the file ft_agui-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: ft_agui-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 13.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.13

File hashes

Hashes for ft_agui-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a14666b73e4d070c3f1362770ae267e21326810cdc89f6454385203ea1f4bc94
MD5 2ea0302037200ef623f4820642aa2f5b
BLAKE2b-256 7f0c56513d75c0513a59fbb58eb1c1a2e366b43df58bec2237bbed22db6edad2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page