"use client"; import { BotIcon, PlusIcon, Trash2Icon } from "lucide-react"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { useState } from "react"; import { toast } from "sonner"; import { CopyButton } from "@/components/copy-button"; import { LocalTime } from "@/components/local-time"; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, AlertDialogTrigger, } from "@/components/ui/alert-dialog"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "@/components/ui/card"; import { Checkbox } from "@/components/ui/checkbox"; import { Input } from "@/components/ui/input"; import { SCOPE_LABEL } from "@/lib/token-scopes"; import type { ApiTokenRow } from "@/lib/api-tokens"; import { TOKEN_SCOPES, type TokenScope } from "@/lib/db/schema"; /** Bearer tokens for /api/v1 (e.g. an AI agent). The token is shown once, then only its prefix. */ export function TokenSettings({ tokens, activity, }: { tokens: ApiTokenRow[]; activity: Record; }) { const [created, setCreated] = useState<{ name: string; token: string; } | null>(null); return ( API tokens For programs and agents calling Kaname's API at{" "} /api/v1 with{" "} Authorization: Bearer <token> . No token can delete models or files, change settings, or manage runtimes and tokens. {created && (

Token “{created.name}” created. Copy it now: it is not shown again.

{created.token}
)} {tokens.length === 0 ? (

No tokens yet.

) : (
{tokens.map((t) => ( ))}
)}
); } function NewTokenForm({ onCreated, }: { onCreated: (t: { name: string; token: string }) => void; }) { const router = useRouter(); const [name, setName] = useState(""); const [scopes, setScopes] = useState(["models:read"]); const [busy, setBusy] = useState(false); async function create(e: React.FormEvent) { e.preventDefault(); setBusy(true); try { const res = await fetch("/api/api-tokens", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ name, scopes }), }); const json = await res.json().catch(() => ({})); if (!res.ok) return toast.error( json.error?.message ?? `Could not create the token (${res.status})`, ); onCreated({ name: name.trim().toLowerCase(), token: json.token }); setName(""); router.refresh(); } finally { setBusy(false); } } return (
setName(e.target.value.toLowerCase().replace(/[^a-z0-9._-]/g, "")) } placeholder="Name, e.g. hermes" aria-label="Token name" className="max-w-56" />
{TOKEN_SCOPES.map((s) => ( ))}

The name is shown as “requested by” on everything the token asks for.

); } function TokenRow({ token, activity, }: { token: ApiTokenRow; activity: ActivityItem[]; }) { const router = useRouter(); const revoked = !!token.revokedAt; const [editing, setEditing] = useState(false); const [scopes, setScopes] = useState(token.scopes); const [showActivity, setShowActivity] = useState(false); async function saveScopes() { const res = await fetch(`/api/api-tokens/${token.id}`, { method: "PATCH", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ scopes }), }); const json = await res.json().catch(() => ({})); if (!res.ok) return void toast.error(json.error?.message ?? `Failed (${res.status})`); toast.success(`Scopes of "${token.name}" changed`); setEditing(false); router.refresh(); } async function act(method: "POST" | "DELETE") { const res = await fetch(`/api/api-tokens/${token.id}`, { method }); const json = await res.json().catch(() => ({})); if (!res.ok) toast.error(json.error?.message ?? `Failed (${res.status})`); else toast.success( method === "POST" ? `Token "${token.name}" revoked` : `Token "${token.name}" deleted`, ); router.refresh(); } return (
{token.name} {token.prefix}… {token.scopes.map((s) => ( {s} ))} {revoked && revoked} {token.lastUsedAt ? ( <> used ) : ( "never used" )} {activity.length > 0 && ( )} {!revoked && ( )} }> {revoked ? : "Revoke"} {revoked ? "Delete" : "Revoke"} token “{token.name} ”? {revoked ? "The entry goes away and the name can be used again. Models it requested keep their “requested by” label." : "It stops working immediately. Downloads it already requested are not affected."} Cancel act(revoked ? "DELETE" : "POST")} > {revoked ? "Delete" : "Revoke"}
{editing && (
{TOKEN_SCOPES.map((s) => ( ))}
)} {showActivity && }
); } export type ActivityItem = { id: string; action: string; targetType: string | null; targetId: string | null; outcome: string; code: string | null; message: string | null; createdAt: string; }; const TARGET_HREF: Record string> = { deployment: (id) => `/deployments/${id}`, benchmark: (id) => `/benchmarks/${id}`, sweep: (id) => `/benchmarks/sweeps/${id}`, model: (id) => `/models/${id}`, }; /** What the token did, newest first: outcome, and the reason when a guardrail refused it. */ function ActivityList({ items }: { items: ActivityItem[] }) { return (
    {items.map((a) => (
  • {a.action} {a.outcome === "refused" ? `refused: ${a.code}` : a.outcome === "pending_approval" ? "awaiting approval" : "ok"} {a.targetType && a.targetId && TARGET_HREF[a.targetType] && ( {a.targetType} )} {a.message && ( {a.message} )}
  • ))}
); }