Add bill action pipeline, admin health panel, and LLM provider fixes

- Fetch bill actions from Congress.gov and populate the action timeline
- Add nightly batch task and beat schedule for active bill actions
- Add admin reprocess endpoint for per-bill debugging
- Add BriefPanel with "What Changed" view and version history
- Add External API Health section with per-source latency testing
- Redesign Manual Controls as health panel with status dots and descriptions
- Add Resume Analysis task for stalled LLM jobs
- Add Backfill Dates & Links task for bills with null metadata
- Fix LLM provider/model DB overrides being ignored (env vars used instead)
- Fix Gemini 404: gemini-1.5-pro deprecated → gemini-2.0-flash
- Fix Anthropic models list: use REST API directly (SDK too old for .models)
- Replace test-LLM full analysis with lightweight ping (max_tokens=20)
- Add has_document field to BillDetail; show "No bill text published" state
- Fix "Introduced: —" showing for bills with null introduced_date
- Add bills_missing_sponsor and bills_missing_metadata to admin stats
- Add GovInfo health check using /collections endpoint (fixes 500 from /packages)

Authored-By: Jack Levy
This commit is contained in:
Jack Levy
2026-03-01 11:06:14 -05:00
parent defc2c116d
commit 5eebc2f196
10 changed files with 586 additions and 74 deletions

View File

@@ -2,7 +2,7 @@
import { use, useEffect, useRef } from "react";
import Link from "next/link";
import { ArrowLeft, ExternalLink, User } from "lucide-react";
import { ArrowLeft, ExternalLink, FileX, User } from "lucide-react";
import { useBill, useBillNews, useBillTrend } from "@/lib/hooks/useBills";
import { BriefPanel } from "@/components/bills/BriefPanel";
import { ActionTimeline } from "@/components/bills/ActionTimeline";
@@ -82,10 +82,12 @@ export default function BillDetailPage({ params }: { params: Promise<{ id: strin
{bill.sponsor.state && <span>{bill.sponsor.state}</span>}
</div>
)}
<p className="text-xs text-muted-foreground mt-1">
Introduced: {formatDate(bill.introduced_date)}
<p className="text-xs text-muted-foreground mt-1 flex items-center gap-3 flex-wrap">
{bill.introduced_date && (
<span>Introduced: {formatDate(bill.introduced_date)}</span>
)}
{bill.congress_url && (
<a href={bill.congress_url} target="_blank" rel="noopener noreferrer" className="ml-3 hover:text-primary transition-colors">
<a href={bill.congress_url} target="_blank" rel="noopener noreferrer" className="hover:text-primary transition-colors">
congress.gov <ExternalLink className="w-3 h-3 inline" />
</a>
)}
@@ -97,7 +99,39 @@ export default function BillDetailPage({ params }: { params: Promise<{ id: strin
{/* Content grid */}
<div className="grid grid-cols-1 md:grid-cols-3 gap-4 md:gap-6">
<div className="md:col-span-2 space-y-6">
<BriefPanel briefs={bill.briefs} />
{bill.briefs.length > 0 ? (
<BriefPanel briefs={bill.briefs} />
) : bill.has_document ? (
<div className="bg-card border border-border rounded-lg p-6 text-center space-y-2">
<p className="text-sm font-medium text-muted-foreground">Analysis pending</p>
<p className="text-xs text-muted-foreground">
Bill text was retrieved but has not yet been analyzed. Check back shortly.
</p>
</div>
) : (
<div className="bg-card border border-border rounded-lg p-6 space-y-3">
<div className="flex items-center gap-2 text-muted-foreground">
<FileX className="w-4 h-4 shrink-0" />
<span className="text-sm font-medium">No bill text published</span>
</div>
<p className="text-sm text-muted-foreground">
As of {new Date().toLocaleDateString("en-US", { month: "long", day: "numeric", year: "numeric" })},{" "}
no official text has been received for{" "}
<span className="font-medium">{billLabel(bill.bill_type, bill.bill_number)}</span>.
Analysis will be generated automatically once text is published on Congress.gov.
</p>
{bill.congress_url && (
<a
href={bill.congress_url}
target="_blank"
rel="noopener noreferrer"
className="inline-flex items-center gap-1 text-xs text-primary hover:underline"
>
Check status on Congress.gov <ExternalLink className="w-3 h-3" />
</a>
)}
</div>
)}
<ActionTimeline actions={bill.actions} />
</div>
<div className="space-y-4">

View File

@@ -20,7 +20,7 @@ import {
Copy,
Rss,
} from "lucide-react";
import { settingsAPI, adminAPI, notificationsAPI, type AdminUser, type LLMModel } from "@/lib/api";
import { settingsAPI, adminAPI, notificationsAPI, type AdminUser, type LLMModel, type ApiHealthResult } from "@/lib/api";
import { useAuthStore } from "@/stores/authStore";
const LLM_PROVIDERS = [
@@ -47,6 +47,18 @@ export default function SettingsPage() {
refetchInterval: 30_000,
});
const [healthTesting, setHealthTesting] = useState(false);
const [healthData, setHealthData] = useState<Record<string, ApiHealthResult> | null>(null);
const testApiHealth = async () => {
setHealthTesting(true);
try {
const result = await adminAPI.getApiHealth();
setHealthData(result as unknown as Record<string, ApiHealthResult>);
} finally {
setHealthTesting(false);
}
};
const { data: users, isLoading: usersLoading } = useQuery({
queryKey: ["admin-users"],
queryFn: () => adminAPI.listUsers(),
@@ -167,34 +179,16 @@ export default function SettingsPage() {
{/* Analysis Status */}
<section className="bg-card border border-border rounded-lg p-6 space-y-4">
<h2 className="font-semibold flex items-center gap-2">
<BarChart3 className="w-4 h-4" /> Analysis Status
<BarChart3 className="w-4 h-4" /> Bill Pipeline
<span className="text-xs text-muted-foreground font-normal ml-auto">refreshes every 30s</span>
</h2>
{stats ? (
<>
<div className="grid grid-cols-1 sm:grid-cols-3 gap-4">
<div className="bg-muted/50 rounded-lg p-3 text-center">
<FileText className="w-4 h-4 mx-auto mb-1 text-muted-foreground" />
<div className="text-xl font-bold">{stats.total_bills.toLocaleString()}</div>
<div className="text-xs text-muted-foreground">Total Bills</div>
</div>
<div className="bg-muted/50 rounded-lg p-3 text-center">
<FileText className="w-4 h-4 mx-auto mb-1 text-blue-500" />
<div className="text-xl font-bold">{stats.docs_fetched.toLocaleString()}</div>
<div className="text-xs text-muted-foreground">Docs Fetched</div>
</div>
<div className="bg-muted/50 rounded-lg p-3 text-center">
<Brain className="w-4 h-4 mx-auto mb-1 text-green-500" />
<div className="text-xl font-bold">{stats.briefs_generated.toLocaleString()}</div>
<div className="text-xs text-muted-foreground">Briefs Generated</div>
</div>
</div>
{/* Progress bar */}
<div className="space-y-1">
<div className="flex justify-between text-xs text-muted-foreground">
<span>{stats.full_briefs} full · {stats.amendment_briefs} amendments</span>
<span>{pct}% analyzed · {stats.remaining.toLocaleString()} remaining</span>
<span>{stats.briefs_generated.toLocaleString()} analyzed ({stats.full_briefs} full · {stats.amendment_briefs} amendments)</span>
<span>{pct}% of {stats.total_bills.toLocaleString()} bills</span>
</div>
<div className="h-2 bg-muted rounded-full overflow-hidden">
<div
@@ -202,11 +196,34 @@ export default function SettingsPage() {
style={{ width: `${pct}%` }}
/>
</div>
{stats.uncited_briefs > 0 && (
<p className="text-xs text-amber-600 dark:text-amber-400">
{stats.uncited_briefs.toLocaleString()} brief{stats.uncited_briefs !== 1 ? "s" : ""} missing citations run Backfill Citations to fix
</p>
)}
</div>
{/* Pipeline breakdown table */}
<div className="divide-y divide-border text-sm">
{[
{ label: "Total bills tracked", value: stats.total_bills, color: "text-foreground", icon: "📋" },
{ label: "Text published on Congress.gov", value: stats.docs_fetched, color: "text-blue-600 dark:text-blue-400", icon: "📄" },
{ label: "No text published yet", value: stats.no_text_bills, color: "text-muted-foreground", icon: "⏳", note: "Normal — bill text appears after committee markup" },
{ label: "AI briefs generated", value: stats.briefs_generated, color: "text-green-600 dark:text-green-400", icon: "✅" },
{ label: "Pending LLM analysis", value: stats.pending_llm, color: stats.pending_llm > 0 ? "text-amber-600 dark:text-amber-400" : "text-muted-foreground", icon: "🔄", action: stats.pending_llm > 0 ? "Resume Analysis" : undefined },
{ label: "Briefs missing citations", value: stats.uncited_briefs, color: stats.uncited_briefs > 0 ? "text-amber-600 dark:text-amber-400" : "text-muted-foreground", icon: "⚠️", action: stats.uncited_briefs > 0 ? "Backfill Citations" : undefined },
].map(({ label, value, color, icon, note, action }) => (
<div key={label} className="flex items-center justify-between py-2.5 gap-3">
<div className="flex items-center gap-2 min-w-0">
<span className="text-base leading-none shrink-0">{icon}</span>
<div>
<span className="text-sm">{label}</span>
{note && <p className="text-xs text-muted-foreground mt-0.5">{note}</p>}
</div>
</div>
<div className="flex items-center gap-3 shrink-0">
<span className={`font-semibold tabular-nums ${color}`}>{value.toLocaleString()}</span>
{action && (
<span className="text-xs text-muted-foreground"> run {action}</span>
)}
</div>
</div>
))}
</div>
</>
) : (
@@ -560,50 +577,171 @@ export default function SettingsPage() {
</div>
</section>
{/* API Health */}
<section className="bg-card border border-border rounded-lg p-6 space-y-4">
<div className="flex items-center justify-between">
<h2 className="font-semibold">External API Health</h2>
<button
onClick={testApiHealth}
disabled={healthTesting}
className="flex items-center gap-2 px-3 py-1.5 text-sm bg-muted hover:bg-accent rounded-md transition-colors disabled:opacity-50"
>
<RefreshCw className={`w-3.5 h-3.5 ${healthTesting ? "animate-spin" : ""}`} />
{healthTesting ? "Testing…" : "Run Tests"}
</button>
</div>
{healthData ? (
<div className="divide-y divide-border">
{[
{ key: "congress_gov", label: "Congress.gov API" },
{ key: "govinfo", label: "GovInfo API" },
{ key: "newsapi", label: "NewsAPI.org" },
{ key: "google_news", label: "Google News RSS" },
].map(({ key, label }) => {
const r = healthData[key];
if (!r) return null;
return (
<div key={key} className="flex items-start justify-between py-3 gap-4">
<div>
<div className="text-sm font-medium">{label}</div>
<div className={`text-xs mt-0.5 ${
r.status === "ok" ? "text-green-600 dark:text-green-400"
: r.status === "skipped" ? "text-muted-foreground"
: "text-red-600 dark:text-red-400"
}`}>
{r.detail}
</div>
</div>
<div className="flex items-center gap-2 shrink-0">
{r.latency_ms !== undefined && (
<span className="text-xs text-muted-foreground">{r.latency_ms}ms</span>
)}
{r.status === "ok" && <CheckCircle className="w-4 h-4 text-green-500" />}
{r.status === "error" && <XCircle className="w-4 h-4 text-red-500" />}
{r.status === "skipped" && <span className="text-xs text-muted-foreground"></span>}
</div>
</div>
);
})}
</div>
) : (
<p className="text-sm text-muted-foreground">
Click Run Tests to check connectivity to each external data source.
</p>
)}
</section>
{/* Manual Controls */}
<section className="bg-card border border-border rounded-lg p-6 space-y-4">
<h2 className="font-semibold">Manual Controls</h2>
<div className="flex flex-wrap gap-3">
<button
onClick={() => trigger("poll", adminAPI.triggerPoll)}
className="flex items-center gap-2 px-4 py-2 text-sm bg-muted hover:bg-accent rounded-md transition-colors"
>
<RefreshCw className="w-3.5 h-3.5" /> Trigger Poll
</button>
<button
onClick={() => trigger("members", adminAPI.triggerMemberSync)}
className="flex items-center gap-2 px-4 py-2 text-sm bg-muted hover:bg-accent rounded-md transition-colors"
>
<RefreshCw className="w-3.5 h-3.5" /> Sync Members
</button>
<button
onClick={() => trigger("trends", adminAPI.triggerTrendScores)}
className="flex items-center gap-2 px-4 py-2 text-sm bg-muted hover:bg-accent rounded-md transition-colors"
>
<RefreshCw className="w-3.5 h-3.5" /> Calculate Trends
</button>
<button
onClick={() => trigger("sponsors", adminAPI.backfillSponsors)}
className="flex items-center gap-2 px-4 py-2 text-sm bg-muted hover:bg-accent rounded-md transition-colors"
>
<RefreshCw className="w-3.5 h-3.5" /> Backfill Sponsors
</button>
<button
onClick={() => trigger("citations", adminAPI.backfillCitations)}
className="flex items-center gap-2 px-4 py-2 text-sm bg-amber-100 text-amber-800 hover:bg-amber-200 dark:bg-amber-900/30 dark:text-amber-300 dark:hover:bg-amber-900/50 rounded-md transition-colors"
>
<RefreshCw className="w-3.5 h-3.5" /> Backfill Citations
</button>
<button
onClick={() => trigger("actions", adminAPI.triggerFetchActions)}
className="flex items-center gap-2 px-4 py-2 text-sm bg-muted hover:bg-accent rounded-md transition-colors"
>
<RefreshCw className="w-3.5 h-3.5" /> Fetch Bill Actions
</button>
<div className="divide-y divide-border">
{([
{
key: "poll",
name: "Trigger Poll",
description: "Check Congress.gov for newly introduced or updated bills. Runs automatically on a schedule — use this to force an immediate sync.",
fn: adminAPI.triggerPoll,
status: "on-demand",
},
{
key: "members",
name: "Sync Members",
description: "Refresh all member profiles from Congress.gov including biography, current term, leadership roles, and contact information.",
fn: adminAPI.triggerMemberSync,
status: "on-demand",
},
{
key: "trends",
name: "Calculate Trends",
description: "Score bill and member newsworthiness by counting recent news headlines and Google search interest. Updates the trend charts.",
fn: adminAPI.triggerTrendScores,
status: "on-demand",
},
{
key: "actions",
name: "Fetch Bill Actions",
description: "Download the full legislative history (votes, referrals, amendments) for recently active bills and populate the timeline view.",
fn: adminAPI.triggerFetchActions,
status: "on-demand",
},
{
key: "sponsors",
name: "Backfill Sponsors",
description: "Link bill sponsors that weren't captured during the initial import. Safe to re-run — skips bills that already have a sponsor.",
fn: adminAPI.backfillSponsors,
status: stats ? (stats.bills_missing_sponsor > 0 ? "needed" : "ok") : "on-demand",
count: stats?.bills_missing_sponsor,
countLabel: "bills missing sponsor",
},
{
key: "metadata",
name: "Backfill Dates & Links",
description: "Fill in missing introduced dates, chamber assignments, and congress.gov links by re-fetching bill detail from Congress.gov.",
fn: adminAPI.backfillMetadata,
status: stats ? (stats.bills_missing_metadata > 0 ? "needed" : "ok") : "on-demand",
count: stats?.bills_missing_metadata,
countLabel: "bills missing metadata",
},
{
key: "citations",
name: "Backfill Citations",
description: "Regenerate AI briefs that were created before inline source citations were added. Deletes the old brief and re-runs LLM analysis using the already-stored bill text — no new Congress.gov calls.",
fn: adminAPI.backfillCitations,
status: stats ? (stats.uncited_briefs > 0 ? "needed" : "ok") : "on-demand",
count: stats?.uncited_briefs,
countLabel: "briefs need regeneration",
},
{
key: "resume",
name: "Resume Analysis",
description: "Restart AI brief generation for bills where processing stalled or failed (e.g. after an LLM quota outage). Also re-queues document fetching for bills that have no text yet.",
fn: adminAPI.resumeAnalysis,
status: stats ? (stats.pending_llm > 0 ? "needed" : "ok") : "on-demand",
count: stats?.pending_llm,
countLabel: "bills pending analysis",
},
] as Array<{
key: string;
name: string;
description: string;
fn: () => Promise<{ task_id: string }>;
status: "ok" | "needed" | "on-demand";
count?: number;
countLabel?: string;
}>).map(({ key, name, description, fn, status, count, countLabel }) => (
<div key={key} className="flex items-start gap-3 py-3.5">
<div className={`w-2.5 h-2.5 rounded-full mt-1 shrink-0 ${
status === "ok" ? "bg-green-500"
: status === "needed" ? "bg-red-500"
: "bg-border"
}`} />
<div className="flex-1 min-w-0 space-y-0.5">
<div className="flex items-center gap-2 flex-wrap">
<span className="text-sm font-medium">{name}</span>
{status === "ok" && (
<span className="text-xs text-green-600 dark:text-green-400"> Up to date</span>
)}
{status === "needed" && count !== undefined && count > 0 && (
<span className="text-xs text-red-600 dark:text-red-400">
{count.toLocaleString()} {countLabel}
</span>
)}
{taskIds[key] && (
<span className="text-xs text-muted-foreground">queued </span>
)}
</div>
<p className="text-xs text-muted-foreground leading-relaxed">{description}</p>
</div>
<button
onClick={() => trigger(key, fn)}
className="shrink-0 px-3 py-1.5 text-xs bg-muted hover:bg-accent rounded-md transition-colors font-medium"
>
Run
</button>
</div>
))}
</div>
{Object.entries(taskIds).map(([name, id]) => (
<p key={name} className="text-xs text-muted-foreground">{name}: task {id} queued</p>
))}
</section>
</div>
);