feat: auto-detect events, seva tasks, and FAQs from WhatsApp messages
Adds the AI-driven content pipeline so portal content is never manually created — the system proposes it, admins review and approve. Worker: - `tower.event.extract` queue + Event Extractor processor: LLM extracts event title/date/time/location from messages with #event hashtag, date/time patterns, RSVP phrases, or location terms; seva action items within event messages become separate SEVA_TASK drafts - `tower.faq.candidate` queue + FAQ Curator processor: LLM extracts Q&A pairs from messages with question patterns + meaningful answer length - `classify.processor.ts`: `isEventCandidate()` and `isFaqCandidate()` run after rule matching on all non-spam, non-DNC messages; candidates are enqueued to the extraction lanes in parallel with normal routing - `match-rules.ts`: add P1 to TenantRuleRow action union so P1 rules are fully typed end-to-end - `NormalizedMessage`: add `quotedPlatformMsgId` field - `IngestJobData.effectiveAction`: add `'P1'` to the union Schema: - `ContentDraft` model with `DraftType` (EVENT|SEVA_TASK|FAQ_ITEM) and `DraftStatus` (PENDING_REVIEW|APPROVED|REJECTED); carries `proposedJson` from the LLM, `confidence` score, and `publishedId` after approval - Migration: `20260617270000_add_content_drafts` API: - `DraftsModule`: `GET /admin/drafts`, `POST /admin/drafts/:id/approve`, `POST /admin/drafts/:id/reject`; approve creates the actual Event/SevaOpportunity/KnowledgeItem and marks the draft APPROVED; reject marks it REJECTED — both audit-logged Web: - `/admin/drafts` and `/drafts` — tabbed view by type (All/Events/Seva/FAQ) - `DraftCard` — shows source message excerpt, AI proposed content, and approve/reject buttons; approve/reject immediately refresh the list - "AI Drafts" added to both super-admin and tenant-admin sidebars - "AI Content Drafts" button added to admin dashboard Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -10,6 +10,7 @@ const NAV_LINKS = [
|
||||
{ href: '/search', label: 'Search' },
|
||||
{ href: '/groups', label: 'Groups & Routes' },
|
||||
{ href: '/messages/pending', label: 'Pending messages' },
|
||||
{ href: '/drafts', label: 'AI Drafts' },
|
||||
{ href: '/settings/rules', label: 'Rules' },
|
||||
{ href: '/settings/bot', label: 'Bot' },
|
||||
];
|
||||
@@ -18,6 +19,7 @@ const SUPER_ADMIN_LINKS = [
|
||||
{ href: '/admin', label: 'Dashboard' },
|
||||
{ href: '/admin/tenants', label: 'Tenants' },
|
||||
{ href: '/admin/bots', label: 'Bot Pool' },
|
||||
{ href: '/admin/drafts', label: 'AI Drafts' },
|
||||
];
|
||||
|
||||
const PUBLIC_PATHS = ['/login', '/signup', '/onboard'];
|
||||
|
||||
@@ -0,0 +1,137 @@
|
||||
'use client';
|
||||
|
||||
import { useState } from 'react';
|
||||
import { useRouter } from 'next/navigation';
|
||||
|
||||
interface SourceMessage {
|
||||
id: string;
|
||||
content: string;
|
||||
senderName: string | null;
|
||||
createdAt: string;
|
||||
}
|
||||
|
||||
interface Draft {
|
||||
id: string;
|
||||
type: 'EVENT' | 'SEVA_TASK' | 'FAQ_ITEM';
|
||||
status: string;
|
||||
proposedJson: Record<string, any>;
|
||||
confidence: number | null;
|
||||
createdAt: string;
|
||||
sourceMessage: SourceMessage;
|
||||
}
|
||||
|
||||
const TYPE_LABEL: Record<Draft['type'], string> = {
|
||||
EVENT: 'Event',
|
||||
SEVA_TASK: 'Seva Task',
|
||||
FAQ_ITEM: 'FAQ',
|
||||
};
|
||||
|
||||
const TYPE_COLOR: Record<Draft['type'], string> = {
|
||||
EVENT: 'bg-indigo-50 text-indigo-700 border-indigo-200',
|
||||
SEVA_TASK: 'bg-green-50 text-green-700 border-green-200',
|
||||
FAQ_ITEM: 'bg-amber-50 text-amber-700 border-amber-200',
|
||||
};
|
||||
|
||||
function ProposedPreview({ type, data }: { type: Draft['type']; data: Record<string, any> }) {
|
||||
if (type === 'EVENT') {
|
||||
return (
|
||||
<div className="space-y-1 text-sm">
|
||||
<p><span className="font-medium text-gray-700">Title:</span> {data.title ?? '—'}</p>
|
||||
{data.date && <p><span className="font-medium text-gray-700">Date:</span> {data.date} {data.time ?? ''}</p>}
|
||||
{data.location && <p><span className="font-medium text-gray-700">Location:</span> {data.location}</p>}
|
||||
{data.description && <p><span className="font-medium text-gray-700">Description:</span> {data.description}</p>}
|
||||
{data.rsvpNeeded && <p className="text-indigo-600 text-xs">RSVP required</p>}
|
||||
{data.sevaActionItems?.length > 0 && (
|
||||
<p className="text-green-700 text-xs">+{data.sevaActionItems.length} seva task(s) will also be created</p>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (type === 'SEVA_TASK') {
|
||||
return (
|
||||
<div className="space-y-1 text-sm">
|
||||
<p><span className="font-medium text-gray-700">Title:</span> {data.title ?? '—'}</p>
|
||||
{data.parentEventTitle && <p><span className="font-medium text-gray-700">Event:</span> {data.parentEventTitle}</p>}
|
||||
{data.slots && <p><span className="font-medium text-gray-700">Slots:</span> {data.slots}</p>}
|
||||
{data.skills?.length > 0 && <p><span className="font-medium text-gray-700">Skills:</span> {data.skills.join(', ')}</p>}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="space-y-1 text-sm">
|
||||
<p><span className="font-medium text-gray-700">Q:</span> {data.question ?? '—'}</p>
|
||||
<p><span className="font-medium text-gray-700">A:</span> {data.answer ?? '—'}</p>
|
||||
{data.tags?.length > 0 && (
|
||||
<div className="flex gap-1 flex-wrap mt-1">
|
||||
{data.tags.map((t: string) => (
|
||||
<span key={t} className="text-xs bg-gray-100 text-gray-600 px-2 py-0.5 rounded-full">{t}</span>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export function DraftCard({ draft }: { draft: Draft }) {
|
||||
const router = useRouter();
|
||||
const [loading, setLoading] = useState<'approve' | 'reject' | null>(null);
|
||||
|
||||
async function act(action: 'approve' | 'reject') {
|
||||
setLoading(action);
|
||||
try {
|
||||
await fetch(`/api/admin/drafts/${draft.id}/${action}`, { method: 'POST' });
|
||||
router.refresh();
|
||||
} finally {
|
||||
setLoading(null);
|
||||
}
|
||||
}
|
||||
|
||||
const confidence = draft.confidence != null ? Math.round(draft.confidence * 100) : null;
|
||||
|
||||
return (
|
||||
<div className="bg-white border border-gray-200 rounded-xl p-5 space-y-4">
|
||||
<div className="flex items-start justify-between gap-3">
|
||||
<span className={`text-xs font-semibold px-2.5 py-1 rounded-full border ${TYPE_COLOR[draft.type]}`}>
|
||||
{TYPE_LABEL[draft.type]}
|
||||
</span>
|
||||
{confidence != null && (
|
||||
<span className="text-xs text-gray-400">{confidence}% confidence</span>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Source message */}
|
||||
<div className="bg-gray-50 rounded-lg p-3 border border-gray-100">
|
||||
<p className="text-xs font-medium text-gray-500 mb-1">
|
||||
Source message{draft.sourceMessage.senderName ? ` — ${draft.sourceMessage.senderName}` : ''}
|
||||
</p>
|
||||
<p className="text-sm text-gray-700 line-clamp-3">{draft.sourceMessage.content}</p>
|
||||
</div>
|
||||
|
||||
{/* AI proposed content */}
|
||||
<div>
|
||||
<p className="text-xs font-medium text-gray-500 mb-2">AI proposed</p>
|
||||
<ProposedPreview type={draft.type} data={draft.proposedJson} />
|
||||
</div>
|
||||
|
||||
{/* Actions */}
|
||||
<div className="flex gap-2 pt-1">
|
||||
<button
|
||||
onClick={() => act('approve')}
|
||||
disabled={loading !== null}
|
||||
className="flex-1 py-2 text-sm font-medium rounded-lg bg-indigo-600 text-white hover:bg-indigo-700 disabled:opacity-50 transition-colors"
|
||||
>
|
||||
{loading === 'approve' ? 'Approving…' : 'Approve'}
|
||||
</button>
|
||||
<button
|
||||
onClick={() => act('reject')}
|
||||
disabled={loading !== null}
|
||||
className="flex-1 py-2 text-sm font-medium rounded-lg border border-gray-200 text-gray-600 hover:bg-gray-50 disabled:opacity-50 transition-colors"
|
||||
>
|
||||
{loading === 'reject' ? 'Rejecting…' : 'Reject'}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
import { apiFetch } from '../../_lib/api';
|
||||
import { redirect } from 'next/navigation';
|
||||
import { getToken } from '../../_lib/api';
|
||||
import { DraftCard } from './DraftCard';
|
||||
import Link from 'next/link';
|
||||
|
||||
type DraftType = 'EVENT' | 'SEVA_TASK' | 'FAQ_ITEM';
|
||||
|
||||
interface Draft {
|
||||
id: string;
|
||||
type: DraftType;
|
||||
status: string;
|
||||
proposedJson: Record<string, any>;
|
||||
confidence: number | null;
|
||||
createdAt: string;
|
||||
sourceMessage: {
|
||||
id: string;
|
||||
content: string;
|
||||
senderName: string | null;
|
||||
createdAt: string;
|
||||
};
|
||||
}
|
||||
|
||||
const TABS: { label: string; value: DraftType | 'ALL' }[] = [
|
||||
{ label: 'All', value: 'ALL' },
|
||||
{ label: 'Events', value: 'EVENT' },
|
||||
{ label: 'Seva Tasks', value: 'SEVA_TASK' },
|
||||
{ label: 'FAQs', value: 'FAQ_ITEM' },
|
||||
];
|
||||
|
||||
async function fetchDrafts(type?: DraftType): Promise<Draft[]> {
|
||||
const query = new URLSearchParams({ status: 'PENDING_REVIEW' });
|
||||
if (type) query.set('type', type);
|
||||
const res = await apiFetch(`/admin/drafts?${query}`);
|
||||
if (!res.ok) return [];
|
||||
return (await res.json()) as Draft[];
|
||||
}
|
||||
|
||||
export default async function DraftsPage({
|
||||
searchParams,
|
||||
}: {
|
||||
searchParams: Promise<{ type?: string }>;
|
||||
}) {
|
||||
const token = await getToken();
|
||||
if (!token) redirect('/login');
|
||||
|
||||
const { type } = await searchParams;
|
||||
const activeType = (type as DraftType) ?? undefined;
|
||||
const drafts = await fetchDrafts(activeType);
|
||||
|
||||
return (
|
||||
<div className="p-6 max-w-5xl mx-auto space-y-6">
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<h1 className="text-xl font-semibold text-gray-900">AI Content Drafts</h1>
|
||||
<p className="text-sm text-gray-500 mt-0.5">
|
||||
Events, seva tasks, and FAQs auto-detected from WhatsApp messages
|
||||
</p>
|
||||
</div>
|
||||
<Link href="/admin" className="text-sm text-indigo-600 hover:underline">← Admin</Link>
|
||||
</div>
|
||||
|
||||
{/* Type filter tabs */}
|
||||
<div className="flex gap-1 border-b border-gray-200">
|
||||
{TABS.map((tab) => {
|
||||
const href = tab.value === 'ALL' ? '/admin/drafts' : `/admin/drafts?type=${tab.value}`;
|
||||
const isActive = (tab.value === 'ALL' && !activeType) || tab.value === activeType;
|
||||
return (
|
||||
<Link
|
||||
key={tab.value}
|
||||
href={href}
|
||||
className={`px-4 py-2 text-sm font-medium border-b-2 -mb-px transition-colors ${
|
||||
isActive
|
||||
? 'border-indigo-600 text-indigo-600'
|
||||
: 'border-transparent text-gray-500 hover:text-gray-700'
|
||||
}`}
|
||||
>
|
||||
{tab.label}
|
||||
</Link>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
|
||||
{drafts.length === 0 ? (
|
||||
<div className="text-center py-20 text-gray-400">
|
||||
<p className="text-sm font-medium">No pending drafts</p>
|
||||
<p className="text-xs mt-1">
|
||||
Drafts appear here when the AI detects events, volunteer asks, or FAQ patterns in group messages.
|
||||
</p>
|
||||
</div>
|
||||
) : (
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
|
||||
{drafts.map((d) => (
|
||||
<DraftCard key={d.id} draft={d} />
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -49,9 +49,10 @@ export default function AdminDashboard() {
|
||||
<div className="text-2xl font-bold mt-1">{totalBots ? Math.round(totalTenants / totalBots) : 0}</div>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex gap-4">
|
||||
<div className="flex gap-4 flex-wrap">
|
||||
<Link href="/admin/tenants" className="bg-blue-600 text-white rounded-lg px-4 py-2 text-sm">Manage Tenants</Link>
|
||||
<Link href="/admin/bots" className="bg-blue-600 text-white rounded-lg px-4 py-2 text-sm">Manage Bots</Link>
|
||||
<Link href="/admin/drafts" className="bg-indigo-600 text-white rounded-lg px-4 py-2 text-sm">AI Content Drafts</Link>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
import { jsonResponse, apiFetch } from '../../../../../_lib/api';
|
||||
|
||||
export const dynamic = 'force-dynamic';
|
||||
|
||||
export async function POST(
|
||||
_request: Request,
|
||||
{ params }: { params: Promise<{ id: string }> },
|
||||
): Promise<Response> {
|
||||
const { id } = await params;
|
||||
const res = await apiFetch(`/admin/drafts/${id}/approve`, { method: 'POST' });
|
||||
const body = await res.json();
|
||||
return jsonResponse(body, res.status);
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import { jsonResponse, apiFetch } from '../../../../../_lib/api';
|
||||
|
||||
export const dynamic = 'force-dynamic';
|
||||
|
||||
export async function POST(
|
||||
_request: Request,
|
||||
{ params }: { params: Promise<{ id: string }> },
|
||||
): Promise<Response> {
|
||||
const { id } = await params;
|
||||
const res = await apiFetch(`/admin/drafts/${id}/reject`, { method: 'POST' });
|
||||
const body = await res.json();
|
||||
return jsonResponse(body, res.status);
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import { jsonResponse, apiFetch } from '../../../_lib/api';
|
||||
|
||||
export const dynamic = 'force-dynamic';
|
||||
|
||||
export async function GET(request: Request): Promise<Response> {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const type = searchParams.get('type');
|
||||
const status = searchParams.get('status') ?? 'PENDING_REVIEW';
|
||||
const query = new URLSearchParams({ status });
|
||||
if (type) query.set('type', type);
|
||||
const res = await apiFetch(`/admin/drafts?${query}`);
|
||||
const body = await res.json();
|
||||
return jsonResponse(body, res.status);
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
import { apiFetch } from '../_lib/api';
|
||||
import { redirect } from 'next/navigation';
|
||||
import { getToken } from '../_lib/api';
|
||||
import { DraftCard } from '../admin/drafts/DraftCard';
|
||||
import Link from 'next/link';
|
||||
|
||||
type DraftType = 'EVENT' | 'SEVA_TASK' | 'FAQ_ITEM';
|
||||
|
||||
interface Draft {
|
||||
id: string;
|
||||
type: DraftType;
|
||||
status: string;
|
||||
proposedJson: Record<string, any>;
|
||||
confidence: number | null;
|
||||
createdAt: string;
|
||||
sourceMessage: {
|
||||
id: string;
|
||||
content: string;
|
||||
senderName: string | null;
|
||||
createdAt: string;
|
||||
};
|
||||
}
|
||||
|
||||
const TABS: { label: string; value: DraftType | 'ALL' }[] = [
|
||||
{ label: 'All', value: 'ALL' },
|
||||
{ label: 'Events', value: 'EVENT' },
|
||||
{ label: 'Seva Tasks', value: 'SEVA_TASK' },
|
||||
{ label: 'FAQs', value: 'FAQ_ITEM' },
|
||||
];
|
||||
|
||||
async function fetchDrafts(type?: DraftType): Promise<Draft[]> {
|
||||
const query = new URLSearchParams({ status: 'PENDING_REVIEW' });
|
||||
if (type) query.set('type', type);
|
||||
const res = await apiFetch(`/admin/drafts?${query}`);
|
||||
if (!res.ok) return [];
|
||||
return (await res.json()) as Draft[];
|
||||
}
|
||||
|
||||
export default async function TenantDraftsPage({
|
||||
searchParams,
|
||||
}: {
|
||||
searchParams: Promise<{ type?: string }>;
|
||||
}) {
|
||||
const token = await getToken();
|
||||
if (!token) redirect('/login');
|
||||
|
||||
const { type } = await searchParams;
|
||||
const activeType = (type as DraftType) ?? undefined;
|
||||
const drafts = await fetchDrafts(activeType);
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<h1 className="text-xl font-semibold text-gray-900">AI Content Drafts</h1>
|
||||
<p className="text-sm text-gray-500 mt-0.5">
|
||||
Events, seva tasks, and FAQs auto-detected from your group messages
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="flex gap-1 border-b border-gray-200">
|
||||
{TABS.map((tab) => {
|
||||
const href = tab.value === 'ALL' ? '/drafts' : `/drafts?type=${tab.value}`;
|
||||
const isActive = (tab.value === 'ALL' && !activeType) || tab.value === activeType;
|
||||
return (
|
||||
<Link
|
||||
key={tab.value}
|
||||
href={href}
|
||||
className={`px-4 py-2 text-sm font-medium border-b-2 -mb-px transition-colors ${
|
||||
isActive
|
||||
? 'border-indigo-600 text-indigo-600'
|
||||
: 'border-transparent text-gray-500 hover:text-gray-700'
|
||||
}`}
|
||||
>
|
||||
{tab.label}
|
||||
</Link>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
|
||||
{drafts.length === 0 ? (
|
||||
<div className="text-center py-20 text-gray-400">
|
||||
<p className="text-sm font-medium">No pending drafts</p>
|
||||
<p className="text-xs mt-1">
|
||||
Drafts appear here when the AI detects events, volunteer asks, or FAQ patterns in your group messages.
|
||||
</p>
|
||||
</div>
|
||||
) : (
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
|
||||
{drafts.map((d) => (
|
||||
<DraftCard key={d.id} draft={d} />
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user