UNPKG

@adonis-agora/agent

Version:

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

61 lines • 3.16 kB
import { jsx as _jsx } from "react/jsx-runtime"; import { useContext, useMemo, useRef } from 'react'; import { AgentClient } from './client.js'; import { sharedContext } from './components/ambient-ui.js'; import { GenuiProvider } from './genui/generative-ui.js'; // Shared by key, like the genui contexts: `/media` and `/genui` are separate bundles. const AgentBackendContext = sharedContext('@adonis-agora/agent/react:agent-backend'); /** * The agent connection, once, at the app root. Every hook below it — `useAgentChat`, `useThreads`, * `useModels`, `useAgents`, `useQuota`, `useToolCatalog`, `useMessageFeedback`, `useAttachments` — * talks to it unless handed a `backend` of its own. * * ```tsx * <AgentProvider baseUrl="https://api.example.com" credentials="include"> * <App /> * </AgentProvider> * ``` * * Without a provider every hook shares one same-origin client on `/agent`. */ export function AgentProvider({ backend, uiCapabilities, baseUrl, path, headers, getHeaders, credentials, fetch, onHttpError, attachments, genui, children, }) { // Headers are read per request, so a re-render with new ones reaches the same client. const latestHeaders = useRef({ headers, getHeaders, onHttpError }); latestHeaders.current = { headers, getHeaders, onHttpError }; const upload = attachments?.upload; const value = useMemo(() => backend ?? new AgentClient({ ...(baseUrl !== undefined ? { baseUrl } : {}), ...(path !== undefined ? { path } : {}), ...(credentials !== undefined ? { credentials } : {}), ...(fetch !== undefined ? { fetch } : {}), ...(upload !== undefined ? { attachments: { upload } } : {}), onHttpError: (error) => latestHeaders.current.onHttpError?.(error), getHeaders: async () => { const current = latestHeaders.current; const dynamic = (await current.getHeaders?.()) ?? {}; return { ...current.headers, ...dynamic }; }, }), [backend, baseUrl, path, credentials, fetch, upload]); const content = genui !== undefined ? _jsx(GenuiProvider, { ...genui, children: children }) : children; return (_jsx(AgentUiCapabilitiesContext.Provider, { value: uiCapabilities ?? null, children: _jsx(AgentBackendContext.Provider, { value: value, children: content }) })); } let fallback; /** The client every hook uses outside an {@link AgentProvider}: same origin, `/agent`. */ function defaultBackend() { fallback ??= new AgentClient(); return fallback; } /** * The backend a hook talks to: `own` when given, else the enclosing {@link AgentProvider}'s, else a * shared same-origin client on `/agent`. For your own hooks over the same connection. */ export function useAgentBackend(own) { const provided = useContext(AgentBackendContext); return (own ?? provided ?? defaultBackend()); } const AgentUiCapabilitiesContext = sharedContext('@adonis-agora/agent/react:ui-capabilities'); export function useAgentUiCapabilities() { return useContext(AgentUiCapabilitiesContext) ?? undefined; } //# sourceMappingURL=provider.js.map