2.0 KiB
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 |
settings.py |
Load and save sloppy.json connections and selections |
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 prompt and turn count, and creates AIChatApp. The app optionally opens the connection picker, then starts the conversation. 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 and requests usage reporting. If a stream fails 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, replacing partial stream content with the final reply.
settings.py writes JSON through a temporary file and atomic replace. Use an env:VARIABLE API-key reference for hosted-provider secrets. .env, sloppy.json, screenshots, and transcripts are ignored by Git.
Tests
After installing requirements.txt, run:
python -m unittest discover -v
The suite covers CLI and settings 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.