// Integrations (operator) — platform/pooled external-API arrangements across // every scope, backed by the integration registry on arcadia-llm-gateway // (`/api/v1/integrations*`). The operator manages pooled credentials and // inspects cross-tenant usage metadata; secrets are write-only. import { useCallback, useEffect, useMemo, useState } from "react" import { AlertTriangle, CheckCircle2, FlaskConical, KeyRound, Pencil, Plug, Plus, Trash2, } from "lucide-react" import { ArcadiaError } from "@crema/arcadia-client" import { AppShell } from "~/components/layout/app-shell" import { Badge } from "~/components/ui/badge" import { Button } from "~/components/ui/button" import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "~/components/ui/card" import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "~/components/ui/dialog" import { Input } from "~/components/ui/input" import { Label } from "~/components/ui/label" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "~/components/ui/select" import { Switch } from "~/components/ui/switch" import { useGatewayClient } from "~/lib/gateway" import { addCredential, createIntegration, credentialHealth, deleteIntegration, formatUsd, listIntegrations, testIntegration, updateIntegration, usageSummary, type AuthKind, type Integration, type Scope, type UsageEntry, } from "~/lib/arcadia/integrations" const AUTH_KINDS: AuthKind[] = ["bearer_static", "api_key_header", "basic", "oauth2"] const SCOPES: Scope[] = ["platform", "tenant", "app", "user", "agent"] const SCOPE_FILTERS: Array = ["all", ...SCOPES] type Form = { scope: Scope scope_id: string provider: string capability: string display_name: string unit: string price_usd: string monthly_budget_usd: string secret_name: string auth_kind: AuthKind secret: string pooled: boolean } const emptyForm: Form = { scope: "platform", scope_id: "", provider: "", capability: "", display_name: "", unit: "call", price_usd: "", monthly_budget_usd: "", secret_name: "", auth_kind: "bearer_static", secret: "", pooled: true, } export default function IntegrationsRoute() { const gw = useGatewayClient() const [items, setItems] = useState([]) const [usage, setUsage] = useState([]) const [scopeFilter, setScopeFilter] = useState("all") const [loading, setLoading] = useState(true) const [error, setError] = useState(null) const [editing, setEditing] = useState(null) const [tests, setTests] = useState>({}) const refresh = useCallback(async () => { setError(null) const filter = scopeFilter === "all" ? {} : { scope: scopeFilter } try { const [list, use] = await Promise.all([ listIntegrations(gw, filter), usageSummary(gw, filter).catch(() => [] as UsageEntry[]), ]) setItems(list) setUsage(use) } catch (e) { setError(e instanceof Error ? e.message : "Failed to load integrations.") } finally { setLoading(false) } }, [gw, scopeFilter]) useEffect(() => { void refresh() }, [refresh]) const usageById = useMemo( () => new Map(usage.map((u) => [u.integration_id, u] as const)), [usage], ) const runTest = useCallback( async (it: Integration) => { setTests((t) => ({ ...t, [it.id]: { ok: true, message: "Testing…" } })) try { const verdict = await testIntegration(gw, it.id) const remaining = verdict.policy?.remaining_budget_usd setTests((t) => ({ ...t, [it.id]: { ok: true, message: verdict.status === "ok" ? `OK — within budget & rate${remaining ? ` (${formatUsd(remaining)} left)` : ""}` : verdict.status, }, })) } catch (e) { const msg = e instanceof ArcadiaError ? e.status === 409 ? "Credential expired — rotate it" : e.status === 429 ? "Over budget / rate limit" : e.status === 404 ? "No credential to test" : e.message : "Test failed" setTests((t) => ({ ...t, [it.id]: { ok: false, message: msg } })) } }, [gw], ) const toggleEnabled = useCallback( async (it: Integration, enabled: boolean) => { setItems((xs) => xs.map((x) => (x.id === it.id ? { ...x, enabled } : x))) try { await updateIntegration(gw, it.id, { enabled }) } catch { setItems((xs) => xs.map((x) => (x.id === it.id ? { ...x, enabled: !enabled } : x))) } }, [gw], ) const remove = useCallback( async (it: Integration) => { if (!window.confirm(`Delete ${it.display_name || it.provider} and its credentials?`)) return await deleteIntegration(gw, it.id) await refresh() }, [gw, refresh], ) return (

Integrations

Platform & pooled external-API credentials across every scope. Keys are stored encrypted and never shown; usage is metadata only.

{error ? ( Couldn’t load integrations {error} ) : loading ? (

Loading…

) : items.length === 0 ? ( No integrations in this scope Register a platform/pooled arrangement — a shared key the platform meters and bills to tenants who opt in. ) : (
{items.map((it) => { const u = usageById.get(it.id) const test = tests[it.id] return (
{it.display_name || it.provider} {it.scope} {it.scope_id ? ( {it.scope_id} ) : null} {it.capability ? ( {it.capability} ) : null} {it.provider} {it.cost_model?.price_usd ? ` · ${formatUsd(it.cost_model.price_usd)}/${it.cost_model.unit ?? "call"}` : ""} {it.constraints?.monthly_budget_usd ? ` · budget ${formatUsd(it.constraints.monthly_budget_usd)}/mo` : ""}
toggleEnabled(it, v)} />
{it.credentials.length === 0 ? (

No credential set.

) : ( it.credentials.map((cred) => { const health = credentialHealth(cred) return (
{cred.secret_name} {cred.source} {cred.expires_at ? ( expires {new Date(cred.expires_at).toLocaleDateString()} ) : null}
) }) )}

{u ? `${u.calls} calls · ${formatUsd(u.cost_usd)} this month` : "No usage yet"}

{test ? (

{test.message}

) : null}
) })}
)} {editing ? ( setEditing(null)} onSaved={async () => { setEditing(null) await refresh() }} /> ) : null}
) } function HealthBadge({ health }: { health: ReturnType }) { if (health === "ok") return ( healthy ) const label = health === "missing" ? "no secret" : health return ( {label} ) } function IntegrationDialog({ mode, initial, onClose, onSaved, }: { mode: "new" | "edit" initial: Integration | null onClose: () => void onSaved: () => void | Promise }) { const gw = useGatewayClient() const [form, setForm] = useState
(() => initial ? { ...emptyForm, scope: initial.scope, scope_id: initial.scope_id ?? "", provider: initial.provider, capability: initial.capability ?? "", display_name: initial.display_name ?? "", unit: initial.cost_model?.unit ?? "call", price_usd: initial.cost_model?.price_usd?.toString() ?? "", monthly_budget_usd: initial.constraints?.monthly_budget_usd?.toString() ?? "", } : emptyForm, ) const [saving, setSaving] = useState(false) const [err, setErr] = useState(null) const set = (patch: Partial) => setForm((f) => ({ ...f, ...patch })) const needsScopeId = form.scope !== "platform" const submit = async () => { setSaving(true) setErr(null) try { const cost_model = form.price_usd ? { unit: form.unit as "call" | "search" | "1k_tokens", price_usd: form.price_usd, currency: "USD" } : undefined const constraints = form.monthly_budget_usd ? { monthly_budget_usd: form.monthly_budget_usd } : undefined if (mode === "edit" && initial) { await updateIntegration(gw, initial.id, { provider: form.provider.trim(), capability: form.capability.trim() || undefined, display_name: form.display_name.trim() || undefined, cost_model, constraints, }) } else { const created = await createIntegration(gw, { scope: form.scope, scope_id: needsScopeId ? form.scope_id.trim() || undefined : undefined, provider: form.provider.trim(), capability: form.capability.trim() || undefined, display_name: form.display_name.trim() || undefined, cost_model, constraints, }) if (form.secret_name.trim() && form.secret.trim()) { await addCredential(gw, created.id, { secret_name: form.secret_name.trim(), auth_kind: form.auth_kind, secret: form.secret, source: form.pooled ? "pooled" : "byo", }) } } await onSaved() } catch (e) { setErr(e instanceof Error ? e.message : "Save failed.") } finally { setSaving(false) } } return ( (!o ? onClose() : undefined)}> {mode === "new" ? "Add integration" : "Edit integration"} Register an external-API arrangement. Platform scope = a pooled key the platform meters and bills.
{mode === "new" ? (
set({ scope_id: e.target.value })} disabled={!needsScopeId} placeholder={needsScopeId ? "acme" : "—"} />
) : null} set({ provider: e.target.value })} placeholder="tavily" /> set({ capability: e.target.value })} placeholder="web_search" /> set({ display_name: e.target.value })} />
set({ price_usd: e.target.value })} placeholder="0.01" />
set({ monthly_budget_usd: e.target.value })} placeholder="500" /> {mode === "new" ? (

Credential (optional)

set({ secret_name: e.target.value })} placeholder="tavily_default" />
set({ pooled: v })} />
set({ secret: e.target.value })} placeholder="sk-…" />
) : null} {err ?

{err}

: null}
) } function Field({ label, hint, children, }: { label: string hint?: string children: React.ReactNode }) { return (
{children} {hint ?

{hint}

: null}
) }