Add Umami analytics, subscription tiers, tool-call metering

- Analytics: <Analytics /> in root layout loads umami when NEXT_PUBLIC_UMAMI_* env are set
- Subscription: free/pro tiers in Postgres, daily quotas (10msg/5tool free, 1000/500 pro)
- Chat route: checkAndConsume(chat_message) gate before streamText, 429 when exhausted
- Tool metering: getWeather wrapped with withLimit decorator that deducts tool_call quota
- Pricing page at /pricing with upgrade button
- /api/billing/checkout posts to EGBE payment gateway, /api/billing/webhook verifies HMAC and upgrades user on checkout.session.completed
- UsageBadge in chat header polls /api/usage every 15s
- Dropped now-unused entitlements.ts
This commit is contained in:
dmitry.galkin 2026-05-25 17:27:08 +04:00
parent 3e21c2334c
commit 22a172e92d
23 changed files with 1374 additions and 86 deletions

20
components/analytics.tsx Normal file
View file

@ -0,0 +1,20 @@
import Script from "next/script";
export function Analytics() {
const websiteId = process.env.NEXT_PUBLIC_UMAMI_WEBSITE_ID;
const scriptUrl = process.env.NEXT_PUBLIC_UMAMI_SCRIPT_URL;
if (!(websiteId && scriptUrl)) {
return null;
}
return (
<Script
async
data-website-id={websiteId}
defer
src={scriptUrl}
strategy="afterInteractive"
/>
);
}

View file

@ -1,11 +1,10 @@
"use client";
import { PanelLeftIcon } from "lucide-react";
import Link from "next/link";
import { memo } from "react";
import { Button } from "@/components/ui/button";
import { useSidebar } from "@/components/ui/sidebar";
import { VercelIcon } from "./icons";
import { UsageBadge } from "./usage-badge";
import { VisibilitySelector, type VisibilityType } from "./visibility-selector";
function PureChatHeader({
@ -34,15 +33,6 @@ function PureChatHeader({
<PanelLeftIcon className="size-4" />
</Button>
<Link
className="flex size-8 items-center justify-center rounded-lg md:hidden"
href="https://vercel.com/templates/next.js/chatbot"
rel="noopener noreferrer"
target="_blank"
>
<VercelIcon size={14} />
</Link>
{!isReadonly && (
<VisibilitySelector
chatId={chatId}
@ -50,19 +40,9 @@ function PureChatHeader({
/>
)}
<Button
asChild
className="hidden rounded-lg bg-foreground px-4 text-background hover:bg-foreground/90 md:ml-auto md:flex"
>
<Link
href="https://vercel.com/templates/next.js/chatbot"
rel="noopener noreferrer"
target="_blank"
>
<VercelIcon size={16} />
Deploy with Vercel
</Link>
</Button>
<div className="ml-auto flex items-center gap-2">
<UsageBadge />
</div>
</header>
);
}

View file

@ -0,0 +1,44 @@
"use client";
import Link from "next/link";
import useSWR from "swr";
import type { QuotaSummary } from "@/lib/subscription/service";
import { fetcher } from "@/lib/utils";
export function UsageBadge() {
const { data } = useSWR<QuotaSummary>(
`${process.env.NEXT_PUBLIC_BASE_PATH ?? ""}/api/usage`,
fetcher,
{
refreshInterval: 15_000,
revalidateOnFocus: true,
}
);
if (!data) {
return null;
}
const isPro = data.tier === "pro";
const lowQuota =
!isPro &&
(data.messages.remaining <= 2 || data.toolCalls.remaining <= 1);
return (
<Link
className={`flex items-center gap-2 rounded-lg border px-2.5 py-1 text-[11px] font-medium transition-colors ${
lowQuota
? "border-amber-500/40 bg-amber-500/10 text-amber-600 hover:bg-amber-500/15"
: "border-border/40 text-muted-foreground hover:text-foreground"
}`}
href="/pricing"
title={`${data.tier} tier — ${data.toolCalls.used}/${data.toolCalls.limit} tool calls today`}
>
<span className="uppercase tracking-wider opacity-60">{data.tier}</span>
<span>
{data.messages.used}/{data.messages.limit} msgs
</span>
{isPro ? null : <span aria-hidden></span>}
</Link>
);
}

View file

@ -0,0 +1,48 @@
"use client";
import { useState } from "react";
import { toast } from "sonner";
import { Button } from "@/components/ui/button";
import type { Tier } from "@/lib/subscription/config";
export function PricingClient({ currentTier }: { currentTier: Tier }) {
const [loading, setLoading] = useState(false);
if (currentTier === "pro") {
return (
<p className="text-muted-foreground text-sm">
You're on Pro. Manage your subscription through the receipt email link.
</p>
);
}
const upgrade = async () => {
setLoading(true);
try {
const response = await fetch("/api/billing/checkout", { method: "POST" });
const data = await response.json();
if (response.ok && data.url) {
window.location.href = data.url;
return;
}
toast.error(data.message ?? "Failed to start checkout");
} catch (error) {
toast.error(error instanceof Error ? error.message : "Checkout failed");
} finally {
setLoading(false);
}
};
return (
<div className="flex justify-center">
<Button
className="px-6"
disabled={loading}
onClick={upgrade}
size="lg"
>
{loading ? "Opening checkout..." : "Upgrade to Pro"}
</Button>
</div>
);
}