|
| 1 | +import asyncio |
| 2 | +from textual.app import App, ComposeResult |
| 3 | +from textual.containers import VerticalScroll |
| 4 | +from textual.binding import Binding |
| 5 | + |
| 6 | +from agent_chat_cli.components.header import Header |
| 7 | +from agent_chat_cli.components.chat_history import ChatHistory, MessagePosted |
| 8 | +from agent_chat_cli.components.thinking_indicator import ThinkingIndicator |
| 9 | +from agent_chat_cli.components.user_input import UserInput |
| 10 | +from agent_chat_cli.utils import AgentLoop |
| 11 | +from agent_chat_cli.utils.message_bus import MessageBus |
| 12 | + |
| 13 | +from dotenv import load_dotenv |
| 14 | + |
| 15 | +load_dotenv() |
| 16 | + |
| 17 | + |
| 18 | +class AgentChatCLIApp(App): |
| 19 | + CSS_PATH = "utils/styles.tcss" |
| 20 | + |
| 21 | + BINDINGS = [Binding("ctrl+c", "quit", "Quit", show=False, priority=True)] |
| 22 | + |
| 23 | + def __init__(self) -> None: |
| 24 | + super().__init__() |
| 25 | + |
| 26 | + self.message_bus = MessageBus(self) |
| 27 | + |
| 28 | + self.agent_loop = AgentLoop( |
| 29 | + on_message=self.message_bus.handle_agent_message, |
| 30 | + ) |
| 31 | + |
| 32 | + def compose(self) -> ComposeResult: |
| 33 | + with VerticalScroll(id="container"): |
| 34 | + yield Header() |
| 35 | + yield ChatHistory() |
| 36 | + yield ThinkingIndicator() |
| 37 | + yield UserInput(query=self.agent_loop.query) |
| 38 | + |
| 39 | + async def on_mount(self) -> None: |
| 40 | + asyncio.create_task(self.agent_loop.start()) |
| 41 | + |
| 42 | + async def on_message_posted(self, event: MessagePosted) -> None: |
| 43 | + await self.message_bus.on_message_posted(event) |
| 44 | + |
| 45 | + |
| 46 | +def main(): |
| 47 | + app = AgentChatCLIApp() |
| 48 | + app.run() |
| 49 | + |
| 50 | + |
| 51 | +if __name__ == "__main__": |
| 52 | + main() |
0 commit comments