Implement BIT-709 acceptance: GET /search and /stats on the writer-pipeline aggregator; bridge AggregatorClient calls /search and maps items with source_url. /writers/:id/candidates merges aggregator-first with WordPress, exposes candidate_source and matched_sources, and passes max_age_hours into ranking. Add docs/AGGREGATOR_OPERATIONS.md and store unit tests. Paperclip-Issue: BIT-709 Made-with: Cursor
446 lines
11 KiB
Markdown
446 lines
11 KiB
Markdown
# Writer Tools — Content Aggregation & Ranking
|
||
|
||
## Overview
|
||
|
||
The Writer Tools system enables AI agents (like Nelson Alves, the AI News Writer) to:
|
||
|
||
1. **Manage topics and sources** — Define what news topics and sources the agent cares about
|
||
2. **Query aggregated content** — Search for articles matching the agent's interests
|
||
3. **Get ranked candidates** — Retrieve top-N writing suggestions sorted by freshness and relevance
|
||
|
||
This reduces token costs by avoiding expensive per-article LLM reads and helps agents focus on the most relevant, timely content.
|
||
|
||
---
|
||
|
||
## Architecture
|
||
|
||
### Configuration
|
||
|
||
Each writer agent has a configuration file in `config/writers/{agent-id}.json`:
|
||
|
||
```json
|
||
{
|
||
"agent_id": "f0251a61-38e1-4a9b-8cd1-38d96a320cab",
|
||
"agent_name": "Nelson Alves",
|
||
"role": "AI News Writer",
|
||
"domain": "andebol.net",
|
||
"topics": ["Andebol", "Futebol", "Desportos"],
|
||
"sources": [
|
||
{
|
||
"id": "fpa-portal",
|
||
"name": "FPA Portal",
|
||
"url": "https://portal.fpa.pt/noticias",
|
||
"type": "html",
|
||
"language": "pt-PT"
|
||
},
|
||
...
|
||
],
|
||
"ranking_preferences": {
|
||
"freshness_weight": 0.6,
|
||
"relevance_weight": 0.4,
|
||
"min_score": 0.3
|
||
}
|
||
}
|
||
```
|
||
|
||
### Ranking Algorithm
|
||
|
||
Articles are ranked by a weighted combination of:
|
||
|
||
**Freshness Score** (0-1)
|
||
- Based on how recent the article is
|
||
- Uses exponential decay: `1 - (age_hours / max_age_hours)^0.5`
|
||
- Very recent articles (< 1 hour) score ~0.95+
|
||
- Articles older than 1 week score ~0
|
||
|
||
**Relevance Score** (0-1)
|
||
- Based on matching topics in article title/excerpt
|
||
- Normalized by number of configured topics
|
||
- Falls back to 0.2 if no topics match (unexpected articles are still valuable)
|
||
|
||
**Combined Score** = (freshness × freshness_weight) + (relevance × relevance_weight)
|
||
|
||
Articles below `min_score` are filtered out. Default: 0.3 (articles must score at least 30%).
|
||
|
||
---
|
||
|
||
## REST API Reference
|
||
|
||
Base URL: `http://bridge:3200` (configured per domain)
|
||
|
||
### Get Writer Configuration
|
||
|
||
```bash
|
||
GET /writers/{agentId}
|
||
```
|
||
|
||
Returns the full writer config (topics, sources, ranking preferences).
|
||
|
||
**Example:**
|
||
```bash
|
||
curl http://localhost:3200/writers/f0251a61-38e1-4a9b-8cd1-38d96a320cab
|
||
```
|
||
|
||
**Response:**
|
||
```json
|
||
{
|
||
"agent_id": "f0251a61-38e1-4a9b-8cd1-38d96a320cab",
|
||
"agent_name": "Nelson Alves",
|
||
"topics": ["Andebol", "Futebol"],
|
||
"sources": [...],
|
||
"ranking_preferences": { ... }
|
||
}
|
||
```
|
||
|
||
---
|
||
|
||
### Manage Topics
|
||
|
||
#### List Topics
|
||
|
||
```bash
|
||
GET /writers/{agentId}/topics
|
||
```
|
||
|
||
**Example:**
|
||
```bash
|
||
curl http://localhost:3200/writers/f0251a61-38e1-4a9b-8cd1-38d96a320cab/topics
|
||
```
|
||
|
||
**Response:**
|
||
```json
|
||
{
|
||
"agent_id": "f0251a61-38e1-4a9b-8cd1-38d96a320cab",
|
||
"topics": ["Andebol", "Futebol", "Desportos"]
|
||
}
|
||
```
|
||
|
||
#### Add Topic
|
||
|
||
```bash
|
||
POST /writers/{agentId}/topics
|
||
Content-Type: application/json
|
||
|
||
{ "topic": "Ténis" }
|
||
```
|
||
|
||
**Example:**
|
||
```bash
|
||
curl -X POST http://localhost:3200/writers/f0251a61-38e1-4a9b-8cd1-38d96a320cab/topics \
|
||
-H "Content-Type: application/json" \
|
||
-d '{"topic":"Ténis"}'
|
||
```
|
||
|
||
**Response:** 201 Created
|
||
```json
|
||
{
|
||
"agent_id": "f0251a61-38e1-4a9b-8cd1-38d96a320cab",
|
||
"topics": ["Andebol", "Futebol", "Desportos", "Ténis"]
|
||
}
|
||
```
|
||
|
||
#### Remove Topic
|
||
|
||
```bash
|
||
DELETE /writers/{agentId}/topics/{topic}
|
||
```
|
||
|
||
**Example:**
|
||
```bash
|
||
curl -X DELETE http://localhost:3200/writers/f0251a61-38e1-4a9b-8cd1-38d96a320cab/topics/Ténis
|
||
```
|
||
|
||
---
|
||
|
||
### Manage Sources
|
||
|
||
#### List Sources
|
||
|
||
```bash
|
||
GET /writers/{agentId}/sources
|
||
```
|
||
|
||
**Response:**
|
||
```json
|
||
{
|
||
"agent_id": "f0251a61-38e1-4a9b-8cd1-38d96a320cab",
|
||
"sources": [
|
||
{
|
||
"id": "fpa-portal",
|
||
"name": "FPA Portal",
|
||
"url": "https://portal.fpa.pt/noticias",
|
||
"type": "html",
|
||
"language": "pt-PT"
|
||
},
|
||
...
|
||
]
|
||
}
|
||
```
|
||
|
||
#### Add Source
|
||
|
||
```bash
|
||
POST /writers/{agentId}/sources
|
||
Content-Type: application/json
|
||
|
||
{
|
||
"id": "new-source",
|
||
"name": "Source Name",
|
||
"url": "https://example.com/news",
|
||
"type": "html",
|
||
"language": "pt-PT"
|
||
}
|
||
```
|
||
|
||
**Example:**
|
||
```bash
|
||
curl -X POST http://localhost:3200/writers/f0251a61-38e1-4a9b-8cd1-38d96a320cab/sources \
|
||
-H "Content-Type: application/json" \
|
||
-d '{
|
||
"id":"espn-sports",
|
||
"name":"ESPN Sports",
|
||
"url":"https://www.espn.com/",
|
||
"type":"html",
|
||
"language":"en-US"
|
||
}'
|
||
```
|
||
|
||
#### Remove Source
|
||
|
||
```bash
|
||
DELETE /writers/{agentId}/sources/{sourceId}
|
||
```
|
||
|
||
**Example:**
|
||
```bash
|
||
curl -X DELETE http://localhost:3200/writers/f0251a61-38e1-4a9b-8cd1-38d96a320cab/sources/espn-sports
|
||
```
|
||
|
||
---
|
||
|
||
### Get Writing Candidates
|
||
|
||
Returns top-N articles ranked by freshness + relevance to the agent's interests.
|
||
|
||
```bash
|
||
GET /writers/{agentId}/candidates?limit=10&max_age_hours=168
|
||
```
|
||
|
||
**Query Parameters:**
|
||
- `limit` (optional, default: 10) — number of candidates to return
|
||
- `max_age_hours` (optional, default: 168) — only include articles from the last N hours
|
||
|
||
**Example:**
|
||
```bash
|
||
curl "http://localhost:3200/writers/f0251a61-38e1-4a9b-8cd1-38d96a320cab/candidates?limit=5&max_age_hours=24"
|
||
```
|
||
|
||
**Response:**
|
||
```json
|
||
{
|
||
"agent_id": "f0251a61-38e1-4a9b-8cd1-38d96a320cab",
|
||
"candidates": [
|
||
{
|
||
"article": {
|
||
"id": 123,
|
||
"slug": "porto-bate-benfica",
|
||
"title": "Porto bate Benfica no clássico",
|
||
"excerpt": "O Porto conquistou uma vitória importante...",
|
||
"featured_image": "...",
|
||
"published_at": "2026-04-02T10:30:00Z",
|
||
"author": { "name": "João Silva" },
|
||
"category": { "name": "Futebol" },
|
||
"reading_time": 5
|
||
},
|
||
"score": 0.92,
|
||
"freshness_score": 0.98,
|
||
"relevance_score": 0.85,
|
||
"matched_topics": ["Futebol"],
|
||
"freshness_hours": 0.5
|
||
},
|
||
...
|
||
],
|
||
"total_candidates": 42,
|
||
"generated_at": "2026-04-02T11:00:00Z"
|
||
}
|
||
```
|
||
|
||
The candidates are sorted by `score` (highest first). Use this list to decide which articles to write about.
|
||
|
||
---
|
||
|
||
### Get Statistics
|
||
|
||
```bash
|
||
GET /writers/{agentId}/stats
|
||
```
|
||
|
||
Returns aggregation statistics for the agent.
|
||
|
||
**Example:**
|
||
```bash
|
||
curl http://localhost:3200/writers/f0251a61-38e1-4a9b-8cd1-38d96a320cab/stats
|
||
```
|
||
|
||
**Response:**
|
||
```json
|
||
{
|
||
"agent_id": "f0251a61-38e1-4a9b-8cd1-38d96a320cab",
|
||
"topics_count": 3,
|
||
"sources_count": 6,
|
||
"aggregator_available": true,
|
||
"last_aggregated_at": null,
|
||
"aggregator_total_items": 1200,
|
||
"aggregator_site_items": 400
|
||
}
|
||
```
|
||
|
||
---
|
||
|
||
### Update Ranking Preferences
|
||
|
||
Adjust freshness vs. relevance weights and minimum score threshold.
|
||
|
||
```bash
|
||
PUT /writers/{agentId}/ranking-preferences
|
||
Content-Type: application/json
|
||
|
||
{
|
||
"freshness_weight": 0.7,
|
||
"relevance_weight": 0.3,
|
||
"min_score": 0.4
|
||
}
|
||
```
|
||
|
||
**Example:**
|
||
```bash
|
||
curl -X PUT http://localhost:3200/writers/f0251a61-38e1-4a9b-8cd1-38d96a320cab/ranking-preferences \
|
||
-H "Content-Type: application/json" \
|
||
-d '{"freshness_weight":0.7,"relevance_weight":0.3,"min_score":0.4}'
|
||
```
|
||
|
||
---
|
||
|
||
## Usage Examples
|
||
|
||
### Scenario 1: Nelson Alves writes about handball
|
||
|
||
Nelson Alves is configured with handball topics and sources. Every time he runs, he:
|
||
|
||
1. **Get candidates:**
|
||
```bash
|
||
curl "http://bridge:3200/writers/f0251a61-38e1-4a9b-8cd1-38d96a320cab/candidates?limit=5"
|
||
```
|
||
|
||
2. **Pick top candidate** (score 0.92) and write article about it
|
||
|
||
3. **Continue with next candidates** until task time limit reached
|
||
|
||
### Scenario 2: Expand to new sport
|
||
|
||
Nelson adds Tennis to his topics:
|
||
|
||
```bash
|
||
curl -X POST http://bridge:3200/writers/f0251a61-38e1-4a9b-8cd1-38d96a320cab/topics \
|
||
-H "Content-Type: application/json" \
|
||
-d '{"topic":"Ténis"}'
|
||
```
|
||
|
||
Next `/candidates` call automatically includes tennis articles.
|
||
|
||
### Scenario 3: Adjust ranking priorities
|
||
|
||
If stale articles are being suggested, increase freshness weight:
|
||
|
||
```bash
|
||
curl -X PUT http://bridge:3200/writers/f0251a61-38e1-4a9b-8cd1-38d96a320cab/ranking-preferences \
|
||
-H "Content-Type: application/json" \
|
||
-d '{"freshness_weight":0.8,"relevance_weight":0.2}'
|
||
```
|
||
|
||
---
|
||
|
||
## Caching
|
||
|
||
All `/candidates` and `/stats` responses are cached for performance:
|
||
|
||
- **Fresh cache:** 60 seconds (served immediately)
|
||
- **Stale-While-Revalidate:** 300 seconds (served stale, revalidated in background)
|
||
|
||
This prevents hammering WordPress when multiple agent instances query frequently.
|
||
|
||
---
|
||
|
||
## Integration with Content Aggregator (BIT-709)
|
||
|
||
With **`AGGREGATOR_URL`** set on the bridge (base URL of `writer-pipeline`’s HTTP server):
|
||
|
||
1. **Aggregator API** (see `docs/AGGREGATOR_OPERATIONS.md`):
|
||
- `GET /search?site={writerConfig.domain}&limit=N&max_age_hours=…&q=…`
|
||
- Items include `title`, `summary`, `url`, `sourceFeedId`, `publishedAt` (dedup by canonical URL at ingest).
|
||
|
||
2. **Bridge behaviour:**
|
||
- `/writers/:agentId/candidates` loads aggregated items **first**, merges WordPress posts, then ranks. Response includes **`candidate_source`**: `aggregator`, `wordpress`, or `mixed`.
|
||
- Aggregated rows expose the original link as **`article.source_url`** (WordPress-sourced rows omit it).
|
||
|
||
3. **No breaking changes** to query parameters; agents should prefer `source_url` when present for the read/fetch step.
|
||
|
||
---
|
||
|
||
## Agent Instructions
|
||
|
||
For agents using the writer tools, here's the recommended workflow:
|
||
|
||
```bash
|
||
# 1. Check what you're configured to write about
|
||
curl http://bridge:3200/writers/$AGENT_ID/topics
|
||
curl http://bridge:3200/writers/$AGENT_ID/sources
|
||
|
||
# 2. Get top writing candidates for this session
|
||
CANDIDATES=$(curl "http://bridge:3200/writers/$AGENT_ID/candidates?limit=5")
|
||
|
||
# 3. Process each candidate (highest score first)
|
||
# — Open candidate.article.source_url if set, else use the WordPress article as today
|
||
# — Write summary in target language
|
||
# — Publish via WordPress API
|
||
|
||
# 4. If needed, adjust preferences for next run
|
||
curl -X PUT http://bridge:3200/writers/$AGENT_ID/ranking-preferences \
|
||
-H "Content-Type: application/json" \
|
||
-d '{"freshness_weight":0.7}'
|
||
```
|
||
|
||
---
|
||
|
||
## Files
|
||
|
||
- **Configurations:** `config/writers/{agent-id}.json`
|
||
- **Bridge services:**
|
||
- `bridge/src/services/writer-config.ts` — config I/O
|
||
- `bridge/src/services/ranking.ts` — scoring algorithm
|
||
- `bridge/src/services/aggregator-client.ts` — aggregator integration
|
||
- `bridge/src/routes/writers.ts` — REST API endpoints
|
||
- **Documentation:** `docs/writer-tools.md` (this file)
|
||
|
||
---
|
||
|
||
## Troubleshooting
|
||
|
||
**Q: Why is my agent getting low-scoring candidates?**
|
||
- Check your `min_score` threshold in ranking preferences
|
||
- Verify your topics match article content (case-insensitive substring match)
|
||
- Try lowering `freshness_weight` if you want older articles
|
||
|
||
**Q: How do I add a new source?**
|
||
- POST to `/writers/{agentId}/sources` with `{id, name, url, type, language}`
|
||
- Type should be `html`, `rss`, or `api`
|
||
- Note: Source filtering is prepared for BIT-709 aggregator; currently only topic-based ranking works
|
||
|
||
**Q: Can I use the same config for multiple agents?**
|
||
- No, each agent has their own config file
|
||
- Copy and modify `config/writers/{agent-id}.json` for a new agent
|
||
|
||
**Q: What happens if the aggregator is down?**
|
||
- Writer tools fall back to WordPress data + local ranking
|
||
- Performance degrades slightly but functionality remains
|