Skip to content

Commit b0d5bdf

Browse files
v1.2.2 - Infrastructure Improvements & Metrics
- Add utils/api.py: centralized OpenRouter API configuration - Add config/models.py: centralized model constants (LLM_MODEL, IMAGE_MODEL) - Add LLMClient.chat() method for multi-turn agent conversations - Refactor AutoPostService to use LLMClient instead of direct API calls - Add database metrics methods (ping, count_posts, count_mentions, etc.) - Extend /health endpoint with database status and tier info - Add /metrics endpoint for bot statistics - Update tools to use centralized imports - Update documentation (README, ARCHITECTURE, CHANGELOG)
1 parent e7ca64f commit b0d5bdf

File tree

11 files changed

+491
-205
lines changed

11 files changed

+491
-205
lines changed

ARCHITECTURE.md

Lines changed: 58 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ main.py # Application entry point
3232
config/
3333
__init__.py
3434
settings.py # Environment configuration
35+
models.py # Model configuration (LLM_MODEL, IMAGE_MODEL)
3536
schemas.py # JSON schemas for LLM responses
3637
personality/ # Modular character definition
3738
__init__.py # Combines parts into SYSTEM_PROMPT
@@ -41,15 +42,17 @@ config/
4142
prompts/ # LLM prompts
4243
agent_autopost.py # Agent planning prompt
4344
mention_selector.py # Mention handling prompt
45+
utils/
46+
__init__.py
47+
api.py # OpenRouter API configuration
4448
services/
4549
__init__.py
46-
autopost.py # Autoposting service
50+
autopost.py # Autoposting service (uses LLMClient)
4751
mentions.py # Mention handling service
4852
tier_manager.py # Twitter API tier detection and limits
49-
llm.py # LLM client
53+
llm.py # LLM client (generate, generate_structured, chat)
5054
twitter.py # Twitter API client
51-
image_gen.py # Image generation service
52-
database.py # Database operations
55+
database.py # Database operations + metrics
5356
tools/
5457
__init__.py
5558
registry.py # Tool registry for function calling
@@ -67,19 +70,41 @@ requirements.txt # Python dependencies
6770
### main.py
6871
FastAPI application with lifespan management. On startup:
6972
1. Connects to PostgreSQL database
70-
2. Initializes AutoPostService and MentionHandler
71-
3. Schedules two recurring jobs via APScheduler:
72-
- `autopost_job` — runs every POST_INTERVAL_MINUTES
73-
- `mentions_job` — runs every MENTIONS_INTERVAL_MINUTES
74-
4. Starts the scheduler
73+
2. Initializes TierManager, AutoPostService, and MentionHandler
74+
3. Logs connected Twitter account info
75+
4. Schedules hourly tier refresh via APScheduler
76+
5. Starts the scheduler
7577

7678
HTTP endpoints:
77-
- `GET /health` — health check with scheduler status
79+
- `GET /health` — health check with database, scheduler, tier status
80+
- `GET /metrics` — bot statistics (posts/mentions counts, timestamps)
7881
- `GET /callback` — OAuth callback for Twitter authentication
79-
- `POST /trigger-post` — manually trigger autopost
80-
- `POST /trigger-mentions` — manually trigger mention processing
82+
- `POST /trigger-post` — manually trigger agent-based autopost
83+
- `GET /check-mentions` — fetch mentions without processing (dry run)
84+
- `POST /process-mentions` — fetch and process mentions
8185
- `GET /tier-status` — get current tier and usage stats
8286
- `POST /tier-refresh` — force tier re-detection
87+
- `POST /webhook/mentions` — Twitter webhook endpoint
88+
- `GET /webhook/mentions` — Twitter CRC challenge verification
89+
90+
### utils/api.py
91+
Centralized OpenRouter API configuration.
92+
93+
**Contents:**
94+
- `OPENROUTER_URL` — API endpoint constant
95+
- `get_openrouter_headers()` — Returns headers with authorization, content-type, referer
96+
97+
**Used by:** services/llm.py, tools/web_search.py, tools/image_generation.py
98+
99+
### config/models.py
100+
Centralized model configuration.
101+
102+
```python
103+
LLM_MODEL = "anthropic/claude-sonnet-4.5"
104+
IMAGE_MODEL = "google/gemini-3-pro-image-preview"
105+
```
106+
107+
**Used by:** services/llm.py, tools/web_search.py, tools/image_generation.py
83108

84109
### config/settings.py
85110
Pydantic Settings class that loads configuration from environment variables and `.env` file. All settings are typed and validated on startup.
@@ -199,9 +224,9 @@ Structured output schema:
199224
Methods:
200225
- `generate(system, user)` — simple text completion
201226
- `generate_structured(system, user, response_format)` — JSON structured output
202-
- `chat(messages, tools)` — chat completion with optional tool calling
227+
- `chat(messages, response_format)`multi-turn chat with optional structured output
203228

204-
Uses `anthropic/claude-sonnet-4.5` model by default (configurable via TEXT_MODEL constant).
229+
Uses model from `config/models.py` (`LLM_MODEL`). The `chat()` method is used by `AutoPostService` for agent conversations.
205230

206231
### services/twitter.py
207232
`TwitterClient` class — Twitter API integration using tweepy.
@@ -215,16 +240,27 @@ Methods:
215240

216241
Note: Media upload uses API v1.1 because v2 doesn't support it yet.
217242

218-
### services/image_gen.py
219-
`ImageGenerator` class — image generation via OpenRouter.
243+
### services/database.py
244+
`Database` class — async PostgreSQL client using asyncpg.
220245

221-
Uses `google/gemini-3-pro-image-preview` model. Supports reference images from `assets/` folder for consistent character appearance.
246+
Tables (auto-created on startup):
247+
- `posts` — stores all posted tweets
248+
- `mentions` — stores mention interactions
249+
- `bot_state` — key-value store for state (e.g., last_mention_id)
222250

223-
Flow:
224-
1. Loads reference images from `assets/` folder (up to 2 randomly selected)
225-
2. Sends reference images + text prompt to model
226-
3. Receives base64-encoded image in response
227-
4. Returns raw image bytes
251+
**Key methods:**
252+
- `get_recent_posts_formatted(limit)` — posts as formatted string for LLM context
253+
- `save_post(text, tweet_id, include_picture)` — save new post
254+
- `save_mention(...)` — save mention interaction
255+
- `mention_exists(tweet_id)` — check if mention already processed
256+
- `get_state(key)` / `set_state(key, value)` — state management
257+
- `get_user_mention_history(author_handle)` — conversation history with user
258+
259+
**Metrics methods (v1.2.2):**
260+
- `ping()` — health check (returns bool)
261+
- `count_posts()` / `count_posts_today()` — post counts
262+
- `count_mentions()` / `count_mentions_today()` — mention counts
263+
- `get_last_post_time()` / `get_last_mention_time()` — timestamps
228264

229265
### services/tier_manager.py
230266
`TierManager` class — automatic Twitter API tier detection and limit management.
@@ -265,21 +301,6 @@ TIER_FEATURES = {
265301
- `MentionHandler.process_mentions_batch()` calls `tier_manager.can_use_mentions()` before processing
266302
- Both services receive `tier_manager` instance via constructor
267303

268-
### services/database.py
269-
`Database` class — async PostgreSQL client using asyncpg.
270-
271-
Tables (auto-created on startup):
272-
- `posts` — stores all posted tweets
273-
- `mentions` — stores mention interactions
274-
- `bot_state` — key-value store for state (e.g., last_mention_id)
275-
276-
Key methods:
277-
- `get_recent_posts_formatted(limit)` — posts as formatted string for LLM context
278-
- `save_post(text, tweet_id, include_picture)` — save new post
279-
- `save_mention(...)` — save mention interaction
280-
- `mention_exists(tweet_id)` — check if mention already processed
281-
- `get_state(key)` / `set_state(key, value)` — state management
282-
283304
### tools/registry.py
284305
Tool registry for agent function calling. Contains:
285306
- `TOOLS` — dict mapping tool names to async functions

README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ my-agent/
216216
217217
├── config/
218218
│ ├── settings.py # Environment & configuration
219+
│ ├── models.py # Model configuration (LLM, Image models)
219220
│ ├── schemas.py # JSON schemas for LLM responses
220221
│ ├── personality/ # Character definition (modular)
221222
│ │ ├── backstory.py # Origin story
@@ -225,13 +226,16 @@ my-agent/
225226
│ ├── agent_autopost.py # Agent planning prompt
226227
│ └── mention_selector.py # Mention handling prompt
227228
229+
├── utils/
230+
│ └── api.py # OpenRouter API configuration
231+
228232
├── services/
229-
│ ├── autopost.py # Scheduled posting logic
230-
│ ├── mentions.py # Mention/reply handler with tool calling
231-
│ ├── llm.py # OpenRouter client
233+
│ ├── autopost.py # Agent-based scheduled posting
234+
│ ├── mentions.py # Mention/reply handler
235+
│ ├── tier_manager.py # Twitter API tier detection
236+
│ ├── llm.py # OpenRouter client (generate, chat)
232237
│ ├── twitter.py # Twitter API v2 integration
233-
│ ├── image_gen.py # Image generation
234-
│ └── database.py # PostgreSQL for history
238+
│ └── database.py # PostgreSQL for history + metrics
235239
236240
├── tools/
237241
│ ├── registry.py # Available tools for LLM

config/models.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
Model configuration for DOT Twitter Bot.
3+
4+
Centralized model definitions used across all services and tools.
5+
Change models here to update them everywhere.
6+
"""
7+
8+
# LLM Models (for text generation)
9+
LLM_MODEL = "anthropic/claude-sonnet-4.5"
10+
11+
# Image Models (for image generation)
12+
IMAGE_MODEL = "google/gemini-3-pro-image-preview"
13+
14+
# Uncomment to override defaults:
15+
# LLM_MAX_TOKENS = 1024
16+
# LLM_TEMPERATURE = 0.8

0 commit comments

Comments
 (0)