Skip to Content

Add Bot Checks, Disposable-Email Blocking and AI Spend Caps

Sign-up refuses bots and throwaway inboxes, and every AI call is capped per user, org and day.

Install

npm i @dapplelight/signet-server @dapplelight/signet-ai @dapplelight/signet-db hono drizzle-orm

Check a captcha on sign-up

src/server/abuse.ts
import { captchaVerifier, requireCaptcha } from "@dapplelight/signet-server/abuse/captcha"

const captcha = (c) =>
  c.env.APP_ENV === "local"
    ? captchaVerifier({ provider: "test" })
    : captchaVerifier({ provider: "turnstile", secret: c.env.TURNSTILE_SECRET, hostname: "app.example.com" })

export const bots = requireCaptcha({ verifier: captcha, trust: "cloudflare" })

Block disposable email

src/app.ts
import { Hono } from "hono"
import { blockDisposableEmail } from "@dapplelight/signet-server/abuse/email"
import { bots } from "./server/abuse"

const app = new Hono()

app.use("/api/auth/sign-up/*", bots)
app.use("/api/auth/forget-password", bots)
app.use("/api/auth/sign-up/*", blockDisposableEmail({ add: ["burner.example"], allow: ["partner.example"] }))

Show the widget

src/web/sign-up.tsx
import { CaptchaWidget } from "@dapplelight/signet-ui/screens/abuse-screens"

Send the token it hands you as the x-captcha-token header or a captchaToken body field.

Add the spend ledger

src/server/ai-spend.ts
import { aiSpendCaps, entitlementTables } from "@dapplelight/signet-db/columns/sqlite"
import { createAiSpend } from "@dapplelight/signet-db/ai-spend"
import { db } from "./db"

export const { usageCounters } = entitlementTables()
export const spendCaps = aiSpendCaps()

export const spend = createAiSpend(db, {
  tables: { usageCounters, aiSpendCaps: spendCaps },
  caps: { user: { day: 200_000 }, org: { month: 20_000_000 }, global: { month: 100_000_000 } },
})

Cost units are millionths of a US dollar.

Enforce and administer the caps

src/app.ts
import { aiSpendAdminRoutes, requireAiSpend } from "@dapplelight/signet-server/http/ai-spend"
import { spend } from "./server/ai-spend"
import { currentTenant } from "@dapplelight/signet-server/http/orgs"

const me = (c) => c.get("session").userId

app.use("/api/ai/*", requireAiSpend({ spend, subject: (c) => ({ userId: me(c), orgId: currentTenant(c).id }) }))
app.route("/api/admin/ai-spend", aiSpendAdminRoutes({
  spend,
  authorize: (c) => c.get("session")?.role === "admin",
  actor: (c) => ({ type: "user", id: me(c) }),
}))

Feed the extractor

src/server/ai.ts
import { createExtractor } from "@dapplelight/signet-ai"
import { spendKey } from "@dapplelight/signet-db/ai-spend"
import { spend } from "./ai-spend"
import { providers } from "./providers"

const extractor = createExtractor({ providers, ledger: spend.ledger })

export async function extractFor(args: Parameters<typeof extractor.extract>[0], userId: string, orgId: string) {
  return extractor.extract({ ...args, budget: { key: spendKey({ userId, orgId }) } })
}

For streamChat, pass onUsage: spend.onUsage instead, never both.

Next