Spaces:
Paused
Paused
File size: 1,121 Bytes
0b9dc2e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | import { useEffect, useState } from 'react';
import { agentApi } from '@/api';
import type { AgentSchemaResponse } from '@/api';
/**
* Module-level cache. The agent schema is derived from Pydantic models
* on the backend and never changes at runtime, so a single in-flight fetch
* is enough for the whole session.
*/
let cached: AgentSchemaResponse | null = null;
let inflight: Promise<AgentSchemaResponse> | null = null;
async function loadSchema(): Promise<AgentSchemaResponse> {
if (cached) return cached;
if (!inflight) {
inflight = agentApi.getSchema().then((res) => {
cached = res;
return res;
});
}
return inflight;
}
/** Fetch (and cache) the JSON Schema fragments for the agent form. */
export function useAgentSchema() {
const [schema, setSchema] = useState<AgentSchemaResponse | null>(cached);
const [error, setError] = useState<Error | null>(null);
useEffect(() => {
if (cached) return;
loadSchema()
.then(setSchema)
.catch((e: Error) => setError(e));
}, []);
return { schema, loading: schema === null && error === null, error };
}
|