@adonis-agora/agent
Version:
Governed, durable-backed AI agent (chat + tool-calling + governance) for AdonisJS — part of the Agora ecosystem.
57 lines • 2.06 kB
JavaScript
import { useEffect, useState } from 'react';
import { useAgentBackend } from '../provider.js';
// One request per backend for the whole page: the config is a deployment constant. A failure is
// forgotten so the next mount retries.
const shared = new WeakMap();
function load(backend) {
const getConfig = backend.getConfig;
if (typeof getConfig !== 'function')
return undefined;
let pending = shared.get(backend);
if (pending === undefined) {
pending = getConfig.call(backend).catch((error) => {
shared.delete(backend);
throw error;
});
shared.set(backend, pending);
}
return pending;
}
/**
* The server's client config (`GET <base>/config`): attachment limits and upload mode, and whether a
* model picker, a quota and anonymous identities are in play. Fetched once per backend and shared;
* `useAttachments` takes its defaults from it.
*/
export function useAgentConfig(options = {}) {
const backend = useAgentBackend(options.backend);
const enabled = options.enabled !== false;
const [config, setConfig] = useState(undefined);
const [isLoading, setLoading] = useState(false);
const [error, setError] = useState(null);
useEffect(() => {
if (!enabled)
return;
const pending = load(backend);
if (pending === undefined)
return;
let cancelled = false;
setLoading(true);
pending.then((loaded) => {
if (cancelled)
return;
setConfig(loaded !== null && typeof loaded === 'object' ? loaded : undefined);
setError(null);
setLoading(false);
}, (failure) => {
if (cancelled)
return;
setError(failure instanceof Error ? failure : new Error(String(failure)));
setLoading(false);
});
return () => {
cancelled = true;
};
}, [backend, enabled]);
return { config, isLoading, error };
}
//# sourceMappingURL=use-agent-config.js.map