import {z} from 'zod' export type RegistryResourceType = 'tool' | 'model' | 'db' | 'vector_db' | 'schema' | 'function' | 'agent' | 'team' | 'knowledge' | 'memory_manager' | 'session_summary_manager'; export type GetApiRegistryArgs = {resource_type?: (RegistryResourceType | null) | undefined, name?: (string | null) | undefined, page?: number | undefined, limit?: number | undefined}; export type GetApiRegistryOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const registryResourceType = z.enum(["tool", "model", "db", "vector_db", "schema", "function", "agent", "team", "knowledge", "memory_manager", "session_summary_manager"]); export const registryContentResponse = z.object({name: z.string(), type: registryResourceType, id: z.string().nullable().optional(), description: z.string().nullable().optional(), metadata: z.record(z.string(), z.unknown()).nullable().optional()}); export const paginationInfo = z.object({page: z.number().int().gte(0).optional(), limit: z.number().int().gte(1).optional(), total_pages: z.number().int().gte(0).optional(), total_count: z.number().int().gte(0).optional(), search_time_ms: z.number().gte(0).optional()}); export const paginatedResponseRegistryContentResponse = z.object({data: z.array(registryContentResponse), meta: paginationInfo}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * List Registry * * GET /registry */ export const getApiRegistry = async (args: GetApiRegistryArgs, options: GetApiRegistryOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.resource_type != null) query.append("resource_type", String(args.resource_type)) if (args.name != null) query.append("name", String(args.name)) if (args.page != null) query.append("page", String(args.page)) if (args.limit != null) query.append("limit", String(args.limit)) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/registry${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(paginatedResponseRegistryContentResponse, await res.json()) };