feat: add AI markdown reformatting and usage metering to NewsService
Add AI-powered body reformatting for valid articles in the news pipeline, converting raw text walls into clean Markdown with paragraphs, headings, and lists. Introduce `news_usage` database table and `add_news_usage`/`get_news_usage` helpers to track per-cycle gateway costs (calls, tokens, latency, USD) from response headers, reported on the admin Services page. Remove unused `.sidebar-more` CSS and apply `render_title()` to poll question/label fields.
This commit is contained in:
@@ -185,6 +185,43 @@ def test_build_pagination_metadata():
|
||||
assert clamped["total_pages"] == 1
|
||||
|
||||
|
||||
def test_news_usage_accumulates_and_computes_averages(local_db):
|
||||
from devplacepy.database import add_news_usage, get_news_usage
|
||||
|
||||
get_table("news_usage").delete()
|
||||
empty = get_news_usage()
|
||||
assert empty["calls"] == 0
|
||||
assert empty["avg_tokens"] == 0.0
|
||||
|
||||
totals = {
|
||||
"calls": 2,
|
||||
"prompt_tokens": 2000,
|
||||
"completion_tokens": 400,
|
||||
"total_tokens": 2400,
|
||||
"cost_usd": 0.0005,
|
||||
"upstream_latency_ms": 1600.0,
|
||||
"total_latency_ms": 1800.0,
|
||||
}
|
||||
add_news_usage(totals)
|
||||
add_news_usage(totals)
|
||||
|
||||
usage = get_news_usage()
|
||||
assert usage["calls"] == 4
|
||||
assert usage["total_tokens"] == 4800
|
||||
assert abs(usage["cost_usd"] - 0.001) < 1e-9
|
||||
assert usage["avg_tokens"] == 1200.0
|
||||
assert usage["avg_cost_usd"] == 0.00025
|
||||
assert usage["avg_tokens_per_second"] == 250.0
|
||||
|
||||
|
||||
def test_add_news_usage_ignores_zero_call_batches(local_db):
|
||||
from devplacepy.database import add_news_usage, get_news_usage
|
||||
|
||||
get_table("news_usage").delete()
|
||||
add_news_usage({"calls": 0, "total_tokens": 999, "cost_usd": 1.0})
|
||||
assert get_news_usage()["calls"] == 0
|
||||
|
||||
|
||||
def test_interleave_by_author_spreads_consecutive_runs():
|
||||
from devplacepy.database import interleave_by_author
|
||||
|
||||
|
||||
Reference in New Issue
Block a user