feat: M1 admin console — all 10 remaining pages + signal/quality/ops infrastructure
Admin console (issues #63–72): - Event stream viewer: live-tail ring buffer (500 events) with subject/user filters - Feature store browser: per-user feature vector history from ml/serving - Model registry panel: MLflow embed at /admin/models - Experiment dashboard: LinUCB per-user stats (pulls, reward, θ) + bandit reset - Recommendation log: per-tip explainability (policy, score, features, latency) - Reward analytics: daily reaction breakdown + per-policy compare - Data quality widget: missing-feature rate, stale-token rate, daily completeness - Ops actions: replay-signal, policy enable/disable; user actions link to Users page - SQL runner: read-only SELECT runner with saved queries - Health rollup: fan-out to api/ml/sqlite/event-bus with auto-refresh Backend: - tip_scores table: logs features+policy+score+latency at every scoring call (#67) - saved_queries table: per-admin saved SQL (#71) - Event bus: 500-event ring buffer + tail() API (#63) - Admin routes: /events, /tips, /reward-analytics, /data-quality, /health, /policies, /replay-signal, /sql, /saved-queries endpoints - /api/ml/* admin-gated proxy to ml/serving (#64, #66) - Shadow-policy registry in recommender (#56) ML serving: - /reset/{user_id}: clear bandit state + feature history (#66) - /stats/{user_id}: pulls, cumulative reward, estimated mean, θ (#66) - /features/{user_id}: last 100 feature vectors logged at scoring time (#64) - Meta (pulls, rewards) persisted alongside A/b matrices Web: - Tip action sheet adds Helpful / Not helpful buttons (#62) - TipFeedback type extended with helpful/not_helpful actions - Rewards mapped: helpful=+0.5, not_helpful=−0.5 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
222
apps/admin/src/lib/api.ts
Normal file
222
apps/admin/src/lib/api.ts
Normal file
@@ -0,0 +1,222 @@
|
||||
const API = '/api';
|
||||
|
||||
async function apiFetch<T>(path: string, init?: RequestInit): Promise<T> {
|
||||
const res = await fetch(`${API}${path}`, {
|
||||
credentials: 'include',
|
||||
...init,
|
||||
headers: { 'Content-Type': 'application/json', ...init?.headers },
|
||||
});
|
||||
if (!res.ok) {
|
||||
const err = await res.json().catch(() => ({ error: res.statusText }));
|
||||
throw Object.assign(new Error(err.error ?? 'API error'), { status: res.status });
|
||||
}
|
||||
return res.json() as T;
|
||||
}
|
||||
|
||||
// ── Types ──────────────────────────────────────────────────────────────────
|
||||
|
||||
export interface AdminStats {
|
||||
dau: number;
|
||||
wau: number;
|
||||
tipsServedLast7d: number;
|
||||
reactionsLast7d: Record<string, number>;
|
||||
totalUsers: number;
|
||||
activatedUsers: number;
|
||||
}
|
||||
|
||||
export interface AdminUser {
|
||||
id: string;
|
||||
email: string;
|
||||
name: string | null;
|
||||
image: string | null;
|
||||
role: string;
|
||||
consentGiven: boolean;
|
||||
consentAt: string | null;
|
||||
createdAt: string;
|
||||
deletedAt: string | null;
|
||||
}
|
||||
|
||||
export interface AdminUserDetail {
|
||||
user: AdminUser;
|
||||
integrations: { provider: string; connectedAt: string }[];
|
||||
tipsServed: number;
|
||||
lastTipAt: string | null;
|
||||
recentFeedback: { id: string; action: string; createdAt: string; tipId: string }[];
|
||||
}
|
||||
|
||||
export interface AuditAction {
|
||||
id: string;
|
||||
adminId: string;
|
||||
action: string;
|
||||
targetType: string | null;
|
||||
targetId: string | null;
|
||||
detail: string | null;
|
||||
createdAt: string;
|
||||
}
|
||||
|
||||
export interface StoredEvent {
|
||||
id: number;
|
||||
subject: string;
|
||||
payload: unknown;
|
||||
ts: string;
|
||||
}
|
||||
|
||||
export interface TipScore {
|
||||
id: string;
|
||||
userId: string;
|
||||
tipId: string;
|
||||
policy: string;
|
||||
mlScore: number | null;
|
||||
featuresJson: string | null;
|
||||
candidateCount: number | null;
|
||||
latencyMs: number | null;
|
||||
servedAt: string;
|
||||
}
|
||||
|
||||
export interface HealthStatus {
|
||||
ok: boolean;
|
||||
checkedAt: string;
|
||||
services: { name: string; status: string; latencyMs: number }[];
|
||||
}
|
||||
|
||||
export interface PolicyInfo {
|
||||
name: string;
|
||||
active: boolean;
|
||||
}
|
||||
|
||||
export interface SavedQuery {
|
||||
id: string;
|
||||
name: string;
|
||||
sql: string;
|
||||
createdAt: string;
|
||||
}
|
||||
|
||||
export interface BanditStats {
|
||||
user_id: string;
|
||||
pulls: number;
|
||||
reward_count: number;
|
||||
cumulative_reward: number;
|
||||
estimated_mean_reward: number;
|
||||
theta: number[];
|
||||
last_updated: string | null;
|
||||
}
|
||||
|
||||
export interface FeatureHistory {
|
||||
user_id: string;
|
||||
history: { ts: string; features: Record<string, unknown>; score: number; tip_id: string }[];
|
||||
}
|
||||
|
||||
// ── Fetchers ───────────────────────────────────────────────────────────────
|
||||
|
||||
export function getStats() {
|
||||
return apiFetch<AdminStats>('/admin/stats');
|
||||
}
|
||||
|
||||
export function getUsers(limit = 50, offset = 0) {
|
||||
return apiFetch<{ users: AdminUser[]; total: number }>(
|
||||
`/admin/users?limit=${limit}&offset=${offset}`,
|
||||
);
|
||||
}
|
||||
|
||||
export function getUserDetail(id: string) {
|
||||
return apiFetch<AdminUserDetail>(`/admin/users/${id}`);
|
||||
}
|
||||
|
||||
export function revokeIntegration(userId: string, provider: string) {
|
||||
return apiFetch<{ ok: boolean }>(`/admin/users/${userId}/revoke-integration`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ provider }),
|
||||
});
|
||||
}
|
||||
|
||||
export function resetBandit(userId: string) {
|
||||
return apiFetch<{ ok: boolean }>(`/admin/users/${userId}/reset-bandit`, {
|
||||
method: 'POST',
|
||||
});
|
||||
}
|
||||
|
||||
export function getAuditLog(limit = 50, offset = 0) {
|
||||
return apiFetch<{ actions: AuditAction[]; total: number }>(
|
||||
`/admin/audit?limit=${limit}&offset=${offset}`,
|
||||
);
|
||||
}
|
||||
|
||||
export function getEvents(params: { subject?: string; userId?: string; limit?: number; since?: number } = {}) {
|
||||
const q = new URLSearchParams();
|
||||
if (params.subject) q.set('subject', params.subject);
|
||||
if (params.userId) q.set('userId', params.userId);
|
||||
if (params.limit) q.set('limit', String(params.limit));
|
||||
if (params.since) q.set('since', String(params.since));
|
||||
return apiFetch<{ events: StoredEvent[]; nextSince: number }>(`/admin/events?${q}`);
|
||||
}
|
||||
|
||||
export function getTips(params: { limit?: number; offset?: number; userId?: string } = {}) {
|
||||
const q = new URLSearchParams();
|
||||
if (params.limit) q.set('limit', String(params.limit));
|
||||
if (params.offset) q.set('offset', String(params.offset));
|
||||
if (params.userId) q.set('userId', params.userId);
|
||||
return apiFetch<{ tips: TipScore[]; total: number }>(`/admin/tips?${q}`);
|
||||
}
|
||||
|
||||
export function getRewardAnalytics(days = 30) {
|
||||
return apiFetch<{
|
||||
daily: { date: string; action: string; count: number }[];
|
||||
byPolicy: { policy: string; action: string; count: number }[];
|
||||
byHour: { action: string; count: number; avgHour: number }[];
|
||||
}>(`/admin/reward-analytics?days=${days}`);
|
||||
}
|
||||
|
||||
export function getDataQuality() {
|
||||
return apiFetch<{
|
||||
scoringCallsLast30d: number;
|
||||
missingFeatureRate: number;
|
||||
staleTokenRate: number;
|
||||
totalTokens: number;
|
||||
staleTokens: number;
|
||||
dailyQuality: { date: string; total: number; withFeatures: number; avgCandidates: number }[];
|
||||
}>('/admin/data-quality');
|
||||
}
|
||||
|
||||
export function getHealth() {
|
||||
return apiFetch<HealthStatus>('/admin/health');
|
||||
}
|
||||
|
||||
export function getPolicies() {
|
||||
return apiFetch<{ policies: PolicyInfo[] }>('/admin/policies');
|
||||
}
|
||||
|
||||
export function togglePolicy(name: string, active: boolean) {
|
||||
return apiFetch<{ ok: boolean }>(`/admin/policies/${name}/toggle`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ active }),
|
||||
});
|
||||
}
|
||||
|
||||
export function replaySignal(subject: string, payload: Record<string, unknown>) {
|
||||
return apiFetch<{ ok: boolean }>('/admin/replay-signal', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ subject, payload }),
|
||||
});
|
||||
}
|
||||
|
||||
export function runSql(query: string) {
|
||||
return apiFetch<{ rows: unknown[]; rowCount: number }>('/admin/sql', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ query }),
|
||||
});
|
||||
}
|
||||
|
||||
export function getSavedQueries() {
|
||||
return apiFetch<{ queries: SavedQuery[] }>('/admin/saved-queries');
|
||||
}
|
||||
|
||||
export function saveQuery(name: string, querySql: string) {
|
||||
return apiFetch<{ id: string }>('/admin/saved-queries', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ name, querySql }),
|
||||
});
|
||||
}
|
||||
|
||||
export function deleteSavedQuery(id: string) {
|
||||
return apiFetch<{ ok: boolean }>(`/admin/saved-queries/${id}`, { method: 'DELETE' });
|
||||
}
|
||||
119
apps/admin/src/lib/docs.ts
Normal file
119
apps/admin/src/lib/docs.ts
Normal file
@@ -0,0 +1,119 @@
|
||||
/**
|
||||
* Server-side utilities for reading project documentation from the monorepo
|
||||
* `docs/` directory and rendering it as HTML.
|
||||
*
|
||||
* All functions are async and must only be called from server components or
|
||||
* server actions (no 'use client' imports of this module).
|
||||
*
|
||||
* Directory layout relative to monorepo root:
|
||||
* docs/adr/ — Architecture Decision Records (NNN-title.md)
|
||||
* docs/architecture/ — longer architecture notes (topic.md)
|
||||
*/
|
||||
import { readdir, readFile } from 'fs/promises';
|
||||
import path from 'path';
|
||||
import { marked } from 'marked';
|
||||
|
||||
// apps/admin sits two levels below the monorepo root.
|
||||
const DOCS_ROOT = path.resolve(process.cwd(), '../../docs');
|
||||
|
||||
export type DocCategory = 'adr' | 'architecture';
|
||||
|
||||
export interface DocMeta {
|
||||
category: DocCategory;
|
||||
slug: string; // filename without .md
|
||||
title: string; // first H1 from the file, fallback = slug
|
||||
href: string; // /docs/adr/0001-monorepo-polyglot
|
||||
status?: string; // for ADRs: "Accepted", "Proposed", …
|
||||
date?: string; // for ADRs: date after em-dash on Status line
|
||||
}
|
||||
|
||||
export interface DocPage extends DocMeta {
|
||||
html: string;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// internal helpers
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/** Extract the first # heading from markdown. */
|
||||
function extractTitle(md: string): string {
|
||||
const m = md.match(/^#\s+(.+)$/m);
|
||||
return m ? m[1].trim() : '';
|
||||
}
|
||||
|
||||
/** Extract status + date from "## Status\nAccepted — 2026-04-13" pattern. */
|
||||
function extractStatus(md: string): { status?: string; date?: string } {
|
||||
const block = md.match(/##\s+Status\s*\n+([^\n#]+)/);
|
||||
if (!block) return {};
|
||||
const line = block[1].trim();
|
||||
// "Accepted — 2026-04-13" or "Proposed"
|
||||
const parts = line.split(/\s*[–—]\s*/);
|
||||
return { status: parts[0]?.trim(), date: parts[1]?.trim() };
|
||||
}
|
||||
|
||||
function slugFromFile(filename: string): string {
|
||||
return filename.replace(/\.md$/, '');
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// public API
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/** List all docs in a category, sorted by filename. */
|
||||
export async function listDocs(category: DocCategory): Promise<DocMeta[]> {
|
||||
const dir = path.join(DOCS_ROOT, category);
|
||||
let files: string[];
|
||||
try {
|
||||
files = (await readdir(dir)).filter((f) => f.endsWith('.md')).sort();
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
|
||||
return Promise.all(
|
||||
files.map(async (file) => {
|
||||
const slug = slugFromFile(file);
|
||||
const md = await readFile(path.join(dir, file), 'utf8');
|
||||
const title = extractTitle(md) || slug;
|
||||
const { status, date } = extractStatus(md);
|
||||
return {
|
||||
category,
|
||||
slug,
|
||||
title,
|
||||
href: `/docs/${category}/${slug}`,
|
||||
status,
|
||||
date,
|
||||
};
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
/** List all docs across all categories. */
|
||||
export async function listAllDocs(): Promise<Record<DocCategory, DocMeta[]>> {
|
||||
const [adr, architecture] = await Promise.all([listDocs('adr'), listDocs('architecture')]);
|
||||
return { adr, architecture };
|
||||
}
|
||||
|
||||
/** Read and render a single doc to HTML. */
|
||||
export async function getDoc(category: DocCategory, slug: string): Promise<DocPage | null> {
|
||||
const file = path.join(DOCS_ROOT, category, `${slug}.md`);
|
||||
let md: string;
|
||||
try {
|
||||
md = await readFile(file, 'utf8');
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
|
||||
const title = extractTitle(md) || slug;
|
||||
const { status, date } = extractStatus(md);
|
||||
const html = await marked(md, { gfm: true });
|
||||
|
||||
return {
|
||||
category,
|
||||
slug,
|
||||
title,
|
||||
href: `/docs/${category}/${slug}`,
|
||||
status,
|
||||
date,
|
||||
html,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user