# Development guide ## Project layout | File | Purpose | | --- | --- | | `run_example.py` | Entry point that calls `ai_chat.main()` | | `ai_chat.py` | CLI, Textual UI, conversation loop, API requests, exports, and statistics | | `test_ai_chat.py` | Unit and Textual integration tests with mock HTTP responses | | `.env.example` | Example runtime configuration | ## Conversation and request flow `main()` loads `.env`, parses CLI overrides, validates the connections, prompt, and turn count, and creates `AIChatApp`. AI 1 and AI 2 alternate for at most `max_turns` replies. For each reply, `history_for()` presents the current speaker's earlier replies as `assistant` messages and the other speaker's replies as `user` messages. It keeps the original topic and recent messages within a 12,000-character request-history budget. The full conversation remains in the UI and exports. Requests go to `{base_url}/chat/completions` with the speaker's system prompt, model ID, temperature `0.7`, and `max_tokens` of `2000`. Streaming reads server-sent `data:` events, requests usage reporting, and requires a final `[DONE]` event. If a stream fails, is incomplete, or is empty, the app attempts a regular JSON completion. Transport and empty-response failures receive up to two retries. The same message bubble is reused during fallback; partial stream content is cleared before the fallback request and cannot enter later conversation history. `.env`, screenshots, and transcripts are ignored by Git. ## Tests After installing `requirements.txt`, run: ```powershell python -m unittest discover -v ``` The suite covers CLI behavior, streaming and fallback, speaker roles and bounded history, UI controls, statistics, and transcript export. It uses `httpx.MockTransport` and Textual's test pilot, so no live model server is needed.