UNPKG

@adonis-agora/agent

Version:

Governed, durable-backed AI agent (chat + tool-calling + governance) for AdonisJS — part of the Agora ecosystem.

47 lines • 2 kB
import { useEffect, useMemo } from 'react'; import { quotaWarning, } from '../../../spi/quota-provider.js'; import { requireBackendMethod } from '../backend.js'; import { useResource } from '../catalog/use-resource.js'; import { useAgentBackend } from '../provider.js'; import { onThreadsEvent } from '../threads/threads-events.js'; /** * The caller's budget (`GET <base>/quota`): every window's usage against its ceilings, and which * one — if any — blocks sends. Re-read after every run a chat on the same backend settles, so a * meter moves with the conversation. Headless: draw the meter yourself. */ export function useQuota(options = {}) { const { enabled = true, pollMs } = options; const backend = useAgentBackend(options.backend); const { data, isLoading, error, refresh } = useResource(() => requireBackendMethod(backend, 'getQuota')(), '', enabled); useEffect(() => { if (!enabled) return; return onThreadsEvent(backend, (event) => { if (event.type === 'changed') void refresh(); }); }, [backend, enabled, refresh]); useEffect(() => { if (!enabled || pollMs === undefined || pollMs <= 0) return; const timer = setInterval(() => void refresh(), pollMs); return () => clearInterval(timer); }, [enabled, pollMs, refresh]); return useMemo(() => { const windows = data?.windows ?? []; return { report: data, windows, day: windows.find((window) => window.period === 'day'), month: windows.find((window) => window.period === 'month'), blocked: data?.blocked ?? null, warning: data === undefined || data.blocked !== undefined ? null : (data.warning ?? quotaWarning(windows) ?? null), isLoading, error, refresh, }; }, [data, isLoading, error, refresh]); } //# sourceMappingURL=use-quota.js.map